Skip to content
Snippets Groups Projects
Commit 08f9effb authored by Marius Kriegerowski's avatar Marius Kriegerowski
Browse files

Changed to return results from rules instead of async yielding

parent b0b010cd
No related branches found
No related tags found
2 merge requests!23Resolve "Rules must not be async iterators",!22Resolve "entrypoint broken and rules loggers behaving correctly"
Pipeline #27986 passed
......@@ -16,7 +16,7 @@
# 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 logging
from typing import Tuple, Iterator
from typing import Optional
from rabotnik import Rule
......@@ -25,21 +25,23 @@ logger = logging.getLogger(__name__)
class GetBuildingLandUse(Rule):
"""A rule to get the land use of a building."""
"""A rule to get the land use of a building. If more than one land
use is found, returns None."""
def __init__(self, storage):
self.storage = storage
async def evaluate(self, payload: dict) -> Iterator[Tuple[int]]:
async def evaluate(self, payload: dict) -> Optional[str]:
building_id = payload["building_id"]
logger.debug("evaluating %s", building_id)
async for land_use in self.storage.iter_results(
land_use = await self.storage.expect_one(
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
logger.info("Land use of %s: %s", building_id, land_use)
return land_use
......@@ -47,4 +47,9 @@ class GetFloorspace(Rule):
"""
floorspace = await self.storage.expect_one(query_floorspace)
logger.info("building %s floorspace: %s.", building_id, floorspace)
if floorspace is None:
return
await upsert(self.storage_target, building_id, floorspace)
......@@ -16,7 +16,6 @@
# 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 logging
from typing import Tuple, Iterator
from rabotnik import Rule
......@@ -29,10 +28,11 @@ class GetPointsInBuilding(Rule):
def __init__(self, storage):
self.storage = storage
async def evaluate(self, payload: dict) -> Iterator[Tuple[int]]:
async def evaluate(self, payload: dict) -> int:
building_id = payload["building_id"]
logger.debug("evaluating %s", building_id)
n_points_total = 0
async for n_points, _ in self.storage.iter_results(
f"""SELECT COUNT(ST_NPOINTS(spots.geometry)), buildings.osm_id
FROM OSM_SPOTS as spots, OSM_Building_polygons as buildings
......@@ -42,4 +42,6 @@ class GetPointsInBuilding(Rule):
"""
):
yield n_points
n_points_total += n_points
return n_points_total
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment