Skip to content
Snippets Groups Projects

Resolve "Add height from GHSL characteristics"

Merged Laurens Oostwegel requested to merge 18-add-height-from-ghsl-characteristics into main
All threads resolved!
5 files
+ 165
3
Compare changes
  • Side-by-side
  • Inline
Files
5
#!/usr/bin/env python3
# Copyright (c) 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/.
class GHSLCharacteristicsInformation:
def __call__(self, database, geometry_wkt, *args, **kwargs):
"""
Intersect the building geometry with the GHSL characteristics layer and return the type
with the largest overlap.
Args:
database (PostGISDatabase):
GHSL source database.
geometry_wkt (string):
The WKT representation of the geometry of the building.
Returns:
A dictionary with the GHSL characteristics type exhibiting the largest overlap with
the building.
"""
# 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_wkt}', 4326),0)
), True)
) AS area
"""
# Select the type with the largest overlap with the building.
sql_statement = f"""
SELECT type
FROM (
SELECT type, {get_area_function}
FROM ghsl_characteristics
WHERE geom && ST_GeomFromText('{geometry_wkt}', 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