Commit be4666ed authored by Danijel Schorlemmer's avatar Danijel Schorlemmer
Browse files

Added the initial set of tables and insert functions

parent 6c8bc320
Pipeline #21595 passed with stage
in 48 seconds
......@@ -100,3 +100,228 @@ class SpatialiteDatabase:
logger.debug("Spatialite version: %s" % row[1])
self.cursor = self.connection.cursor()
class ExposureDatabase(SpatialiteDatabase):
"""
The ExposureDatabase class represents a Spatialite database for
exposure models. It is derived from the generic Database class.
Args:
database_filepath (str):
File path for the exposure database file.
spatialite_filepath (str):
File path of the Spatialite extension.
"""
def __init__(self, database_filepath, spatialite_filepath="mod_spatialite"):
SpatialiteDatabase.__init__(self, database_filepath, spatialite_filepath)
def create_tables(self):
"""
Creates all necessary tables in the database. These are:
BuildingAsset: Stores building-type assets
TileAsset : Stores tile-type assets
Building : Stores building geometries
Taxonomy : Stores taxonomy strings
District : Stores administrative districts
"""
# Create table BuildingAsset
sql_statement = "CREATE TABLE BuildingAsset ("
sql_statement += "id TEXT, "
sql_statement += "taxonomy_id INTEGER, "
sql_statement += "number REAL, "
sql_statement += "structural REAL, "
sql_statement += "night REAL, "
sql_statement += "occupancy TEXT, "
sql_statement += "district_id TEXT, "
sql_statement += "building_id INTEGER)"
self.connection.execute(sql_statement)
logger.debug("Table BuildingAsset created")
# Create table TileAsset
sql_statement = "CREATE TABLE TileAsset ("
sql_statement += "id TEXT, "
sql_statement += "taxonomy_id INTEGER, "
sql_statement += "number REAL, "
sql_statement += "structural REAL, "
sql_statement += "night REAL, "
sql_statement += "occupancy TEXT, "
sql_statement += "district_id TEXT, "
sql_statement += "quadkey INTEGER)"
self.connection.execute(sql_statement)
logger.debug("Table TileAsset created")
# Create table Building
sql_statement = "CREATE TABLE Building ("
sql_statement += "id INTEGER, "
sql_statement += "quadkey TEXT)"
self.connection.execute(sql_statement)
sql_statement = "SELECT AddGeometryColumn('Building', 'geom', 4326, "
sql_statement += "'MULTIPOLYGON', 'XY')"
self.connection.execute(sql_statement)
logger.debug("Table Building created")
# Create table Taxonomy
sql_statement = "CREATE TABLE Taxonomy ("
sql_statement += "id INTEGER, "
sql_statement += "taxonomy_string TEXT)"
self.connection.execute(sql_statement)
logger.debug("Table Taxonomy created")
# Create table District
sql_statement = "CREATE TABLE District ("
sql_statement += "id INTEGER, "
sql_statement += "admin_id TEXT, "
sql_statement += "admin_name TEXT)"
self.connection.execute(sql_statement)
logger.debug("Table District created")
def insert_building_asset(
self,
building_asset_id,
taxonomy_id,
number,
structural,
night,
occupancy,
district_id,
building_id,
):
"""
Inserts a building-type asset to the BuildingAsset table.
Args:
building_asset_id (str):
ID of the building asset
taxonomy_id (int):
ID of the taxonomy of the building asset (corresponds to Taxonomy.id)
number (float):
Number of buildings of the building asset
structural (float):
Structural value of the building asset
night (float):
Number of people in the buildings of this asset (sum)
occupancy (str):
Occupancy type of the building asset
district_id (int):
ID of the district (corresponds to District.id)
building_id (int):
ID of the building(corresponds to Building.id)
"""
sql_statement = "INSERT INTO BuildingAsset "
sql_statement += "(id, taxonomy_id, number, structural, night, "
sql_statement += "occupancy, district_id, building_id) "
sql_statement += "VALUES ('%s', %d, %f, %f, %f, '%s', %d, %d)" % (
building_asset_id,
taxonomy_id,
number,
structural,
night,
occupancy,
district_id,
building_id,
)
self.cursor.execute(sql_statement)
def insert_tile_asset(
self,
tile_asset_id,
taxonomy_id,
number,
structural,
night,
occupancy,
district_id,
quadkey,
):
"""
Inserts a tile-type asset to the TileAsset table.
Args:
tile_asset_id (str):
ID of the tile asset
taxonomy_id (int):
ID of the taxonomy of the tile asset (corresponds to Taxonomy.id)
number (float):
Number of buildings of the tile asset
structural (float):
Structural value of the tile asset (sum over all buildings in this asset)
night (float):
Number of people in the buildings of the tile asset (sum)
occupancy (str):
Occupancy type of the tile asset
district_id (int):
ID of the district (corresponds to District.id)
quadkey (str):
ID (Quadkey) of the tile the tile asset is located in
"""
sql_statement = "INSERT INTO TileAsset "
sql_statement += "(id, taxonomy_id, number, structural, night, "
sql_statement += "occupancy, district_id, quadkey) "
sql_statement += "VALUES ('%s', %d, %f, %f, %f, '%s', %d, '%s')" % (
tile_asset_id,
taxonomy_id,
number,
structural,
night,
occupancy,
district_id,
quadkey,
)
self.cursor.execute(sql_statement)
def insert_building(self, building_id, geom, quadkey):
"""
Inserts a building and its geometry to the Building table.
Args:
building_id (int):
ID of the building
geom (geom):
Geometry of the building (multi-polygon)
quadkey (str):
ID (Quadkey) of the tile the building centroid is located in
"""
sql_statement = "INSERT INTO Building "
sql_statement += "(id, geom, quadkey) "
sql_statement += "VALUES (%d, %s, '%s')" % (building_id, geom, quadkey)
self.cursor.execute(sql_statement)
def insert_taxonomy(self, taxonomy_id, taxonomy_string):
"""
Inserts a taxonomy ID and its respective description to the Taxonomy table.
Args:
taxonomy_id (int):
ID of the taxonomy item
taxonomy_string (str):
Taxonomy string following the GEM Taxonomy
"""
sql_statement = "INSERT INTO Taxonomy "
sql_statement += "(id, taxonomy_string) "
sql_statement += "VALUES (%d, '%s')" % (taxonomy_id, taxonomy_string)
self.cursor.execute(sql_statement)
def insert_district(self, district_id, admin_id, admin_name):
"""
Inserts a district ID and name to the District table.
Args:
district_id (int):
ID of the district dataset
admin_id (str):
Official ID of the district
admin_name (str):
Official Name of the district
"""
sql_statement = "INSERT INTO District "
sql_statement += "(id, admin_id, admin_name) "
sql_statement += "VALUES (%d, '%s', '%s')" % (district_id, admin_id, admin_name)
self.cursor.execute(sql_statement)
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment