Skip to content
Snippets Groups Projects

Resolve "Add height from OSM `height` and `min_height` tags"

Merged Laurens Oostwegel requested to merge 16-add-height-from-osm-height-and-min_height-tags into main
2 files
+ 141
36
Compare changes
  • Side-by-side
  • Inline
Files
2
@@ -40,16 +40,14 @@ class HeightAndFloorspaceRule:
# If they are present, the floorspace of a building can be calculated. OpenStreetMap
# buildings can also contain a `height` tag but this tag is only parsed without the
# floorspace being estimated.
try:
stories, floorspace = self.get_stories_and_floorspace_from_osm(tags, area)
# OSM can have unexpected data types and unexpected content in their tagging scheme,
# therefore we do not raise an exception in case of a ValueError, but instead ignore
# the values.
except ValueError:
stories, floorspace = None, None
stories, floorspace = self.get_stories_and_floorspace_from_osm(tags, area)
if stories:
gem_taxonomy_height.append(stories)
height = self.get_height_from_osm(tags)
if height:
gem_taxonomy_height.append(height)
# If any of the number-of-stories and height tags are found in OpenStreetMap, they are
# returned.
if len(gem_taxonomy_height) > 0:
@@ -130,6 +128,36 @@ class HeightAndFloorspaceRule:
return "+".join(story_tags), floorspace
def get_height_from_osm(self, tags: dict):
"""
Get the height of a building, based on the `height` and `min_height` tags. The
information about parsing the tags comes from
https://wiki.openstreetmap.org/wiki/Key:building:levels.
Args:
tags (dict):
Building tags, such as the building levels or building type.
Returns:
A string formatted according to the `height` tag in the GEM Taxonomy.
"""
# Parse the height tag from OpenStreetMap.
height = self.tag_to_float(tags, "height")
if not height:
return None
# Check if there is a `min_height` tag.
min_height = self.tag_to_float(tags, "min_height")
if min_height:
height -= min_height
# Check if the height is higher than 0.
if height <= 0:
return None
return f"HHT:{height:.2f}"
def tag_to_float(self, tags: dict, tag_key: str):
"""
Try to parse a tag as a floating point value. If the value cannot be parsed, return
@@ -152,8 +180,9 @@ class HeightAndFloorspaceRule:
if tag_value is None or tag_value == "":
return None
# If the tag does exist, try to convert it to float. If that raises a `ValueError`,
# return None.
# OSM can have unexpected data types and unexpected content in their tagging scheme,
# therefore we do not raise an exception in case of a ValueError, but instead ignore
# the values.
try:
return float(tag_value)
except ValueError:
Loading