Skip to content
Snippets Groups Projects

Resolve "Change the processing of the `stories` rule to a `height` GEM Taxonomy tag"

13 files
+ 424
82
Compare changes
  • Side-by-side
  • Inline
Files
13
#!/usr/bin/env python3
# Copyright (C) 2023:
# 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/.
class GHSLCharacteristicsRule:
def __call__(self, database, geometry, *args, **kwargs):
"""
Intersect the building geometry with the GHSL characteristics layer and return the type
with the most overlap.
Args:
database (PostGISDatabase):
OSM Replication database.
geometry (string):
The WKT representation of the geometry of the building.
Returns:
A dictionary with the most overlapping GHSL characteristics type.
"""
# Get the total area of the intersection between a GHSL type and the building.
get_area_function = f"""
SUM(
ST_Area(
ST_Intersection(geom, ST_Buffer(ST_GeomFromText('{geometry}', 4326),0)
), True)
) AS area
"""
# Select the type that most overlaps the building.
sql_statement = f"""
SELECT type
FROM (
SELECT type, {get_area_function}
FROM ghsl_characteristics
WHERE geom && ST_GeomFromText('{geometry}', 4326)
GROUP BY type
) AS ghsl_area
ORDER BY area DESC
LIMIT 1
"""
database.cursor.execute(sql_statement)
result = database.cursor.fetchone()
if result:
return {"ghsl_characteristics_type": result[0]}
else:
return {"ghsl_characteristics_type": None}
Loading