Skip to content

Commit 652e885

Browse files
committed
Merge branch 'feature/street' of https://github.com/mapswipe/python-mapswipe-workers into feature/street
2 parents 7f46e80 + 24c7eec commit 652e885

File tree

8 files changed

+58
-52
lines changed

8 files changed

+58
-52
lines changed

.github/workflows/actions.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,7 @@ jobs:
8080
POSTGRES_DB: postgres
8181
OSMCHA_API_KEY: ${{ secrets.OSMCHA_API_KEY }}
8282
DJANGO_SECRET_KEY: test-django-secret-key
83+
MAPILLARY_API_KEY: test-mapillary-api-key
8384
COMPOSE_FILE: ../docker-compose.yaml:../docker-compose-ci.yaml
8485
run: |
8586
docker compose run --rm mapswipe_workers_creation python -m unittest discover --verbose --start-directory tests/unittests/

mapswipe_workers/mapswipe_workers/definitions.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
OSMCHA_API_LINK = "https://osmcha.org/api/v1/"
1818
OSMCHA_API_KEY = os.environ["OSMCHA_API_KEY"]
1919
MAPILLARY_API_LINK = "https://tiles.mapillary.com/maps/vtp/mly1_computed_public/2/"
20-
MAPILLARY_API_KEY = os.environ["MAPILLARY_API_KEY"]
20+
MAPILLARY_API_KEY = os.getenv("MAPILLARY_API_KEY")
2121

2222
# number of geometries for project geometries
2323
MAX_INPUT_GEOMETRIES = 10

mapswipe_workers/mapswipe_workers/project_types/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,13 @@
22
from .arbitrary_geometry.footprint.project import FootprintProject
33
from .arbitrary_geometry.footprint.tutorial import FootprintTutorial
44
from .media_classification.project import MediaClassificationProject
5+
from .street.project import StreetProject
56
from .tile_map_service.change_detection.project import ChangeDetectionProject
67
from .tile_map_service.change_detection.tutorial import ChangeDetectionTutorial
78
from .tile_map_service.classification.project import ClassificationProject
89
from .tile_map_service.classification.tutorial import ClassificationTutorial
910
from .tile_map_service.completeness.project import CompletenessProject
1011
from .tile_map_service.completeness.tutorial import CompletenessTutorial
11-
from .street.project import StreetProject
1212

1313
__all__ = [
1414
"ClassificationProject",

mapswipe_workers/mapswipe_workers/project_types/street/project.py

Lines changed: 8 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,8 @@
1-
import json
2-
import os
3-
import urllib
41
import math
5-
6-
from osgeo import ogr
72
from dataclasses import dataclass
8-
from mapswipe_workers.definitions import DATA_PATH, logger
3+
from typing import Dict, List
4+
5+
from mapswipe_workers.definitions import logger
96
from mapswipe_workers.firebase.firebase import Firebase
107
from mapswipe_workers.firebase_to_postgres.transfer_results import (
118
results_to_file,
@@ -15,16 +12,16 @@
1512
from mapswipe_workers.generate_stats.project_stats import (
1613
get_statistics_for_integer_result_project,
1714
)
15+
from mapswipe_workers.project_types.project import BaseGroup, BaseProject, BaseTask
16+
from mapswipe_workers.utils.process_mapillary import get_image_metadata
1817
from mapswipe_workers.utils.validate_input import (
19-
check_if_layer_is_empty,
20-
load_geojson_to_ogr,
2118
build_multipolygon_from_layer_geometries,
2219
check_if_layer_has_too_many_geometries,
23-
save_geojson_to_file,
20+
check_if_layer_is_empty,
21+
load_geojson_to_ogr,
2422
multipolygon_to_wkt,
23+
save_geojson_to_file,
2524
)
26-
from mapswipe_workers.project_types.project import BaseProject, BaseTask, BaseGroup
27-
from mapswipe_workers.utils.process_mapillary import get_image_metadata
2825

2926

3027
@dataclass

mapswipe_workers/mapswipe_workers/utils/process_mapillary.py

Lines changed: 14 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,22 @@
1+
import os
2+
from concurrent.futures import ThreadPoolExecutor, as_completed
3+
14
import mercantile
2-
import json
5+
import pandas as pd
36
import requests
4-
import os
5-
import time
67
from shapely import (
7-
box,
8-
Polygon,
9-
MultiPolygon,
10-
Point,
118
LineString,
129
MultiLineString,
10+
MultiPolygon,
11+
Point,
12+
Polygon,
13+
box,
1314
unary_union,
1415
)
1516
from shapely.geometry import shape
16-
import pandas as pd
1717
from vt2geojson import tools as vt2geojson_tools
18-
from concurrent.futures import ThreadPoolExecutor, as_completed
19-
from mapswipe_workers.definitions import MAPILLARY_API_LINK, MAPILLARY_API_KEY
20-
from mapswipe_workers.definitions import logger
18+
19+
from mapswipe_workers.definitions import MAPILLARY_API_KEY, MAPILLARY_API_LINK, logger
2120
from mapswipe_workers.utils.spatial_sampling import spatial_sampling
2221

2322

@@ -137,8 +136,8 @@ def coordinate_download(
137136
downloaded_metadata[col] = None
138137

139138
if (
140-
downloaded_metadata.isna().all().all() == False
141-
or downloaded_metadata.empty == True
139+
downloaded_metadata.isna().all().all() is False
140+
or downloaded_metadata.empty is True
142141
):
143142
downloaded_metadata = downloaded_metadata[
144143
downloaded_metadata["geometry"].apply(
@@ -240,8 +239,8 @@ def get_image_metadata(
240239
if sampling_threshold is not None:
241240
downloaded_metadata = spatial_sampling(downloaded_metadata, sampling_threshold)
242241
if (
243-
downloaded_metadata.isna().all().all() == False
244-
or downloaded_metadata.empty == False
242+
downloaded_metadata.isna().all().all() is False
243+
or downloaded_metadata.empty is False
245244
):
246245
if len(downloaded_metadata) > 100000:
247246
err = (

mapswipe_workers/mapswipe_workers/utils/spatial_sampling.py

Lines changed: 26 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,29 @@
11
import numpy as np
22
import pandas as pd
3-
from shapely import wkt
4-
from shapely.geometry import Point
53

64

75
def distance_on_sphere(p1, p2):
86
"""
9-
p1 and p2 are two lists that have two elements. They are numpy arrays of the long and lat
10-
coordinates of the points in set1 and set2
7+
p1 and p2 are two lists that have two elements. They are numpy arrays of the long
8+
and lat coordinates of the points in set1 and set2
119
12-
Calculate the distance between two points on the Earth's surface using the haversine formula.
10+
Calculate the distance between two points on the Earth's surface using the
11+
haversine formula.
1312
1413
Args:
15-
p1 (list): Array containing the longitude and latitude coordinates of points FROM which the distance to be calculated in degree
16-
p2 (list): Array containing the longitude and latitude coordinates of points TO which the distance to be calculated in degree
14+
p1 (list): Array containing the longitude and latitude coordinates of points
15+
FROM which the distance to be calculated in degree
16+
p2 (list): Array containing the longitude and latitude coordinates of points
17+
TO which the distance to be calculated in degree
1718
1819
Returns:
19-
numpy.ndarray: Array containing the distances between the two points on the sphere in kilometers.
20+
numpy.ndarray: Array containing the distances between the two points on the
21+
sphere in kilometers.
2022
21-
This function computes the distance between two points on the Earth's surface using the haversine formula,
22-
which takes into account the spherical shape of the Earth. The input arrays `p1` and `p2` should contain
23-
longitude and latitude coordinates in degrees. The function returns an array containing the distances
23+
This function computes the distance between two points on the Earth's surface
24+
using the haversine formula, which takes into account the spherical shape of the
25+
Earth. The input arrays `p1` and `p2` should contain longitude and latitude
26+
coordinates in degrees. The function returns an array containing the distances
2427
between corresponding pairs of points.
2528
"""
2629
earth_radius = 6371 # km
@@ -41,7 +44,7 @@ def distance_on_sphere(p1, p2):
4144
return distances
4245

4346

44-
"""-----------------------------------Filtering Points------------------------------------------------"""
47+
"""----------------------------Filtering Points-------------------------------"""
4548

4649

4750
def filter_points(df, threshold_distance):
@@ -56,10 +59,11 @@ def filter_points(df, threshold_distance):
5659
pandas.DataFrame: Filtered DataFrame containing selected points.
5760
float: Total road length calculated from the selected points.
5861
59-
This function filters points from a DataFrame based on the given threshold distance. It calculates
60-
distances between consecutive points and accumulates them until the accumulated distance surpasses
61-
the threshold distance. It then selects those points and constructs a new DataFrame. Additionally,
62-
it manually checks the last point to include it if it satisfies the length condition. The function
62+
This function filters points from a DataFrame based on the given threshold
63+
distance. It calculates distances between consecutive points and accumulates them
64+
until the accumulated distance surpasses the threshold distance. It then selects
65+
those points and constructs a new DataFrame. Additionally, it manually checks the
66+
last point to include it if it satisfies the length condition. The function
6367
returns the filtered DataFrame along with the calculated road length.
6468
"""
6569
road_length = 0
@@ -83,7 +87,8 @@ def filter_points(df, threshold_distance):
8387
accumulated_distance = 0 # Reset accumulated distance
8488

8589
to_be_returned_df = df[mask]
86-
# since the last point has to be omitted in the vectorized distance calculation, it is being checked manually
90+
# since the last point has to be omitted in the vectorized distance calculation,
91+
# it is being checked manually
8792
p2 = to_be_returned_df.iloc[0]
8893
distance = distance_on_sphere(
8994
[float(p2["long"]), float(p2["lat"])], [long[-1], lat[-1]]
@@ -114,10 +119,10 @@ def spatial_sampling(df, interval_length):
114119
geopandas.GeoDataFrame: Filtered GeoDataFrame containing selected points.
115120
float: Total road length calculated from the selected points.
116121
117-
This function calculates the spacing between points in a GeoDataFrame by filtering points
118-
based on the provided interval length. It first sorts the GeoDataFrame by timestamp and
119-
then filters points using the filter_points function. The function returns the filtered
120-
GeoDataFrame along with the total road length.
122+
This function calculates the spacing between points in a GeoDataFrame by filtering
123+
points based on the provided interval length. It first sorts the GeoDataFrame by
124+
timestamp and then filters points using the filter_points function. The function
125+
returns the filtered GeoDataFrame along with the total road length.
121126
"""
122127
if len(df) == 1:
123128
return df

mapswipe_workers/requirements.txt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,3 +14,6 @@ sentry-sdk==0.18.0
1414
six==1.15.0
1515
slackclient==2.9.2
1616
xdg==4.0.1
17+
shapely
18+
mercantile
19+
vt2geojson

mapswipe_workers/tests/integration/test_create_street_project.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
from tests.integration import set_up, tear_down
99

1010

11-
class TestCreateFootprintProject(unittest.TestCase):
11+
class TestCreateStreetProject(unittest.TestCase):
1212
def setUp(self):
1313
self.project_id = [
1414
set_up.create_test_project_draft("street", "street"),
@@ -33,7 +33,7 @@ def test_create_street_project(self):
3333
result = pg_db.retr_query(query, [element])[0][0]
3434
self.assertEqual(result, element)
3535

36-
# check if usernames made it to postgres
36+
# check if tasks made it to postgres
3737
query = """
3838
SELECT count(*)
3939
FROM tasks
@@ -51,10 +51,11 @@ def test_create_street_project(self):
5151
result = ref.get(shallow=True)
5252
self.assertIsNotNone(result)
5353

54-
# Footprint projects have tasks in Firebase
54+
# Street projects have tasks in Firebase
5555
ref = fb_db.reference(f"/v2/tasks/{element}")
5656
result = ref.get(shallow=True)
5757
self.assertIsNotNone(result)
5858

59+
5960
if __name__ == "__main__":
6061
unittest.main()

0 commit comments

Comments
 (0)