Commit 8f14b17f authored by Danijel Schorlemmer's avatar Danijel Schorlemmer
Browse files

Output of tile geometries implemented

parent d97ef5c4
Pipeline #19124 failed with stage
in 1 minute and 25 seconds
......@@ -3,6 +3,7 @@
import sqlite3
import csv
from shapely import wkt
from shapely.geometry import box
import geopandas
from pygeotile.tile import Tile
import numpy
......@@ -191,6 +192,9 @@ def add_buildings(conn, building_filepath):
sql = "SELECT AddGeometryColumn('Buildings', "
sql += "'geom', 4326, 'MULTIPOLYGON', 'XY')"
cur.execute(sql)
sql = "SELECT AddGeometryColumn('Buildings', "
sql += "'centroid', 4326, 'POINT', 'XY')"
cur.execute(sql)
print("Table Buildings created")
buildings = geopandas.read_file(building_filepath)
......@@ -201,9 +205,9 @@ def add_buildings(conn, building_filepath):
polys = cur.fetchall()
try:
location_index = polys[0][0]
centroid = "POINT(%f %f)" % (row.geometry.centroid.x, row.geometry.centroid.y)
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()
......@@ -213,7 +217,7 @@ def add_buildings(conn, building_filepath):
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)
sql_statement = "INSERT INTO Buildings (loc_id, quadkey, geom, centroid) VALUES (%d, %s, %s)" % (location_index, quadkey, building_geometry, centroid)
print("SQL statement: " + sql_statement)
conn.execute(sql_statement)
conn.commit()
......@@ -224,14 +228,14 @@ def classify_buildings(conn):
# Create building classification table
cur = conn.cursor()
# Create the Buildings table
# sql = 'CREATE TABLE Exposure ('
# sql += 'building_id INTEGER, '
# sql += 'taxonomy TEXT, '
# sql += 'NUMBER REAL, '
# sql += 'STRUCTURAL REAL, '
# sql += 'NIGHT REAL);'
# cur.execute(sql)
# print("Table Exposure created")
sql = 'CREATE TABLE Exposure ('
sql += 'building_id INTEGER, '
sql += 'taxonomy TEXT, '
sql += 'NUMBER REAL, '
sql += 'STRUCTURAL REAL, '
sql += 'NIGHT REAL);'
cur.execute(sql)
print("Table Exposure created")
# Simple version
# Loop over all exposure-data locations (Voronoi cells)
......@@ -273,7 +277,30 @@ def classify_buildings(conn):
conn.commit()
def output_tiles(conn, tile_filepath):
with open(tile_filepath, mode='w') as tiles_file:
fieldnames = ['origin_id', 'geometry']
writer = csv.DictWriter(tiles_file, fieldnames=fieldnames)
writer.writeheader()
cur = conn.cursor()
sql_statement = "SELECT DISTINCT quadkey FROM Buildings;"
print(sql_statement)
cur.execute(sql_statement)
quadkeys = cur.fetchall()
for quadkey in quadkeys:
print(quadkey[0])
tile = Tile.from_quad_tree(quadkey[0])
print(tile)
print(tile.bounds[0].latitude)
tile_polygon = box(tile.bounds[0].longitude, tile.bounds[0].latitude, tile.bounds[1].longitude, tile.bounds[1].latitude)
writer.writerow({'origin_id': 'Cell_ ' + quadkey[0], 'geometry': tile_polygon.wkt})
def output_exposure(output_filepath):
return 0
def main():
# conn = create_sara_database('../skr/testdb.sqlite')
......@@ -281,8 +308,10 @@ def main():
# read_sara_exposure_into_database(conn, '../data/Exposure_Ind_Chile.csv')
# add_voronoi_cells(conn)
# add_buildings(conn, '../data/obm.building.small.gpkg')
classify_buildings(conn)
# classify_buildings(conn)
output_tiles(conn, '../skr/tiles.csv')
# output_exposure('../skr/exposure.csv')
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