Using the loss-calculator
For this tutorial you need to install matplotlib, folium, mapclassify and geopandas. Also the software packages losscalculator and databaselibfrom GFZ are needed.
In [6]:
Copied!
from exposurelib import SpatiaLiteExposure
from shapely import from_wkt
import geopandas
from exposurelib import SpatiaLiteExposure
from shapely import from_wkt
import geopandas
In [11]:
Copied!
# Open a new database connection
db = SpatiaLiteExposure('data/losscalculator.db')
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()
# Open a new database connection
db = SpatiaLiteExposure('data/losscalculator.db')
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()
attach database
In [12]:
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-02-26 14:51:54.057518 DEBUG:root:Connecting to database at data/losscalculator.db DEBUG:root:Connection to database established DEBUG:root:SpatiaLite extension loaded DEBUG:root:SQLite version: 3.45.1 DEBUG:root:SpatiaLite version: 5.1.0 DEBUG:exposurelib.database:Dataset for losscalculator2 in `AssessmentSource` does not exist DEBUG:root:Entities within GMF hull selected DEBUG:exposurelib.database:Intensity type gmv_PGA does not exist. DEBUG:exposurelib.database:Intensity type gmv_SA(0.3) does not exist. DEBUG:exposurelib.database:Intensity type gmv_SA(0.6) does not exist. DEBUG:exposurelib.database:Intensity type gmv_SA(1.0) does not exist. INFO:root:Entity 1000 computed INFO:root:Entity 2000 computed INFO:root:Entity 3000 computed INFO:root:Entity 4000 computed DEBUG:exposurelib.database:View `view_damage_building_1` created DEBUG:exposurelib.database:Geometry entry for view `view_damage_building_1` created. INFO:root:Damage view view_damage_building_1 created DEBUG:exposurelib.database:View `view_damage_residual_1` created DEBUG:exposurelib.database:Geometry entry for view `view_damage_residual_1` created. INFO:root:Damage view view_damage_residual_1 created DEBUG:exposurelib.database:View `view_damage_tile_1` created DEBUG:exposurelib.database:Geometry entry for view `view_damage_tile_1` created. INFO:root:Damage view view_damage_tile_1 created INFO:root:Execution time: 0:00:30.487842
In [7]:
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" InitSpatiaMetaData() error:"table spatial_ref_sys already exists"
In [12]:
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)
In [34]:
Copied!
gdf = geopandas.GeoDataFrame(df_dict, crs='EPSG:4326')
gdf.explore('complete_damage')
gdf = geopandas.GeoDataFrame(df_dict, crs='EPSG:4326')
gdf.explore('complete_damage')
Out[34]:
Make this Notebook Trusted to load map: File -> Trust Notebook
In [20]:
Copied!
In [ ]:
Copied!