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
gde-importer
Commits
2d82e39b
Commit
2d82e39b
authored
Feb 07, 2022
by
Cecilia Nievas
Browse files
Added feature to store the geometries of data units
parent
1644a340
Pipeline
#38670
passed with stage
in 3 minutes and 23 seconds
Changes
3
Pipelines
3
Hide whitespace changes
Inline
Side-by-side
gdeimporter/dataunit.py
View file @
2d82e39b
...
...
@@ -297,6 +297,8 @@ class DataUnit:
Total number of census people in the DataUnit.
cost_total (float):
Total replacement cost of buildings in the DataUnit.
geometry (PSQL geometry):
Geometry of the data unit.
aggregated_source_id (int):
ID of the source of the aggregated exposure model.
occupancy_case (str):
...
...
@@ -320,16 +322,16 @@ class DataUnit:
sql_commands
[
"update"
]
=
"UPDATE %s"
sql_commands
[
"update"
]
+=
" SET (exposure_entity, buildings_total, dwellings_total,"
sql_commands
[
"update"
]
+=
" people_census, cost_total)"
sql_commands
[
"update"
]
+=
" = ('%s','%s','%s','%s','%s')"
sql_commands
[
"update"
]
+=
" people_census, cost_total
, geometry
)"
sql_commands
[
"update"
]
+=
" = ('%s','%s','%s','%s','%s'
,'%s'
)"
sql_commands
[
"update"
]
+=
" WHERE (data_unit_id='%s' AND occupancy_case='%s'"
sql_commands
[
"update"
]
+=
" AND aggregated_source_id='%s');"
sql_commands
[
"insert"
]
=
"INSERT INTO"
sql_commands
[
"insert"
]
+=
" %s(data_unit_id, occupancy_case, aggregated_source_id,"
sql_commands
[
"insert"
]
+=
" exposure_entity, buildings_total, dwellings_total,"
sql_commands
[
"insert"
]
+=
" people_census, cost_total)"
sql_commands
[
"insert"
]
+=
" VALUES('%s','%s','%s','%s','%s','%s','%s','%s');"
sql_commands
[
"insert"
]
+=
" people_census, cost_total
, geometry
)"
sql_commands
[
"insert"
]
+=
" VALUES('%s','%s','%s','%s','%s','%s','%s','%s'
,'%s'
);"
db_gde_tiles
=
Database
(
**
db_data_units_config
)
db_gde_tiles
.
create_connection_and_cursor
()
...
...
@@ -355,6 +357,7 @@ class DataUnit:
str
(
self
.
total_dwellings
),
str
(
self
.
total_people
[
"Census"
]),
str
(
self
.
total_cost
[
"Total"
]),
self
.
geometry
,
data_unit_full_id
,
occupancy_case
,
str
(
aggregated_source_id
),
...
...
@@ -373,6 +376,7 @@ class DataUnit:
str
(
self
.
total_dwellings
),
str
(
self
.
total_people
[
"Census"
]),
str
(
self
.
total_cost
[
"Total"
]),
self
.
geometry
,
)
)
else
:
# More than one entries found, this is an error
...
...
tests/data/test_database_set_up.sql
View file @
2d82e39b
...
...
@@ -7,6 +7,9 @@ DROP TABLE IF EXISTS exposure_entities_population_time_distribution;
DROP
TABLE
IF
EXISTS
data_units_buildings
;
DROP
TYPE
IF
EXISTS
occupancycase
;
DROP
TYPE
IF
EXISTS
settlement
;
DROP
EXTENSION
IF
EXISTS
postgis
;
CREATE
EXTENSION
postgis
;
CREATE
TYPE
occupancycase
AS
ENUM
(
'residential'
,
'commercial'
,
'industrial'
);
CREATE
TYPE
settlement
AS
ENUM
(
'urban'
,
'rural'
,
'big_city'
,
'all'
);
...
...
@@ -92,6 +95,7 @@ CREATE TABLE data_units
dwellings_total
FLOAT
,
people_census
FLOAT
,
cost_total
FLOAT
,
geometry
GEOMETRY
,
PRIMARY
KEY
(
data_unit_id
,
occupancy_case
,
aggregated_source_id
)
);
...
...
tests/test_dataunit.py
View file @
2d82e39b
...
...
@@ -145,12 +145,19 @@ def test_DataUnit(test_db):
17
,
)
# Transform gometry from query output into a shapely geometry
query_result_geometry
=
shapely
.
wkb
.
loads
(
query_result
[
0
][
-
1
],
hex
=
True
)
assert
len
(
query_result
)
==
1
# one entry found in the database
assert
query_result
[
0
][
0
]
==
"ABC"
# exposure_entity
assert
round
(
query_result
[
0
][
1
],
1
)
==
round
(
total_buildings
[
i
],
1
)
assert
round
(
query_result
[
0
][
2
],
1
)
==
round
(
total_dwellings
[
i
],
1
)
assert
round
(
query_result
[
0
][
3
],
1
)
==
round
(
total_people
[
"Census"
],
1
)
assert
round
(
query_result
[
0
][
4
],
0
)
==
round
(
total_cost
[
"Total"
],
0
)
assert
round
(
query_result_geometry
.
bounds
[
0
],
2
)
==
round
(
geometries
[
i
].
bounds
[
0
],
2
)
assert
round
(
query_result_geometry
.
bounds
[
1
],
2
)
==
round
(
geometries
[
i
].
bounds
[
1
],
2
)
assert
round
(
query_result_geometry
.
bounds
[
2
],
2
)
==
round
(
geometries
[
i
].
bounds
[
2
],
2
)
assert
round
(
query_result_geometry
.
bounds
[
3
],
2
)
==
round
(
geometries
[
i
].
bounds
[
3
],
2
)
def
query_data_units
(
credentials
,
data_unit_id_full
,
occupancy_case
,
aggregated_source_id
):
...
...
@@ -193,11 +200,13 @@ def query_data_units(credentials, data_unit_id_full, occupancy_case, aggregated_
Total number of census people in the DataUnit.
cost_total (float):
Total replacement cost of buildings in the DataUnit.
geometry (str):
Geometry of the DataUnit, as a HEX string.
"""
sql_command
=
(
"SELECT exposure_entity, buildings_total, dwellings_total, people_census, cost_total"
" FROM data_units"
"SELECT exposure_entity, buildings_total, dwellings_total, people_census, cost_total
,
"
"
geometry
FROM data_units"
" WHERE (data_unit_id='%s' AND occupancy_case='%s' AND aggregated_source_id='%s');"
%
(
data_unit_id_full
,
occupancy_case
,
aggregated_source_id
)
)
...
...
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