Commit 9b51bcc0 authored by Danijel Schorlemmer's avatar Danijel Schorlemmer
Browse files

Added a derived class of Database and added the table definitions to it

parent 96f5dcf8
Pipeline #19933 passed with stage
in 48 seconds
......@@ -30,9 +30,9 @@ class Database:
connection and cursor. Upon connection, the Spatialite extension is loaded.
Args:
database_filepath:
database_filepath (str):
File path for the Spatialite database file.
spatialite_filepath:
spatialite_filepath (str):
File path of the Spatialite extension.
Attributes:
......@@ -75,3 +75,87 @@ class Database:
logger.debug("Spatialite version: %s" % row[1])
self.cursor = self.connection.cursor()
class CoquimboDatabase(Database):
"""The CoquimboDatabase class represents a Spatialite database for the Coquimbo
exposure model. It is derived from the generic Database class.
Args:
database_filepath (str):
File path for the Spatialite database file.
spatialite_filepath (str):
File path of the Spatialite extension.
Attributes:
self.database_filepath (str): Spatialite database file path.
self.spatialite_filepath (str): File path to the Spatialite extension.
"""
def __init__(self, database_filepath, spatialite_filepath="mod_spatialite"):
Database.__init__(self, database_filepath, spatialite_filepath)
def create_tables(self):
"""
Creates all necessary tables in the database. These are:
Location : Stores the locations of the SARA exposure data
Asset : Stores the assets per location
Voronoi : Stores the Voronoi cells around each location
Buildings: Stores the OpenStreetMap buildings
Exposure : Stores the resulting exposure assets for each building
"""
# Create table Locations
sql_statement = "CREATE TABLE Locations ("
sql_statement += "idx INTEGER PRIMARY KEY AUTOINCREMENT, "
sql_statement += "occupancy TEXT, "
sql_statement += "id INTEGER, "
sql_statement += "name TEXT, "
sql_statement += "longitude REAL, "
sql_statement += "latitude REAL)"
self.connection.execute(sql_statement)
sql_statement = "SELECT AddGeometryColumn('Locations', 'geom', 4326, 'POINT', 'XY')"
self.connection.execute(sql_statement)
logger.debug("Table Locations created")
# Create table Asset
sql_statement = "CREATE TABLE Assets ("
sql_statement += "location_idx INTEGER, "
sql_statement += "taxonomy TEXT, "
sql_statement += "number REAL, "
sql_statement += "structural REAL, "
sql_statement += "night REAL)"
self.connection.execute(sql_statement)
logger.debug("Table Assets created")
# Create table Voronoi
sql_statement = "CREATE TABLE Voronoi ("
sql_statement += "location_idx INTEGER)"
self.connection.execute(sql_statement)
sql_statement = "SELECT AddGeometryColumn('Voronoi', 'geom', 4326, 'POLYGON', 'XY')"
self.connection.execute(sql_statement)
logger.debug("Table Voronoi created")
# Create table Buildings
sql_statement = "CREATE TABLE Buildings ("
sql_statement += "idx INTEGER PRIMARY KEY AUTOINCREMENT, "
sql_statement += "location INTEGER, "
sql_statement += "quadkey TEXT)"
self.connection.execute(sql_statement)
sql_statement = "SELECT AddGeometryColumn('Buildings', "
sql_statement += "'geom', 4326, 'MULTIPOLYGON', 'XY')"
self.connection.execute(sql_statement)
sql_statement = "SELECT AddGeometryColumn('Buildings', 'centroid', 4326, 'POINT', 'XY')"
self.connection.execute(sql_statement)
logger.debug("Table Buildings created")
# Create table Exposure
sql_statement = "CREATE TABLE Exposure ("
sql_statement += "idx INTEGER PRIMARY KEY AUTOINCREMENT, "
sql_statement += "building_idx INTEGER, "
sql_statement += "taxonomy TEXT, "
sql_statement += "number REAL, "
sql_statement += "structural REAL, "
sql_statement += "night REAL)"
self.connection.execute(sql_statement)
logger.debug("Table Exposure created")
......@@ -22,7 +22,7 @@ import sys
import argparse
import os
import sqlite3
from database import Database # pylint: disable=E0611,E0401
from database import CoquimboDatabase # pylint: disable=E0611,E0401
# Add a logger printing error, warning, info and debug messages to the screen
......@@ -61,7 +61,7 @@ def main():
database_filepath = args.database
spatialite_filepath = args.spatialite_extension
db = Database(database_filepath, spatialite_filepath)
db = CoquimboDatabase(database_filepath, spatialite_filepath)
if command == "create":
if os.path.exists(database_filepath):
......@@ -72,6 +72,7 @@ def main():
except sqlite3.OperationalError:
logger.warning("Spatialite extension cannot be loaded. Exiting ...")
exit()
db.create_tables()
else:
logger.warning("Command not recognized. Exiting ...")
......
Supports Markdown
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