diff --git a/rabotnikobm/rules/get_building_land_use.py b/rabotnikobm/rules/get_building_land_use.py new file mode 100644 index 0000000000000000000000000000000000000000..df3029412c3bc6b10b108f89e8aacec8c62f0d6f --- /dev/null +++ b/rabotnikobm/rules/get_building_land_use.py @@ -0,0 +1,41 @@ +#!/usr/bin/env python3 + +# Copyright (C) 2021: +# Helmholtz-Zentrum Potsdam Deutsches GeoForschungsZentrum GFZ +# +# This program is free software: you can redistribute it and/or modify it +# under the terms of the GNU Affero General Public License as published by +# the Free Software Foundation, either version 3 of the License, or (at +# your option) any later version. +# +# This program is distributed in the hope that it will be useful, but +# WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Affero +# General Public License for more details. +# +# You should have received a copy of the GNU Affero General Public License +# along with this program. If not, see http://www.gnu.org/licenses/. + +from typing import Tuple, Iterator + +from rabotnik import Rule + + +class GetBuildingLandUse(Rule): + """A rule to get the land use of a building.""" + + def __init__(self, storage): + self.storage = storage + + async def evaluate(self, payload: dict) -> Iterator[Tuple[int]]: + building_id = payload["building_id"] + + async for land_use in self.storage.iter_results( + f"""SELECT land.class + FROM osm_building_relations AS building, osm_lands AS land + WHERE {building_id} = building.id + AND ST_INTERSECTS(land.geometry, building.geometry); + """ + ): + + yield land_use diff --git a/tests/test_get_building_land_use.py b/tests/test_get_building_land_use.py new file mode 100644 index 0000000000000000000000000000000000000000..f88da4d96ded975d4f99f78331afd7f37ee1c876 --- /dev/null +++ b/tests/test_get_building_land_use.py @@ -0,0 +1,33 @@ +#!/usr/bin/env python3 + +# Copyright (C) 2021: +# Helmholtz-Zentrum Potsdam Deutsches GeoForschungsZentrum GFZ +# +# This program is free software: you can redistribute it and/or modify it +# under the terms of the GNU Affero General Public License as published by +# the Free Software Foundation, either version 3 of the License, or (at +# your option) any later version. +# +# This program is distributed in the hope that it will be useful, but +# WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Affero +# General Public License for more details. +# +# You should have received a copy of the GNU Affero General Public License +# along with this program. If not, see http://www.gnu.org/licenses/. + +import pytest + +from rabotnikobm.rules.get_building_land_use import ( + GetBuildingLandUse, +) + + +@pytest.mark.requires_storage +@pytest.mark.asyncio +async def test_get_points_in_building(connected_storage): + rule = GetBuildingLandUse(storage=connected_storage) + + payload = {"building_id": 193799} + result = [result async for result in rule.evaluate(payload=payload)] + assert result == [("forest",)]