Commit 5034af8d authored by Danijel Schorlemmer's avatar Danijel Schorlemmer
Browse files

Voronoi cells created and associated to SARA locations

parent bb3b135a
Pipeline #19097 failed with stage
in 1 minute and 18 seconds
......@@ -2,6 +2,7 @@
import sqlite3
import csv
from shapely import wkt
COQUIMBO_LONGITUDE = [-71.5, -71.0]
COQUIMBO_LATITUDE = [-30.25, -29.25]
......@@ -56,6 +57,8 @@ def create_sara_database(database_filepath):
NAME TEXT);''')
sql_statement = "SELECT AddGeometryColumn('LOCATIONS', 'geometry', 4326, 'POINT', 'XY');"
conn.execute(sql_statement)
sql_statement = "SELECT AddGeometryColumn('LOCATIONS', 'voronoi', 4326, 'POLYGON', 'XY');"
conn.execute(sql_statement)
print("Table LOCATIONS created")
......@@ -90,6 +93,7 @@ def read_sara_exposure_into_database(conn, sara_exposure_filepath):
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]) < COQUIMBO_LONGITUDE[0]) or (float(row[0]) > COQUIMBO_LONGITUDE[1]) or \
......@@ -98,11 +102,6 @@ def read_sara_exposure_into_database(conn, sara_exposure_filepath):
if not ((last_longitude == row[0]) and (last_latitude == row[1])):
# Create new location entry
location_count += 1
# inserting some POINTs
# please note well: SQLite is ACID and Transactional
# so (to get best performance) the whole insert cycle
# will be handled as a single TRANSACTION
location = "GeomFromText('POINT(%f %f)', 4326)" % (float(row[0]), float(row[1]))
sql_statement = "INSERT INTO LOCATIONS (geometry, NAME) VALUES (%s, '%s')" % (location, row[3])
print("SQL statement: " + sql_statement)
......@@ -112,59 +111,94 @@ def read_sara_exposure_into_database(conn, sara_exposure_filepath):
last_longitude = row[0]
last_latitude = row[1]
else:
sql_statement = 'INSERT INTO ASSETS (LOCATION, TAXONOMY, NUMBER, STRUCTURAL, NIGHT) VALUES (' \
+ str(location_count) + ', """' + row[5] + '""", ' + row[6] + ', ' + row[7] + ', ' + row[8] + ')'
print("SQL statement: " + sql_statement)
conn.execute(sql_statement)
conn.commit()
print("Asset created")
# sql_statement = 'INSERT INTO ASSETS (LOCATION, TAXONOMY, NUMBER, STRUCTURAL, NIGHT) VALUES (' \
# + str(location_count) + ', """' + row[5] + '""", ' + row[6] + ', ' + row[7] + ', ' + row[8] + ')'
# print("SQL statement: " + sql_statement)
# conn.execute(sql_statement)
# conn.commit()
# print("Asset created")
asset_count += 1
print("Number of assets: " + str(asset_count))
def add_voronoi_cells(database_filepath):
conn = sqlite3.connect(database_filepath)
conn.enable_load_extension(True)
sql_statement = "SELECT load_extension('/usr/lib64/mod_spatialite.so.7');"
conn.execute(sql_statement)
sql_statement = "SELECT InitSpatialMetaData(1);"
conn.execute(sql_statement)
print("Spatialite loaded")
def add_voronoi_cells(conn):
cur = conn.cursor()
# creating a POLYGON table
sql = 'CREATE TABLE test_pg ('
sql += 'id INTEGER NOT NULL PRIMARY KEY,'
sql += 'name TEXT NOT NULL)'
# Create the temporary Voronoi table
sql = 'CREATE TABLE Voronoi ('
sql += 'loc_id INTEGER)'
cur.execute(sql)
# creating a POLYGON Geometry column
sql = "SELECT AddGeometryColumn('test_pg', "
sql = "SELECT AddGeometryColumn('Voronoi', "
sql += "'geom', 4326, 'POLYGON', 'XY')"
cur.execute(sql)
print("Table Voronoi created")
cur.execute("""SELECT ST_AsText(ST_VoronojDiagram(ST_Collect(geometry))) FROM LOCATIONS""")
conn.commit()
diag = cur.fetchall()
multipolygon = diag[0][0]
print(multipolygon)
p1 = wkt.loads(multipolygon)
for polygon in p1: # same for multipolygon.geoms
print(polygon)
location = "GeomFromText('%s', 4326)" % polygon
sql_statement = "INSERT INTO Voronoi (geom) VALUES (%s)" % location
print("SQL statement: " + sql_statement)
conn.execute(sql_statement)
conn.commit()
# Loop over points in Locations
sql_statement = "SELECT idx, ST_AsText(geometry) FROM LOCATIONS;"
print(sql_statement)
cur.execute(sql_statement)
rows = cur.fetchall()
for row in rows:
print(" ")
print("Point")
print(row)
location_index = row[0]
print("Location index: " + str(location_index))
print("Point coordinates: " + str(row[1]))
sql_statement = "SELECT ST_AsText(geom) as voro FROM voronoi where within(GeomFromText('%s', 4326), voronoi.geom);" % row[1]
print(sql_statement)
cur.execute(sql_statement)
polys = cur.fetchall()
print(polys[0][0])
# Add Voronoi polygon to LOCATIONS table
voronoicell = "GeomFromText('%s', 4326)" % polys[0][0]
sql_statement = "UPDATE Voronoi SET loc_id = %d WHERE geom = GeomFromText('%s', 4326);" % (location_index, polys[0][0])
# sql_statement = "UPDATE LOCATIONS SET voronoi = %s WHERE geometry = GeomFromText('%s', 4326);" % (voronoicell, row[0])
print(sql_statement)
cur.execute(sql_statement)
# inserting some POLYGONs
for i in range(100):
name = "test POLYGON #%d" % (i+1)
geom = "GeomFromText('POLYGON(("
geom += "%f " % (-10.0 - (i / 1000.0))
geom += "%f, " % (-10.0 - (i / 1000.0))
geom += "%f " % (10.0 + (i / 1000.0))
geom += "%f, " % (-10.0 - (i / 1000.0))
geom += "%f " % (10.0 + (i / 1000.0))
geom += "%f, " % (10.0 + (i / 1000.0))
geom += "%f " % (-10.0 - (i / 1000.0))
geom += "%f, " % (10.0 + (i / 1000.0))
geom += "%f " % (-10.0 - (i / 1000.0))
geom += "%f" % (-10.0 - (i / 1000.0))
geom += "))', 4326)"
sql = "INSERT INTO test_pg (id, name, geom) "
sql += "VALUES (%d, '%s', %s)" % (i+1, name, geom)
print(sql)
cur.execute(sql)
conn.commit()
# creating a POLYGON table
exit()
# # Loop over points in Locations
# sql_statement = "SELECT ST_AsText(geometry) FROM LOCATIONS;"
# print(sql_statement)
# cur.execute(sql_statement)
# rows = cur.fetchall()
# for row in rows:
# print(" ")
# print("Point: " + row[0])
# sql_statement = "SELECT ST_AsText(geom) as voro FROM voronoi where within(GeomFromText('%s', 4326), voronoi.geom);" % row[0]
# print(sql_statement)
# cur.execute(sql_statement)
# polys = cur.fetchall()
# print(polys[0][0])
# # Add Voronoi polygon to LOCATIONS table
# voronoicell = "GeomFromText('%s', 4326)" % polys[0][0]
# sql_statement = "UPDATE LOCATIONS SET voronoi = %s WHERE geometry = GeomFromText('%s', 4326);" % (voronoicell, row[0])
# print(sql_statement)
# cur.execute(sql_statement)
#
# conn.commit()
sql = 'CREATE TABLE Voronoi ('
sql += 'id INTEGER NOT NULL PRIMARY KEY)'
cur.execute(sql)
......@@ -174,12 +208,29 @@ def add_voronoi_cells(database_filepath):
cur.execute(sql)
print("Table Voronoi created")
# inserting polygons
# cursor.execute("""
# cur.execute("""
# INSERT INTO Voronoi
# SELECT NULL, CastToMultiPolygon(ST_VoronojDiagram(ST_Collect(LOCATION),1,100,0.01))
# SELECT NULL, ST_VoronojDiagram(ST_Collect(geometry), 1, 100, 0.01)
# FROM LOCATIONS
# """)
conn.commit()
# cur.execute("SELECT ST_AsText(ST_Collect(geometry)) FROM LOCATIONS")
# rows = cur.fetchall()
# for row in rows:
# print(row)
cur.execute("SELECT ST_AsText(ST_VoronojDiagram('MULTIPOINT(-71.3217 -30.2094, -71.3217 -29.3094, -71.0217 -30.2094, -71.0217 -29.9094, -71.0217 -29.6094, -71.0217 -29.3094)',1,100,0.01)) FROM LOCATIONS")
rows = cur.fetchall()
for row in rows:
print(row)
# cur.execute("""
# INSERT INTO Voronoi
# SELECT NULL, CastToMultiPolygon(ST_VoronojDiagram(ST_Collect(geometry),1,100,0.01))
# FROM LOCATIONS
# """)
# conn.commit()
# Create SQLite database for the exposure data
......@@ -191,101 +242,27 @@ def add_voronoi_cells(database_filepath):
# """)
# conn.commit()
#
# #getting voronoi polygons geometry as text
# cursor.execute("""SELECT AsText(CastToMultiPolygon(ST_VoronojDiagram(ST_Collect(geometry), 1, 100, 0.01))) FROM LOCATIONS""")
# conn.commit()
# diag = cursor.fetchall()
# print(diag)
cur = conn.cursor()
# location = "GeomFromText('MULTIPOLYGON((-71.4 -29.7, -71 -29.4, 3.5 -29, 3.5 -29.7, -71.4 -29.7), (-71.1 -29.4, -71.4 -29.7, -75.0 -29.7, -75.0 3.5, -71.1 3.5, -71.1 -29.4))', 4326)"
location = "GeomFromText('POLYGON((-71.4 -29.7, -71 -29.4, 3.5 -29, 3.5 -29.7, -71.4 -29.7))', 4326)"
sql_statement = "INSERT INTO Voronoi (id, geom) VALUES (NULL, %s)" % (location)
print(sql_statement)
cur.execute(sql_statement)
#getting voronoi polygons geometry as text
cur.execute("""SELECT AsText(CastToMultiPolygon(ST_VoronojDiagram(ST_Collect(geometry), 1, 100, 0.01))) FROM LOCATIONS""")
conn.commit()
diag = cur.fetchall()
print(diag)
# cur = conn.cursor()
#
# # location = "GeomFromText('MULTIPOLYGON((-71.4 -29.7, -71 -29.4, 3.5 -29, 3.5 -29.7, -71.4 -29.7), (-71.1 -29.4, -71.4 -29.7, -75.0 -29.7, -75.0 3.5, -71.1 3.5, -71.1 -29.4))', 4326)"
# location = "GeomFromText('POLYGON((-71.4 -29.7, -71 -29.4, 3.5 -29, 3.5 -29.7, -71.4 -29.7))', 4326)"
#
# sql_statement = "INSERT INTO Voronoi (id, geom) VALUES (NULL, %s)" % (location)
# print(sql_statement)
# cur.execute(sql_statement)
# conn.commit()
def create_voronoi_polygons(exposure_database):
return 0
def polygon_test(database_filepath):
# Create SQLite database for the exposure data
print("Database filepath: " + database_filepath)
conn = sqlite3.connect(database_filepath)
print("Database created")
with conn:
conn.enable_load_extension(True)
sql_statement = "SELECT load_extension('/usr/lib64/mod_spatialite.so.7');"
conn.execute(sql_statement)
# For debug
rs = conn.execute('SELECT sqlite_version(), spatialite_version()')
for row in rs:
msg = "> SQLite v%s Spatialite v%s" % (row[0], row[1])
print(msg)
sql_statement = "SELECT InitSpatialMetaData(1);"
conn.execute(sql_statement)
print("Spatialite loaded")
cur = conn.cursor()
# creating a POLYGON table
sql = 'CREATE TABLE test_pg ('
sql += 'id INTEGER NOT NULL PRIMARY KEY,'
sql += 'name TEXT NOT NULL)'
cur.execute(sql)
# creating a POLYGON Geometry column
sql = "SELECT AddGeometryColumn('test_pg', "
sql += "'geom', 4326, 'POLYGON', 'XY')"
cur.execute(sql)
# Create Voronoi table
cur.execute("""CREATE TABLE Voronoi (id INTEGER NOT NULL PRIMARY KEY)""")
cur.execute("""SELECT AddGeometryColumn('Voronoi', 'geometry', 4326, 'POLYGON', 'XY')""")
print("Table Voronoi created")
# inserting some POLYGONs
for i in range(100):
name = "test POLYGON #%d" % (i+1)
geom = "GeomFromText('POLYGON(("
geom += "%f " % (-10.0 - (i / 1000.0))
geom += "%f, " % (-10.0 - (i / 1000.0))
geom += "%f " % (10.0 + (i / 1000.0))
geom += "%f, " % (-10.0 - (i / 1000.0))
geom += "%f " % (10.0 + (i / 1000.0))
geom += "%f, " % (10.0 + (i / 1000.0))
geom += "%f " % (-10.0 - (i / 1000.0))
geom += "%f, " % (10.0 + (i / 1000.0))
geom += "%f " % (-10.0 - (i / 1000.0))
geom += "%f" % (-10.0 - (i / 1000.0))
geom += "))', 4326)"
sql = "INSERT INTO test_pg (id, name, geom) "
sql += "VALUES (%d, '%s', %s)" % (i+1, name, geom)
print(sql)
cur.execute(sql)
conn.commit()
location = "GeomFromText('POLYGON((-71.4 -29.7, -71 -29.4, 3.5 -29, 3.5 -29.7, -71.4 -29.7))', 4326)"
sql_statement = "INSERT INTO test_pg (id, name, geom) VALUES (NULL, 'Hello', %s)" % (location)
print(sql_statement)
cur.execute(sql_statement)
sql_statement = "INSERT INTO Voronoi (id, geometry) VALUES (NULL, %s)" % (location)
print(sql_statement)
cur.execute(sql_statement)
conn.commit()
def main():
# polygon_test()
......@@ -294,8 +271,8 @@ def main():
conn = create_sara_database('../skr/testdb.sqlite')
# conn = start_sara_database('../skr/testdb.sqlite')
read_sara_exposure_into_database(conn, '../data/Exposure_Ind_Chile.csv')
# add_voronoi_cells('../skr/testdb.sqlite')
read_sara_exposure_into_database(conn, '../data/Exposure_Res_Chile.csv')
add_voronoi_cells(conn)
# polygon_test('../skr/testdb.sqlite')
# polygon_test('../skr/polygon_test.sqlite')
......
......@@ -145,8 +145,8 @@ def db_test(database_filepath):
def main():
# polygon_test('../skr/polygon_test.sqlite')
polygon_test('../skr/testdb.sqlite')
polygon_test('../skr/polygon_test.sqlite')
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