-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathtest_osm_snapshot.py
More file actions
359 lines (288 loc) · 13 KB
/
test_osm_snapshot.py
File metadata and controls
359 lines (288 loc) · 13 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
# -------------------------------------------------------------
# Copyright (c) Henry Spatial Analysis. All rights reserved.
# Licensed under the MIT License. See LICENSE in project root for information.
# -------------------------------------------------------------
"""
Unit tests for openpois.io.osm_snapshot.
All external I/O (requests.get, subprocess.run, osmium.FileProcessor)
is mocked so tests run in milliseconds without network or filesystem access.
"""
from __future__ import annotations
from unittest.mock import MagicMock, patch
import osmium
import pytest
from shapely.geometry import Point, Polygon
from openpois.io._osm_poi_handler import POIRecordBuilder
from openpois.io.osm_snapshot import (
download_pbf,
filter_pbf,
parse_pbf_to_geodataframe,
)
# ---------------------------------------------------------------------------
# Helpers: fake osmium objects
# ---------------------------------------------------------------------------
def _make_tags(tag_dict: dict) -> MagicMock:
"""Return a mock osmium TagList from a plain dict."""
tags = MagicMock()
tags.__iter__ = MagicMock(return_value=iter(tag_dict.keys()))
tags.__contains__ = MagicMock(side_effect=lambda k: k in tag_dict)
tags.get = MagicMock(side_effect=lambda k, default=None: tag_dict.get(k, default))
return tags
def _make_node(osm_id: int, lon: float, lat: float, tag_dict: dict) -> MagicMock:
node = MagicMock()
node.id = osm_id
node.location.lon = lon
node.location.lat = lat
node.tags = _make_tags(tag_dict)
node.timestamp = None
node.is_node = MagicMock(return_value=True)
node.is_way = MagicMock(return_value=False)
node.is_area = MagicMock(return_value=False)
return node
def _make_way_nodes(coords: list[tuple[float, float]]) -> list[MagicMock]:
way_nodes = []
for lon, lat in coords:
nd = MagicMock()
nd.lon = lon
nd.lat = lat
way_nodes.append(nd)
return way_nodes
def _make_way(
osm_id: int, coords: list[tuple[float, float]], tag_dict: dict
) -> MagicMock:
way = MagicMock()
way.id = osm_id
way.nodes = _make_way_nodes(coords)
way.tags = _make_tags(tag_dict)
return way
def _make_file_processor(objects: list) -> MagicMock:
"""Return a mock osmium.FileProcessor that yields the given objects."""
fp = MagicMock()
fp.with_locations.return_value = fp
fp.with_areas.return_value = fp
fp.__iter__ = MagicMock(return_value=iter(objects))
return fp
# ---------------------------------------------------------------------------
# download_pbf
# ---------------------------------------------------------------------------
class TestDownloadPbf:
def test_skips_if_exists_and_no_overwrite(self, tmp_path):
"""Should return early without calling requests.get when file exists."""
output = tmp_path / "out.pbf"
output.write_bytes(b"fake")
with patch("openpois.io.osm_snapshot.requests.get") as mock_get:
result = download_pbf("http://example.com/file.pbf", output, overwrite=False)
mock_get.assert_not_called()
assert result == output
def test_downloads_when_no_file(self, tmp_path):
"""Should call requests.get with stream=True and write content."""
output = tmp_path / "subdir" / "out.pbf"
mock_resp = MagicMock()
mock_resp.__enter__ = MagicMock(return_value=mock_resp)
mock_resp.__exit__ = MagicMock(return_value=False)
mock_resp.headers = {"content-length": "10"}
mock_resp.iter_content = MagicMock(return_value=[b"hellworld!"])
with patch(
"openpois.io.osm_snapshot.requests.get", return_value=mock_resp
) as mock_get:
result = download_pbf(
"http://example.com/file.pbf", output, overwrite=False
)
mock_get.assert_called_once_with(
"http://example.com/file.pbf", stream=True, timeout=(30, None)
)
assert result == output
assert output.exists()
def test_overwrites_existing_file(self, tmp_path):
"""Should call requests.get even if file already exists when overwrite=True."""
output = tmp_path / "out.pbf"
output.write_bytes(b"old content")
mock_resp = MagicMock()
mock_resp.__enter__ = MagicMock(return_value=mock_resp)
mock_resp.__exit__ = MagicMock(return_value=False)
mock_resp.headers = {}
mock_resp.iter_content = MagicMock(return_value=[b"new"])
with patch(
"openpois.io.osm_snapshot.requests.get", return_value=mock_resp
) as mock_get:
download_pbf("http://example.com/file.pbf", output, overwrite=True)
mock_get.assert_called_once()
# ---------------------------------------------------------------------------
# filter_pbf
# ---------------------------------------------------------------------------
class TestFilterPbf:
def test_skips_if_output_exists_and_no_overwrite(self, tmp_path):
"""Should return early without calling subprocess.run when output exists."""
input_pbf = tmp_path / "in.pbf"
output_pbf = tmp_path / "out.pbf"
input_pbf.write_bytes(b"fake")
output_pbf.write_bytes(b"fake")
with patch("openpois.io.osm_snapshot.subprocess.run") as mock_run:
result = filter_pbf(input_pbf, output_pbf, ["amenity"], overwrite=False)
mock_run.assert_not_called()
assert result == output_pbf
def test_runs_osmium_command_with_correct_args(self, tmp_path):
"""Should construct osmium tags-filter command with nwr/ prefixed keys."""
input_pbf = tmp_path / "in.pbf"
output_pbf = tmp_path / "out.pbf"
input_pbf.write_bytes(b"fake")
osm_keys = ["amenity", "shop", "tourism"]
with (
patch("openpois.io.osm_snapshot.subprocess.run") as mock_run,
patch(
"openpois.io.osm_snapshot.shutil.which",
return_value = "/usr/bin/osmium",
),
):
filter_pbf(input_pbf, output_pbf, osm_keys, overwrite=False)
mock_run.assert_called_once()
cmd = mock_run.call_args[0][0]
assert cmd[1] == "tags-filter"
assert "-o" in cmd
for key in osm_keys:
assert f"nwr/{key}" in cmd
assert mock_run.call_args[1].get("check") is True
def test_overwrites_when_flag_set(self, tmp_path):
"""Should call subprocess.run even if output exists when overwrite=True."""
input_pbf = tmp_path / "in.pbf"
output_pbf = tmp_path / "out.pbf"
input_pbf.write_bytes(b"fake")
output_pbf.write_bytes(b"existing")
with (
patch("openpois.io.osm_snapshot.subprocess.run") as mock_run,
patch(
"openpois.io.osm_snapshot.shutil.which",
return_value = "/usr/bin/osmium",
),
):
filter_pbf(input_pbf, output_pbf, ["amenity"], overwrite=True)
mock_run.assert_called_once()
# ---------------------------------------------------------------------------
# POIRecordBuilder.process_node
# ---------------------------------------------------------------------------
class TestPOIRecordBuilderNode:
def test_returns_record_when_tag_matches(self):
"""process_node() should return a record dict when a filter key matches."""
builder = POIRecordBuilder(
filter_keys=["amenity", "shop"],
extract_keys=["amenity", "shop"],
source_label="osm_test",
)
node = _make_node(
osm_id=42,
lon=-122.3,
lat=47.6,
tag_dict={"amenity": "restaurant", "name": "Good Eats"},
)
rec = builder.process_node(node)
assert rec is not None
assert rec["osm_id"] == 42
assert rec["osm_type"] == "node"
assert rec["source"] == "osm_test"
assert rec["amenity"] == "restaurant"
assert rec["shop"] is None # in extract_keys but absent from tags
assert rec["name"] == "Good Eats"
assert isinstance(rec["geometry"], Point)
assert rec["geometry"].x == pytest.approx(-122.3)
assert rec["geometry"].y == pytest.approx(47.6)
def test_returns_none_when_no_matching_tag(self):
"""process_node() should return None when no filter key is present."""
builder = POIRecordBuilder(filter_keys=["amenity"], source_label="osm")
node = _make_node(osm_id=99, lon=0.0, lat=0.0, tag_dict={"highway": "crossing"})
assert builder.process_node(node) is None
# ---------------------------------------------------------------------------
# POIRecordBuilder.process_way
# ---------------------------------------------------------------------------
class TestPOIRecordBuilderWay:
def test_closed_way_produces_polygon(self):
"""A closed way with >= 4 coords should produce a Polygon geometry."""
builder = POIRecordBuilder(
filter_keys=["amenity"], extract_keys=["amenity"], source_label="osm"
)
coords = [(-122.0, 47.0), (-121.9, 47.0), (-121.9, 47.1), (-122.0, 47.0)]
way = _make_way(osm_id=10, coords=coords, tag_dict={"amenity": "school"})
rec = builder.process_way(way)
assert rec is not None
assert isinstance(rec["geometry"], Polygon)
assert rec["osm_type"] == "way"
def test_open_way_produces_centroid_point(self):
"""An open way should produce a Point (centroid of LineString)."""
builder = POIRecordBuilder(
filter_keys=["amenity"], extract_keys=["amenity"], source_label="osm"
)
coords = [(-122.0, 47.0), (-121.9, 47.1), (-121.8, 47.2)]
way = _make_way(osm_id=20, coords=coords, tag_dict={"amenity": "parking"})
rec = builder.process_way(way)
assert rec is not None
assert isinstance(rec["geometry"], Point)
def test_invalid_location_error_returns_none(self):
"""process_way() should return None on InvalidLocationError."""
builder = POIRecordBuilder(filter_keys=["amenity"], source_label="osm")
way = MagicMock()
way.id = 30
way.tags = _make_tags({"amenity": "cafe"})
def _raising_iter():
raise osmium.InvalidLocationError("no location")
way.nodes.__iter__ = MagicMock(side_effect=_raising_iter)
assert builder.process_way(way) is None
def test_returns_none_when_no_matching_tag(self):
"""process_way() should return None when no filter key is present."""
builder = POIRecordBuilder(filter_keys=["amenity"], source_label="osm")
coords = [(-122.0, 47.0), (-121.9, 47.1)]
way = _make_way(osm_id=40, coords=coords, tag_dict={"highway": "path"})
assert builder.process_way(way) is None
def test_way_with_fewer_than_two_coords_returns_none(self):
"""process_way() should return None for ways with fewer than 2 coords."""
builder = POIRecordBuilder(filter_keys=["amenity"], source_label="osm")
coords = [(-122.0, 47.0)]
way = _make_way(osm_id=50, coords=coords, tag_dict={"amenity": "bench"})
assert builder.process_way(way) is None
# ---------------------------------------------------------------------------
# parse_pbf_to_geodataframe
# ---------------------------------------------------------------------------
class TestParsePbfToGeoDataFrame:
def test_returns_empty_geodataframe_with_correct_schema_when_no_records(
self, tmp_path
):
"""Should return an empty GeoDataFrame with the expected columns."""
pbf_path = tmp_path / "empty.pbf"
pbf_path.write_bytes(b"fake")
extract_keys = ["amenity", "shop"]
with patch(
"openpois.io.osm_snapshot.osmium.FileProcessor",
return_value=_make_file_processor([]),
):
gdf = parse_pbf_to_geodataframe(
pbf_path,
filter_keys=extract_keys,
extract_keys=extract_keys,
verbose=False,
)
assert len(gdf) == 0
assert gdf.crs.to_epsg() == 4326
for col in ["source", "osm_id", "osm_type", "name", "geometry"] + extract_keys:
assert col in gdf.columns
def test_returns_geodataframe_from_handler_records(self, tmp_path):
"""Should build a GeoDataFrame from records produced by POIRecordBuilder."""
pbf_path = tmp_path / "pois.pbf"
pbf_path.write_bytes(b"fake")
fake_node = _make_node(
osm_id=1,
lon=-122.0,
lat=47.0,
tag_dict={"amenity": "cafe", "name": "A Cafe"},
)
with patch(
"openpois.io.osm_snapshot.osmium.FileProcessor",
return_value=_make_file_processor([fake_node]),
):
gdf = parse_pbf_to_geodataframe(
pbf_path,
filter_keys=["amenity"],
extract_keys=["amenity"],
source_label="osm",
verbose=False,
)
assert len(gdf) == 1
assert gdf.iloc[0]["amenity"] == "cafe"
assert gdf.crs.to_epsg() == 4326