Commit eb8e9327 authored by Danijel Schorlemmer's avatar Danijel Schorlemmer
Browse files

Initial test version creating exposure database. Vornoi cells in works

parent 1b3f5446
Pipeline #19072 failed with stage
in 1 minute and 3 seconds
import sqlite3
import csv
import os
COQUIMBO_LONGITUDE = [-71.5, -71.0]
COQUIMBO_LATITUDE = [-30.25, -29.25]
def start_sara_database(database_filepath):
# Create SQLite database for the exposure data
print("Database filepath: " + database_filepath)
conn = sqlite3.connect(database_filepath)
print("Database created")
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")
return conn
def create_sara_database(database_filepath):
# Create SQLite database for the exposure data
print("Database filepath: " + database_filepath)
conn = sqlite3.connect(database_filepath)
print("Database created")
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")
# Create LOCATIONS table
conn.execute('''CREATE TABLE LOCATIONS
(IDX INTEGER PRIMARY KEY AUTOINCREMENT,
NAME TEXT);''')
sql_statement = "SELECT AddGeometryColumn('LOCATIONS', 'geometry', 4326, 'POINT', 'XY');"
conn.execute(sql_statement)
print("Table LOCATIONS created")
# sql_statement = "CREATE INDEX coordinate_index ON LOCATION;"
# conn.execute(sql_statement)
# print("Index coordinate_index created")
conn.execute('''CREATE TABLE ASSETS
(LOCATION INTEGER,
TAXONOMY TEXT,
NUMBER REAL,
STRUCTURAL REAL,
NIGHT REAL);''')
print("Table ASSETS created")
sql_statement = "CREATE INDEX location_index ON ASSETS(LOCATION);"
conn.execute(sql_statement)
print("Index location_index created")
# # Create Voronoi table
# conn.execute("""CREATE TABLE Voronoi (id INTEGER NOT NULL PRIMARY KEY)""")
# conn.execute("""SELECT AddGeometryColumn('Voronoi', 'geometry', 4326, 'POLYGON', 'XY')""")
# conn.commit()
# print("Table Voronoi created")
return conn
def read_sara_exposure_into_database(conn, sara_exposure_filepath):
with open(sara_exposure_filepath) as csv_file:
csv_reader = csv.reader(csv_file, delimiter=',')
next(csv_reader) # Skip first line
last_longitude = 1000
last_latitude = 1000
location_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 \
(float(row[1]) < COQUIMBO_LATITUDE[0]) or (float(row[1]) > COQUIMBO_LATITUDE[1]):
continue
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)
conn.execute(sql_statement)
conn.commit()
print("Location created")
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")
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)'
cur.execute(sql)
# creating a POLYGON Geometry column
sql = "SELECT AddGeometryColumn('test_pg', "
sql += "'geom', 4326, 'POLYGON', 'XY')"
cur.execute(sql)
# 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)
# creating a POLYGON table
sql = 'CREATE TABLE Voronoi ('
sql += 'id INTEGER NOT NULL PRIMARY KEY)'
cur.execute(sql)
# creating a POLYGON Geometry column
sql = "SELECT AddGeometryColumn('Voronoi', "
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 polygons
# cursor.execute("""
# INSERT INTO Voronoi
# SELECT NULL, CastToMultiPolygon(ST_VoronojDiagram(ST_Collect(LOCATION),1,100,0.01))
# FROM LOCATIONS
# """)
conn.commit()
# Create SQLite database for the exposure data
# conn.execute("""
# INSERT INTO Voronoi
# SELECT NULL, CastToMultiPolygon(ST_VoronojDiagram(ST_Collect(geometry), 1, 100, 0.01))
# FROM LOCATIONS
# """)
# 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, geometry) 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 = '../skr/polygon_test.sqlite'
# Create SQLite database for the exposure data
print("Database filepath: " + database_filepath)
conn = sqlite3.connect(database_filepath)
print("Database created")
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()
#exit()
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(conn)
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