Files
gta6/tools/extract/tests/test_osm.py
T

53 lines
2.4 KiB
Python

import json
from osm import dumps, keep, overpass_query, snapshot
def way(tags, geometry=((25.78, -80.137), (25.781, -80.137)), id=1):
return {"type": "way", "id": id, "tags": tags, "geometry": [{"lat": a, "lon": b} for a, b in geometry]}
def test_keep_takes_park_paths_fields_pools_parking_and_buildings():
for tags in ({"highway": "footway"}, {"highway": "service"}, {"highway": "steps"},
{"leisure": "pitch", "sport": "tennis"}, {"leisure": "swimming_pool"}, {"leisure": "dog_park"},
{"amenity": "parking"}, {"building": "yes"}, {"building": "yes", "leisure": "sports_centre"}):
assert keep(way(tags)), tags
def test_keep_drops_streets_street_sidewalks_and_the_park_outline():
for tags in ({"highway": "residential"}, {"highway": "footway", "footway": "sidewalk"},
{"leisure": "park"}, {"amenity": "restaurant"}):
assert not keep(way(tags)), tags
def test_keep_takes_the_sidewalk_tagged_piece_of_the_13th_street_path():
assert keep(way({"highway": "footway", "footway": "sidewalk", "name": "Flamingo Park Path (13th St)"}))
def test_keep_needs_a_way_with_geometry():
assert not keep({"type": "node", "id": 1, "tags": {"building": "yes"}, "lat": 25.78, "lon": -80.13})
assert not keep({"type": "way", "id": 1, "tags": {"building": "yes"}, "geometry": []})
def test_snapshot_keeps_id_tags_and_lat_lon_pairs_with_licence_and_timestamp():
raw = {"osm3s": {"timestamp_osm_base": "2026-09-15T20:28:44Z"},
"elements": [way({"highway": "residential"}, id=7), way({"highway": "footway"}, id=3)]}
snap = snapshot(raw)
assert snap["fetched"] == "2026-09-15T20:28:44Z"
assert "ODbL" in snap["license"] and "OpenStreetMap" in snap["license"]
assert snap["elements"] == [{"id": 3, "tags": {"highway": "footway"},
"geometry": [[25.78, -80.137], [25.781, -80.137]]}]
def test_dumps_writes_one_line_per_element_and_reads_back():
snap = snapshot({"elements": [way({"building": "yes"}, id=2), way({"highway": "path"}, id=1)]})
text = dumps(snap)
assert json.loads(text) == snap
assert sum(1 for line in text.splitlines() if line.lstrip().startswith('{"id"')) == 2
def test_query_asks_for_ways_with_geometry_in_the_box():
query = overpass_query((1, 2, 3, 4))
assert "way(1,2,3,4)[highway]" in query and "way(1,2,3,4)[building]" in query
assert "way(1,2,3,4)[amenity=parking]" in query and query.endswith("out geom;")