Skip to content
Snippets Groups Projects

Resolve "[Bug] Program crashes when no built area is present within a district"

@@ -223,9 +223,12 @@ class ExposureInitializer:
# in subquery `SumQuery` to estimate the sum of the built-area and the cross-joined
# query retrieving the built-area size per tile to compute the built-area proportion.
if isinstance(self.exposure_db, SpatiaLiteExposure):
where_clause = "WHERE built_area_size IS NOT NULL"
where_clause = "WHERE built_area_size IS NOT NULL AND built_area_size > 0"
else:
where_clause = "WHERE A.geometry && B.border AND built_area_size IS NOT NULL"
where_clause = """
WHERE A.geometry && B.border AND built_area_size IS NOT NULL
AND built_area_size > 0
"""
sql_statement = f"""
WITH T AS
(
@@ -263,17 +266,15 @@ class ExposureInitializer:
# Add the respective assets by proportion of built area to `AssetReference`
for taxonomy_id, value in asset_dict.items():
sql_statement = """
sql_statement = f"""
INSERT INTO AssetReference
(entity_id, taxonomy_id, number, structural, night)
VALUES (%d, %d, %f, %f, %f)
""" % (
entity_id,
int(taxonomy_id), # taxonomy_id
float(value["number"]) * proportion, # number
float(value["structural"]) * proportion, # structural
float(value["night"]) * proportion, # night
)
VALUES ({entity_id},
{int(taxonomy_id)},
{float(value["number"]) * proportion},
{float(value["structural"]) * proportion},
{float(value["night"]) * proportion})
"""
self.exposure_db.cursor.execute(sql_statement)
self.exposure_db.connection.commit()
@@ -347,21 +348,19 @@ class ExposureInitializer:
sum_number += value["number"]
# Assign the country-average asset distribution to the country assets
for taxonomy_id, value in country_asset_dict.items():
sql_statement = """
sql_statement = f"""
INSERT INTO AssetCountry
(country_iso_code, taxonomy_id, number, number_normalized, structural,
structural_normalized, night, night_normalized)
VALUES ('%s', %d, %f, %f, %f, %f, %f, %f)
""" % (
country_iso_code,
int(taxonomy_id),
float(value["number"]),
float(value["number"]) / sum_number,
float(value["structural"]),
float(value["structural"]) / sum_number,
float(value["night"]),
float(value["night"]) / sum_number,
)
VALUES ('{country_iso_code}',
{int(taxonomy_id)},
{float(value["number"])},
{float(value["number"]) / sum_number},
{float(value["structural"])},
{float(value["structural"]) / sum_number},
{float(value["night"])},
{float(value["night"]) / sum_number})
"""
self.exposure_db.cursor.execute(sql_statement)
self.exposure_db.connection.commit()
@@ -408,6 +407,7 @@ class ExposureInitializer:
FROM {self.exposure_db.tile_view}
WHERE (country_iso_code = '{country_iso_code}')
AND (built_area_size IS NOT NULL)
AND (built_area_size > 0)
) AS A
LEFT JOIN EntityReference AS E
ON E.quadkey = A.quadkey
Loading