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-coquimbo
Commits
1fd1da8f
Commit
1fd1da8f
authored
Mar 03, 2021
by
Danijel Schorlemmer
Browse files
Added the functions to read in the SARA data
parent
9b51bcc0
Pipeline
#20091
passed with stage
in 47 seconds
Changes
2
Pipelines
2
Hide whitespace changes
Inline
Side-by-side
exposurecoquimbo/database.py
View file @
1fd1da8f
...
...
@@ -19,6 +19,8 @@
import
logging
import
sqlite3
import
csv
from
collections
import
namedtuple
# Initialize log
...
...
@@ -95,6 +97,12 @@ class CoquimboDatabase(Database):
def
__init__
(
self
,
database_filepath
,
spatialite_filepath
=
"mod_spatialite"
):
Database
.
__init__
(
self
,
database_filepath
,
spatialite_filepath
)
# Coquimbo bounding box
BoundingBox
=
namedtuple
(
"BoundingBox"
,
[
"lon_min"
,
"lon_max"
,
"lat_min"
,
"lat_max"
])
self
.
coquimbo_bounding_box
=
BoundingBox
(
lon_min
=-
71.5
,
lon_max
=-
71.0
,
lat_min
=-
30.25
,
lat_max
=-
29.25
)
def
create_tables
(
self
):
"""
Creates all necessary tables in the database. These are:
...
...
@@ -110,9 +118,7 @@ class CoquimboDatabase(Database):
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)"
sql_statement
+=
"name TEXT)"
self
.
connection
.
execute
(
sql_statement
)
sql_statement
=
"SELECT AddGeometryColumn('Locations', 'geom', 4326, 'POINT', 'XY')"
self
.
connection
.
execute
(
sql_statement
)
...
...
@@ -159,3 +165,95 @@ class CoquimboDatabase(Database):
sql_statement
+=
"night REAL)"
self
.
connection
.
execute
(
sql_statement
)
logger
.
debug
(
"Table Exposure created"
)
def
insert_location
(
self
,
dataset
):
"""
Inserts a dataset in the Locations table.
Args:
dataset (tuple):
(occupancy,
point geometry in WKT,
location id,
location name)
Return:
Index of inserted dataset (Locations.idx)
"""
sql_statement
=
"INSERT INTO Locations "
sql_statement
+=
"(occupancy, geom, id, name) "
sql_statement
+=
"VALUES ('%s', %s, %s, '%s')"
%
dataset
self
.
cursor
.
execute
(
sql_statement
)
return
self
.
cursor
.
lastrowid
def
insert_asset
(
self
,
dataset
):
"""
Inserts a dataset in the Assets table.
Args:
dataset (tuple):
(location index corresponding to Locations.idx,
taxonomy,
number of buildings,
structural value of buildings (total value),
number of people inside buildings during night (total value))
"""
sql_statement
=
"INSERT INTO Assets "
sql_statement
+=
"(location_idx, taxonomy, number, structural, night) "
sql_statement
+=
"VALUES (%s, '%s', %s, %s, %s)"
%
dataset
self
.
cursor
.
execute
(
sql_statement
)
def
read_sara_exposure
(
self
,
exposure_filepath
):
"""
Reads a SARA exposure file into the tables Locations and Assets.
Args:
exposure_filepath (str):
File path to a SARA exposure file.
"""
with
open
(
exposure_filepath
)
as
csv_file
:
csv_reader
=
csv
.
reader
(
csv_file
,
delimiter
=
","
)
next
(
csv_reader
)
# Skip first line
# Impossible location to identify the first location as new one
last_longitude
=
1000
last_latitude
=
1000
location_count
=
0
asset_count
=
0
for
row
in
csv_reader
:
# Create location in database
if
(
(
float
(
row
[
0
])
<
self
.
coquimbo_bounding_box
.
lon_min
)
or
(
float
(
row
[
0
])
>
self
.
coquimbo_bounding_box
.
lon_max
)
or
(
float
(
row
[
1
])
<
self
.
coquimbo_bounding_box
.
lat_min
)
or
(
float
(
row
[
1
])
>
self
.
coquimbo_bounding_box
.
lat_max
)
):
continue
if
not
((
last_longitude
==
row
[
0
])
and
(
last_latitude
==
row
[
1
])):
# Create new location entry
location_count
+=
1
location
=
"GeomFromText('POINT(%s %s)', 4326)"
%
(
row
[
0
],
row
[
1
])
dataset
=
(
row
[
4
],
location
,
row
[
2
],
row
[
3
])
location_idx
=
self
.
insert_location
(
dataset
)
logger
.
debug
(
"Location %d created"
%
location_count
)
dataset
=
(
location_idx
,
row
[
5
],
row
[
6
],
row
[
7
],
row
[
8
])
self
.
insert_asset
(
dataset
)
asset_count
+=
1
logger
.
debug
(
"Asset %d for location %d created"
%
(
asset_count
,
location_count
)
)
last_longitude
=
row
[
0
]
last_latitude
=
row
[
1
]
else
:
# Add asset to current location
dataset
=
(
location_idx
,
row
[
5
],
row
[
6
],
row
[
7
],
row
[
8
])
self
.
insert_asset
(
dataset
)
asset_count
+=
1
logger
.
debug
(
"Asset %d for location %d created"
%
(
asset_count
,
location_count
)
)
self
.
connection
.
commit
()
logger
.
info
(
"Number of locations created: %d."
%
location_count
)
logger
.
info
(
"Number of assets created: %d."
%
asset_count
)
exposurecoquimbo/exposurecoquimbo.py
View file @
1fd1da8f
...
...
@@ -45,6 +45,13 @@ def main():
type
=
str
,
help
=
"File path of the database"
,
)
parser
.
add_argument
(
"-e"
,
"--exposure_filepath"
,
required
=
False
,
type
=
str
,
help
=
"File path of a SARA exposure-model file (any occupancy type)"
,
)
parser
.
add_argument
(
"-s"
,
"--spatialite_extension"
,
...
...
@@ -59,6 +66,7 @@ def main():
# Read arguments from command line
command
=
args
.
command
database_filepath
=
args
.
database
exposure_filepath
=
args
.
exposure_filepath
spatialite_filepath
=
args
.
spatialite_extension
db
=
CoquimboDatabase
(
database_filepath
,
spatialite_filepath
)
...
...
@@ -73,6 +81,7 @@ def main():
logger
.
warning
(
"Spatialite extension cannot be loaded. Exiting ..."
)
exit
()
db
.
create_tables
()
db
.
read_sara_exposure
(
exposure_filepath
)
else
:
logger
.
warning
(
"Command not recognized. Exiting ..."
)
...
...
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