Commit 26166d51 authored by Danijel Schorlemmer's avatar Danijel Schorlemmer
Browse files

DB-problem test code

parent eb8e9327
Pipeline #19076 failed with stage
in 59 seconds
......@@ -2,7 +2,6 @@
import sqlite3
import csv
import os
COQUIMBO_LONGITUDE = [-71.5, -71.0]
COQUIMBO_LATITUDE = [-30.25, -29.25]
......@@ -120,7 +119,16 @@ def read_sara_exposure_into_database(conn, sara_exposure_filepath):
conn.commit()
print("Asset created")
def add_voronoi_cells(conn):
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")
cur = conn.cursor()
......@@ -152,9 +160,9 @@ def add_voronoi_cells(conn):
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
sql = 'CREATE TABLE Voronoi ('
......@@ -164,9 +172,6 @@ def add_voronoi_cells(conn):
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("""
......@@ -197,7 +202,7 @@ def add_voronoi_cells(conn):
# 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)
sql_statement = "INSERT INTO Voronoi (id, geom) VALUES (NULL, %s)" % (location)
print(sql_statement)
cur.execute(sql_statement)
conn.commit()
......@@ -207,78 +212,79 @@ def create_voronoi_polygons(exposure_database):
return 0
def polygon_test():
def polygon_test(database_filepath):
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)
with conn:
# 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")
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)
# 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)
sql_statement = "SELECT InitSpatialMetaData(1);"
conn.execute(sql_statement)
print("Spatialite loaded")
cur = conn.cursor()
print(sql)
# 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)
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()
# 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():
......@@ -288,7 +294,9 @@ 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(conn)
read_sara_exposure_into_database(conn, '../data/Exposure_Ind_Chile.csv')
# add_voronoi_cells('../skr/testdb.sqlite')
# polygon_test('../skr/testdb.sqlite')
# polygon_test('../skr/polygon_test.sqlite')
main()
import sqlite3
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")
# 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)
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")
with 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)
# 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 db_test(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)
# 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")
#
# with 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)
#
# # 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():
db_test('../skr/polygon_test.sqlite')
db_test('../skr/testdb.sqlite')
exit()
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