Using the loss-calculator
For this tutorial you need to install the packages below. Also the software packages losscalculator and databaselibfrom GFZ are needed.
In [1]:
Copied!
# uncomment to install the packages
#!pip install matplotlib folium mapclassify geopandas
#!pip install databaselib exposurelib
#!pip install git+https://git.gfz-potsdam.de/globaldynamicexposure/loss-calculator@docs-patch
# uncomment to install the packages
#!pip install matplotlib folium mapclassify geopandas
#!pip install databaselib exposurelib
#!pip install git+https://git.gfz-potsdam.de/globaldynamicexposure/loss-calculator@docs-patch
In [2]:
Copied!
from exposurelib import SpatiaLiteExposure
from shapely import from_wkt
import geopandas
from exposurelib import SpatiaLiteExposure
from shapely import from_wkt
import geopandas
In [3]:
Copied!
db = SpatiaLiteExposure('data/losscalculator.db')
db = SpatiaLiteExposure('data/losscalculator.db')
In [4]:
Copied!
try:
db.connect()
db.create_tables()
print('attach database')
db.connection.executescript(
f"""
ATTACH DATABASE 'data/2023-011_Schorlemmer-et-al_ALB.Albania.db' AS source;
INSERT INTO Taxonomy SELECT * FROM source.Taxonomy;
INSERT INTO Entity SELECT * FROM source.Entity;
INSERT INTO Asset SELECT * FROM source.Asset
"""
)
db.connection.commit()
db.close()
except Exception as e:
print("Delete existing dataset if Integrity Error - InitSpatialMetaData()")
print(e)
try:
db.connect()
db.create_tables()
print('attach database')
db.connection.executescript(
f"""
ATTACH DATABASE 'data/2023-011_Schorlemmer-et-al_ALB.Albania.db' AS source;
INSERT INTO Taxonomy SELECT * FROM source.Taxonomy;
INSERT INTO Entity SELECT * FROM source.Entity;
INSERT INTO Asset SELECT * FROM source.Asset
"""
)
db.connection.commit()
db.close()
except Exception as e:
print("Delete existing dataset if Integrity Error - InitSpatialMetaData()")
print(e)
attach database
Calculate damage loss¶
In [5]:
Copied!
!damagecalculator damage \
-e data/losscalculator.db \
-f data/fragility_ESRM20_various_IM.xml \
-g data/ground_motion_field.csv \
-t data/esrm20_exposure_vulnerability_mapping.csv \
-S losscalculator
!damagecalculator damage \
-e data/losscalculator.db \
-f data/fragility_ESRM20_various_IM.xml \
-g data/ground_motion_field.csv \
-t data/esrm20_exposure_vulnerability_mapping.csv \
-S losscalculator
INFO:root:Start time: 2025-03-17 11:44:48.425698 INFO:root:Entity 1000 computed INFO:root:Entity 2000 computed INFO:root:Entity 3000 computed INFO:root:Entity 4000 computed INFO:root:Damage view view_damage_building_1 created INFO:root:Damage view view_damage_residual_1 created INFO:root:Damage view view_damage_tile_1 created INFO:root:Execution time: 0:00:22.452799
In [6]:
Copied!
db = SpatiaLiteExposure('data/losscalculator.db')
db.connect()
sql_query = f"""
SELECT
quadkey,
ST_AsText(geometry),
no_damage_damage AS no_damage,
slight_damage,
moderate_damage,
extensive_damage,
complete_damage
FROM view_damage_tile_1
"""
db.cursor.execute(sql_query)
res = db.cursor.fetchall()
description = db.cursor.description
df_dict = {
"quadkey": [],
"geometry": [],
"no_damage": [],
"slight_damage": [],
"moderate_damage": [],
"extensive_damage": [],
"complete_damage": []
}
db = SpatiaLiteExposure('data/losscalculator.db')
db.connect()
sql_query = f"""
SELECT
quadkey,
ST_AsText(geometry),
no_damage_damage AS no_damage,
slight_damage,
moderate_damage,
extensive_damage,
complete_damage
FROM view_damage_tile_1
"""
db.cursor.execute(sql_query)
res = db.cursor.fetchall()
description = db.cursor.description
df_dict = {
"quadkey": [],
"geometry": [],
"no_damage": [],
"slight_damage": [],
"moderate_damage": [],
"extensive_damage": [],
"complete_damage": []
}
InitSpatiaMetaData() error:"table spatial_ref_sys already exists"
In [7]:
Copied!
for quadkey, geom , nd, sd, md, ed, cd in res:
df_dict["quadkey"].append(quadkey)
df_dict["geometry"].append(from_wkt(geom))
df_dict["no_damage"].append(nd)
df_dict["slight_damage"].append(sd)
df_dict["moderate_damage"].append(md)
df_dict["extensive_damage"].append(ed)
df_dict["complete_damage"].append(cd)
for quadkey, geom , nd, sd, md, ed, cd in res:
df_dict["quadkey"].append(quadkey)
df_dict["geometry"].append(from_wkt(geom))
df_dict["no_damage"].append(nd)
df_dict["slight_damage"].append(sd)
df_dict["moderate_damage"].append(md)
df_dict["extensive_damage"].append(ed)
df_dict["complete_damage"].append(cd)
Check results at map¶
In [11]:
Copied!
gdf = geopandas.GeoDataFrame(df_dict, crs='EPSG:4326')
gdf.iloc[:, 2:] = gdf.iloc[:, 2:].round(2)
gdf.explore('slight_damage')
gdf = geopandas.GeoDataFrame(df_dict, crs='EPSG:4326')
gdf.iloc[:, 2:] = gdf.iloc[:, 2:].round(2)
gdf.explore('slight_damage')
Out[11]:
Make this Notebook Trusted to load map: File -> Trust Notebook
Get descriptive statistics¶
In [ ]:
Copied!
damage_columns = ["no_damage", "slight_damage", "moderate_damage", "extensive_damage", "complete_damage"]
damage_stats = gdf[damage_columns].describe().T
damage_stats["total"] = gdf[damage_columns].sum()
damage_stats
damage_columns = ["no_damage", "slight_damage", "moderate_damage", "extensive_damage", "complete_damage"]
damage_stats = gdf[damage_columns].describe().T
damage_stats["total"] = gdf[damage_columns].sum()
damage_stats
Out[ ]:
| count | mean | std | min | 25% | 50% | 75% | max | total | |
|---|---|---|---|---|---|---|---|---|---|
| no_damage | 4409.0 | 19.713218 | 15.310294 | 0.92 | 11.41 | 17.32 | 23.74 | 295.61 | 86915.58 |
| slight_damage | 4409.0 | 1.443178 | 1.122570 | 0.05 | 0.81 | 1.25 | 1.75 | 22.17 | 6362.97 |
| moderate_damage | 4409.0 | 0.207757 | 0.164187 | 0.01 | 0.11 | 0.18 | 0.25 | 3.08 | 916.00 |
| extensive_damage | 4409.0 | 0.060581 | 0.048772 | 0.00 | 0.03 | 0.05 | 0.07 | 0.87 | 267.10 |
| complete_damage | 4409.0 | 0.054695 | 0.044742 | 0.00 | 0.03 | 0.05 | 0.07 | 0.75 | 241.15 |