diff --git a/obmdataapi/__init__.py b/obmdataapi/__init__.py index 4a839c38cace60a7e09303e42d621647823b7991..4c6c2d89c31ba8abbfb6e30f4eb096084e6b6c9f 100644 --- a/obmdataapi/__init__.py +++ b/obmdataapi/__init__.py @@ -79,8 +79,36 @@ async def heartbeat() -> dict: @app.api.add_function async def get_building(osm_id) -> dict: - building = await app.db.fetch_val( - "SELECT osm_id FROM osm_building_polygons WHERE osm_id = :osm_id", + result = await app.db.fetch_all( + "SELECT * FROM osm_building_polygons WHERE osm_id = :osm_id", values={"osm_id": int(osm_id)}, ) - return {"building": str(building)} + values = {} + for building in result: + for key, value in building.items(): + values[key] = value + return {"building": values} + + +@app.api.add_function +async def get_buildings_by_bbox(xmin, ymin, xmax, ymax) -> dict: + buildings = await app.db.fetch_all( + "SELECT * FROM osm_building_polygons \ + WHERE geometry && ST_MakeEnvelope(:xmin, :ymin, :xmax, :ymax, 4326)", + values={ + "xmin": float(xmin), + "ymin": float(ymin), + "xmax": float(xmax), + "ymax": float(ymax), + }, + ) + buildings_data_object = {} + for building in buildings: + osm_id = 0 + values = {} + for key, value in building.items(): + values[key] = value + if key == "osm_id": + osm_id = value + buildings_data_object[osm_id] = values + return {"buildings": buildings_data_object} diff --git a/tests/test_obmdataapi.py b/tests/test_obmdataapi.py index a4c7f76db8bfdebddc248af87917b082367d4079..43e2a8162b3253ceec57edea10565a9831ab8df1 100644 --- a/tests/test_obmdataapi.py +++ b/tests/test_obmdataapi.py @@ -40,4 +40,5 @@ def test_heartbeat(client) -> None: def test_get_building(client) -> None: response = request("http://127.0.0.1:5000/v1", "get_building", "-12412416").data.result - assert "-12412416" in response["building"] + print(response["building"]["osm_id"]) + assert -12412416 == response["building"]["osm_id"]