Commit 24fce12c authored by Danijel Schorlemmer's avatar Danijel Schorlemmer
Browse files

Added the creation of Voronoi cells and their association to the respective exposure locations

parent 1fd1da8f
Pipeline #20192 passed with stage
in 54 seconds
...@@ -2,6 +2,11 @@ ...@@ -2,6 +2,11 @@
Create exposure model for Coquimbo, Chile. Create exposure model for Coquimbo, Chile.
## Requirements
* `python3`
* `shapely`
## Copyright and copyleft ## Copyright and copyleft
Copyright (C) 2020 Copyright (C) 2020
......
...@@ -21,7 +21,7 @@ import logging ...@@ -21,7 +21,7 @@ import logging
import sqlite3 import sqlite3
import csv import csv
from collections import namedtuple from collections import namedtuple
from shapely import wkt
# Initialize log # Initialize log
logger = logging.getLogger(__name__) logger = logging.getLogger(__name__)
...@@ -257,3 +257,45 @@ class CoquimboDatabase(Database): ...@@ -257,3 +257,45 @@ class CoquimboDatabase(Database):
self.connection.commit() self.connection.commit()
logger.info("Number of locations created: %d." % location_count) logger.info("Number of locations created: %d." % location_count)
logger.info("Number of assets created: %d." % asset_count) logger.info("Number of assets created: %d." % asset_count)
def add_voronoi_cells(self):
"""
Creates Voronoi cells around each of the locations (Locations.geom) and
associates them with their respective location via the Voronoi.location_idx field.
"""
# Create the multipolygon of all Voronoi cells
sql_statement = "SELECT ST_AsText(ST_VoronojDiagram(ST_Collect(geom))) FROM Locations"
self.cursor.execute(sql_statement)
self.connection.commit()
fetch_result = self.cursor.fetchall()
voronoi_multipolygon = fetch_result[0][0]
logger.debug("Voronoi cells computed.")
# Add each single Voronoi cell from the multipolygon to the Voronoi table
for polygon in wkt.loads(voronoi_multipolygon):
polygon_geometry = "GeomFromText('%s', 4326)" % polygon
sql_statement = "INSERT INTO Voronoi (geom) VALUES (%s)" % polygon_geometry
self.cursor.execute(sql_statement)
self.connection.commit()
logger.debug("Voronoi cells added to the Voronoi table.")
# Associate each Voronoi cell with its respective location
sql_statement = "SELECT idx, ST_AsText(geom) FROM Locations"
self.cursor.execute(sql_statement)
locations = self.cursor.fetchall()
for location in locations:
index = location[0]
geometry = location[1]
# Identify the Voronoi cell of the given location
sql_statement = "SELECT ST_AsText(geom) FROM Voronoi WHERE "
sql_statement += "within(GeomFromText('%s', 4326), Voronoi.geom)" % geometry
self.cursor.execute(sql_statement)
# Assign the Voronoi cell to its respective location
voronoi_cell = self.cursor.fetchall()
sql_statement = "UPDATE Voronoi SET location_idx = %d " % index
sql_statement += "WHERE geom = GeomFromText('%s', 4326)" % voronoi_cell[0][0]
self.cursor.execute(sql_statement)
logger.debug("Voronoi cell assigned to location %d." % index)
self.connection.commit()
...@@ -82,6 +82,7 @@ def main(): ...@@ -82,6 +82,7 @@ def main():
exit() exit()
db.create_tables() db.create_tables()
db.read_sara_exposure(exposure_filepath) db.read_sara_exposure(exposure_filepath)
db.add_voronoi_cells()
else: else:
logger.warning("Command not recognized. Exiting ...") logger.warning("Command not recognized. Exiting ...")
......
...@@ -10,6 +10,7 @@ setup( ...@@ -10,6 +10,7 @@ setup(
version="0.1", version="0.1",
description="Create exposure model for Coquimbo", description="Create exposure model for Coquimbo",
license="AGPLv3+", license="AGPLv3+",
install_requires=["shapely"],
extras_require={ extras_require={
"tests": tests_require, "tests": tests_require,
"linters": linters_require, "linters": linters_require,
......
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