Skip to content

Commit d4dd6a0

Browse files
committed
refactor: extract functions for improved testing
1 parent bc2bcd5 commit d4dd6a0

File tree

1 file changed

+39
-35
lines changed

1 file changed

+39
-35
lines changed

mapswipe_workers/mapswipe_workers/utils/process_mapillary.py

Lines changed: 39 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -46,25 +46,7 @@ def download_and_process_tile(row, polygon, kwargs, attempt_limit=3):
4646
attempt = 0
4747
while attempt < attempt_limit:
4848
try:
49-
r = requests.get(url)
50-
assert r.status_code == 200, r.content
51-
features = vt2geojson_tools.vt_bytes_to_geojson(r.content, x, y, z).get(
52-
"features", []
53-
)
54-
data = []
55-
data.extend(
56-
[
57-
{
58-
"geometry": Point(feature["geometry"]["coordinates"]),
59-
**feature.get("properties", {}),
60-
}
61-
for feature in features
62-
if feature.get("geometry", {}).get("type") == "Point"
63-
]
64-
)
65-
66-
data = pd.DataFrame(data)
67-
49+
data = get_mapillary_data(url, x, y, z)
6850
if data.isna().all().all() is False or data.empty is False:
6951
data = data[data["geometry"].apply(lambda point: point.within(polygon))]
7052
target_columns = [
@@ -79,7 +61,6 @@ def download_and_process_tile(row, polygon, kwargs, attempt_limit=3):
7961
for col in target_columns:
8062
if col not in data.columns:
8163
data[col] = None
82-
8364
if data.isna().all().all() is False or data.empty is False:
8465
data = filter_results(data, **kwargs)
8566

@@ -92,6 +73,26 @@ def download_and_process_tile(row, polygon, kwargs, attempt_limit=3):
9273
return None
9374

9475

76+
def get_mapillary_data(url, x, y, z):
77+
r = requests.get(url)
78+
assert r.status_code == 200, r.content
79+
features = vt2geojson_tools.vt_bytes_to_geojson(r.content, x, y, z).get(
80+
"features", []
81+
)
82+
data = []
83+
data.extend(
84+
[
85+
{
86+
"geometry": Point(feature["geometry"]["coordinates"]),
87+
**feature.get("properties", {}),
88+
}
89+
for feature in features
90+
if feature.get("geometry", {}).get("type") == "Point"
91+
]
92+
)
93+
return pd.DataFrame(data)
94+
95+
9596
def coordinate_download(
9697
polygon, level, kwargs: dict, use_concurrency=True, workers=os.cpu_count() * 4
9798
):
@@ -103,25 +104,33 @@ def coordinate_download(
103104
if not use_concurrency:
104105
workers = 1
105106

106-
process_tile_with_args = partial(
107-
download_and_process_tile, polygon=polygon, kwargs=kwargs
107+
downloaded_metadata = parallelized_processing(
108+
downloaded_metadata, kwargs, polygon, tiles, workers
108109
)
109-
with ProcessPoolExecutor(max_workers=workers) as executor:
110-
futures = list(
111-
executor.map(process_tile_with_args, tiles.to_dict(orient="records"))
112-
)
113-
114-
for df in futures:
115-
if df is not None and not df.empty:
116-
downloaded_metadata.append(df)
117110
if len(downloaded_metadata):
111+
breakpoint()
118112
downloaded_metadata = pd.concat(downloaded_metadata, ignore_index=True)
119113
else:
120114
return pd.DataFrame(downloaded_metadata)
121115

122116
return downloaded_metadata
123117

124118

119+
def parallelized_processing(data, kwargs, polygon, tiles, workers):
120+
process_tile_with_args = partial(
121+
download_and_process_tile, polygon=polygon, kwargs=kwargs
122+
)
123+
with ProcessPoolExecutor(max_workers=workers) as executor:
124+
futures = list(
125+
executor.map(process_tile_with_args, tiles.to_dict(orient="records"))
126+
)
127+
128+
for df in futures:
129+
if df is not None and not df.empty:
130+
data.append(df)
131+
return data
132+
133+
125134
def geojson_to_polygon(geojson_data):
126135
if geojson_data["type"] == "FeatureCollection":
127136
features = geojson_data["features"]
@@ -176,7 +185,6 @@ def filter_results(
176185
df = df[df["creator_id"] == creator_id]
177186
if is_pano is not None:
178187
if df["is_pano"].isna().all():
179-
print(df)
180188
logger.exception("No Mapillary Feature in the AoI has a 'is_pano' value.")
181189
return None
182190
df = df[df["is_pano"] == is_pano]
@@ -219,10 +227,6 @@ def get_image_metadata(
219227
if downloaded_metadata.empty or downloaded_metadata.isna().all().all():
220228
raise ValueError("No Mapillary Features in the AoI.")
221229

222-
downloaded_metadata = downloaded_metadata[
223-
downloaded_metadata["geometry"].apply(lambda geom: isinstance(geom, Point))
224-
]
225-
226230
if (
227231
downloaded_metadata is None
228232
or downloaded_metadata.empty

0 commit comments

Comments
 (0)