Commit 340199a4 authored by Danijel Schorlemmer's avatar Danijel Schorlemmer
Browse files

Introducing the exposurelib for the elementary Spatialite database class

parent b5992faf
Pipeline #21650 passed with stage
in 1 minute and 30 seconds
......@@ -17,7 +17,7 @@
# along with this program. If not, see
import logging
import sqlite3
from exposurelib.database import SpatialiteDatabase
import pandas
......@@ -25,57 +25,7 @@ import pandas
logger = logging.getLogger(__name__)
class Database:
"""The Database class represents a Spatialite database. It manages the database
connection and cursor. Upon connection, the Spatialite extension is loaded.
database_filepath (str):
File path for the Spatialite database file.
spatialite_filepath (str):
File path of the Spatialite extension.
database_filepath (str): Spatialite database file path.
spatialite_filepath (str): File path to the Spatialite extension.
def __init__(self, database_filepath, spatialite_filepath="mod_spatialite"):
self.database_filepath = database_filepath
self.spatialite_filepath = spatialite_filepath
self.connection = None
self.cursor = None
def create_connection_and_cursor(self, init_spatial_metadata=True):
"""Create a database connection, loads the Spatialite extension and initializes it.
init_spatial_metadata (bool, optional):
If set, the InitSpatialMetadata function of Spatialite is invoked
# Create SQLite database for the exposure data
logger.debug("Connecting to database at %s" % self.database_filepath)
self.connection = sqlite3.connect(self.database_filepath)
logger.debug("Connection to database established")
sql_statement = "SELECT load_extension('%s');" % self.spatialite_filepath
if init_spatial_metadata:
self.connection.execute("SELECT InitSpatialMetaData(1);")
logger.debug("Spatialite extension loaded and initialized")
versions = self.connection.execute("SELECT sqlite_version(), spatialite_version()")
for row in versions:
logger.debug("SQLite version: %s" % row[0])
logger.debug("Spatialite version: %s" % row[1])
self.cursor = self.connection.cursor()
class JapanDatabase(Database):
class JapanDatabase(SpatialiteDatabase):
"""The JapanDatabase class represents a Spatialite database for the Japan
exposure model. It is derived from the generic Database class.
......@@ -91,12 +41,17 @@ class JapanDatabase(Database):
def __init__(self, database_filepath, spatialite_filepath="mod_spatialite"):
Database.__init__(self, database_filepath, spatialite_filepath)
SpatialiteDatabase.__init__(self, database_filepath, spatialite_filepath)
def create_tables(self):
Creates all necessary tables in the database. These are:
District : Stores the districts with their IDs, names and geometries
District: Stores the districts with their IDs, names and geometries
DwellingNumber: Stores the number of dwellings depending on building types,
construction material and number of stories for each district
BuildingType: Stores the different types of buildings
ConstructionMaterial: Stores the construction-material types
NumberStories: Stores the classifications of numbers of stories
# Create table District
......@@ -252,8 +207,8 @@ class JapanDatabase(Database):
File path to the boundary file
boundary_db = Database(district_boundary_filepath, self.spatialite_filepath)
boundary_db = SpatialiteDatabase(district_boundary_filepath, self.spatialite_filepath)
sql_statement = "SELECT key_code_ward, "
sql_statement += "CITY_NAME, "
......@@ -20,7 +20,7 @@
import logging
import sys
import sqlite3
from database import JapanDatabase # pylint: disable=E0611,E0401
from .database import JapanDatabase # pylint: disable=E0611,E0401
# Add a logger printing error, warning, info and debug messages to the screen
......@@ -37,13 +37,13 @@ def main():
db = JapanDatabase("test.sqlite", "/usr/lib64/")
except sqlite3.OperationalError:
logger.warning("Spatialite extension cannot be loaded. Exiting ...")
# Leave the program
......@@ -29,7 +29,10 @@ setup(
keywords="exposuremodel, building",
author="Helmholtz-Zentrum Potsdam Deutsches GeoForschungsZentrum GFZ",
install_requires=["pandas", "numpy", "scipy"],
"exposurelib@", # noqa: E501
extras_require={"tests": tests_require, "linters": linters_require},
entry_points={"console_scripts": ["exposurejapan = exposurejapan.exposurejapan:main"]},
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