Skip to content
GitLab
Menu
Projects
Groups
Snippets
/
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
Menu
Open sidebar
Dynamic Exposure
Global Dynamic Exposure
exposure-japan
Commits
340199a4
Commit
340199a4
authored
Mar 23, 2021
by
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
Changes
3
Pipelines
2
Hide whitespace changes
Inline
Side-by-side
exposurejapan/database.py
View file @
340199a4
...
...
@@ -17,7 +17,7 @@
# along with this program. If not, see http://www.gnu.org/licenses/.
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.
Args:
database_filepath (str):
File path for the Spatialite database file.
spatialite_filepath (str):
File path of the Spatialite extension.
Attributes:
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.
Attributes:
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"
)
self
.
connection
.
enable_load_extension
(
True
)
sql_statement
=
"SELECT load_extension('%s');"
%
self
.
spatialite_filepath
self
.
connection
.
execute
(
sql_statement
)
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
)
Spatialite
Database
.
__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
.
c
reate_connection_and_cursor
(
)
boundary_db
=
Spatialite
Database
(
district_boundary_filepath
,
self
.
spatialite_filepath
)
boundary_db
.
c
onnect
(
init_spatial_metadata
=
False
)
sql_statement
=
"SELECT key_code_ward, "
sql_statement
+=
"CITY_NAME, "
...
...
exposurejapan/exposurejapan.py
View file @
340199a4
...
...
@@ -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/mod_spatialite.so.7"
)
try
:
db
.
c
reate_connection_and_cursor
()
db
.
c
onnect
()
except
sqlite3
.
OperationalError
:
logger
.
warning
(
"Spatialite extension cannot be loaded. Exiting ..."
)
exit
()
db
.
create_tables
()
db
.
read_districts_and_boundaries
(
"
../
data/estat_bound_municipal.gpkg"
)
db
.
import_exposure_data
(
"
../
data/e008_3e.xlsx"
)
db
.
read_districts_and_boundaries
(
"data/estat_bound_municipal.gpkg"
)
db
.
import_exposure_data
(
"data/e008_3e.xlsx"
)
# Leave the program
sys
.
exit
()
...
...
setup.py
View file @
340199a4
...
...
@@ -29,7 +29,10 @@ setup(
keywords
=
"exposuremodel, building"
,
author
=
"Helmholtz-Zentrum Potsdam Deutsches GeoForschungsZentrum GFZ"
,
license
=
"AGPLv3+"
,
install_requires
=
[
"pandas"
,
"numpy"
,
"scipy"
],
install_requires
=
[
"pandas"
,
"exposurelib@https://git.gfz-potsdam.de/dynamicexposure/globaldynamicexposure/exposure-lib/-/archive/master/exposure-lib-master.zip"
,
# noqa: E501
],
extras_require
=
{
"tests"
:
tests_require
,
"linters"
:
linters_require
},
packages
=
find_packages
(),
entry_points
=
{
"console_scripts"
:
[
"exposurejapan = exposurejapan.exposurejapan:main"
]},
...
...
Write
Preview
Supports
Markdown
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment