Skip to content
Snippets Groups Projects

Resolve "Create a rule that gets the construction date from cadaster data for Tabula dataset."

Compare and Show latest version
4 files
+ 15
85
Compare changes
  • Side-by-side
  • Inline
Files
4
#!/usr/bin/env python3
# Copyright (c) 2023-2024:
# 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 rulelib import AbstractRule
class StoriesAndFloorspaceRule(AbstractRule):
def __call__(self, tags, relations, area, *args, **kwargs):
"""
Find the `building:levels` tag in the attributes of the building or one of the building
relations and save this as the number of stories. Calculate the floorspace of the
building, based on the footprint size of the building and the number of stories.
Args:
tags (dict):
Building tags, such as the building levels or building type.
relations (list):
List of the attributes of all relations that the building is member of.
area (float):
Footprint size of the building.
Returns:
A dictionary with the number of stories and the floorspace of the building.
"""
from math import ceil
all_building_tags = [tags] + [building["tags"] for building in relations]
for tags in all_building_tags:
story_string = self.get_story_tag(tags)
if story_string is None or story_string == "":
continue
try:
stories = ceil(float(story_string))
if stories < 1:
raise ValueError("Number of stories cannot be below 1")
elif stories > 175:
raise ValueError("Number of stories cannot be above 175")
floorspace = stories * area
return {"stories": stories, "floorspace": floorspace}
except ValueError:
continue
return {"stories": None, "floorspace": None}
@staticmethod
def get_story_tag(tags):
"""
Get the number of stories, if the attribute `building:levels` exist
Args:
tags:
Building tags, such as the building levels or building type.
Returns:
Number of stories, if the attribute `building:levels` exist, otherwise `None`.
"""
return tags.get("building:levels", None)
Loading