Commit 3832336f authored by Danijel Schorlemmer's avatar Danijel Schorlemmer
Browse files

Buildings read into database, building centroids created and associated to...

Buildings read into database, building centroids created and associated to their respective Voronoi polygon
parent 5034af8d
Pipeline #19107 failed with stage
in 1 minute and 2 seconds
......@@ -3,6 +3,8 @@
import sqlite3
import csv
from shapely import wkt
import geopandas
from pygeotile.tile import Tile
COQUIMBO_LONGITUDE = [-71.5, -71.0]
COQUIMBO_LATITUDE = [-30.25, -29.25]
......@@ -111,12 +113,12 @@ 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))
......@@ -125,7 +127,7 @@ def add_voronoi_cells(conn):
cur = conn.cursor()
# Create the temporary Voronoi table
# Create the Voronoi table
sql = 'CREATE TABLE Voronoi ('
sql += 'loc_id INTEGER)'
cur.execute(sql)
......@@ -167,113 +169,59 @@ def add_voronoi_cells(conn):
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)
conn.commit()
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()
def add_buildings(conn, building_filepath):
sql = 'CREATE TABLE Voronoi ('
sql += 'id INTEGER NOT NULL PRIMARY KEY)'
cur = conn.cursor()
# Create the Buildings table
sql = 'CREATE TABLE Buildings ('
sql += 'loc_id INTEGER,'
sql += 'quadkey TEXT);'
cur.execute(sql)
# creating a POLYGON Geometry column
sql = "SELECT AddGeometryColumn('Voronoi', "
sql += "'geom', 4326, 'POLYGON', 'XY')"
sql = "SELECT AddGeometryColumn('Buildings', "
sql += "'geom', 4326, 'MULTIPOLYGON', 'XY')"
cur.execute(sql)
print("Table Voronoi created")
# inserting polygons
# cur.execute("""
# INSERT INTO Voronoi
# SELECT NULL, ST_VoronojDiagram(ST_Collect(geometry), 1, 100, 0.01)
# FROM LOCATIONS
# """)
# 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
print("Table Buildings created")
# 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
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
buildings = geopandas.read_file(building_filepath)
for index, row in buildings.iterrows(): # Looping over all points
sql_statement = "SELECT loc_id, geom FROM voronoi where within(GeomFromText('%s', 4326), voronoi.geom);" % row.geometry.centroid
# print(sql_statement)
cur.execute(sql_statement)
polys = cur.fetchall()
try:
location_index = polys[0][0]
except: # If centroid lies on the voronoi boundary
centroid = "POINT(%f %f)" % (row.geometry.centroid.x + 0.000001, row.geometry.centroid.y + 0.000001)
print(centroid)
sql_statement = "SELECT loc_id, geom FROM voronoi where within(GeomFromText('%s', 4326), voronoi.geom);" % centroid
cur.execute(sql_statement)
polys = cur.fetchall()
location_index = polys[0][0]
print(location_index)
quadkey = Tile.for_latitude_longitude(longitude=float(row.geometry.centroid.x), latitude=float(row.geometry.centroid.y), zoom=18).quad_tree
building_geometry = "GeomFromText('%s', 4326)" % (row.geometry)
sql_statement = "INSERT INTO Buildings (loc_id, quadkey, geom) VALUES (%d, %s, %s)" % (location_index, quadkey, building_geometry)
print("SQL statement: " + sql_statement)
conn.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_Res_Chile.csv')
add_voronoi_cells(conn)
# polygon_test('../skr/testdb.sqlite')
# polygon_test('../skr/polygon_test.sqlite')
add_buildings(conn, '../data/obm.building.gpkg')
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