Skip to content
Snippets Groups Projects
Commit d97ef5c4 authored by Danijel Schorlemmer's avatar Danijel Schorlemmer
Browse files

Building classification and further exposure indicators are assigned to each...

Building classification and further exposure indicators are assigned to each building and written to the Exposure table
parent 3832336f
Branches
No related tags found
Loading
Pipeline #19121 failed
......@@ -5,6 +5,8 @@ import csv
from shapely import wkt
import geopandas
from pygeotile.tile import Tile
import numpy
COQUIMBO_LONGITUDE = [-71.5, -71.0]
COQUIMBO_LATITUDE = [-30.25, -29.25]
......@@ -59,8 +61,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)
# sql_statement = "SELECT AddGeometryColumn('LOCATIONS', 'voronoi', 4326, 'POLYGON', 'XY');"
# conn.execute(sql_statement)
print("Table LOCATIONS created")
......@@ -181,6 +183,7 @@ def add_buildings(conn, building_filepath):
cur = conn.cursor()
# Create the Buildings table
sql = 'CREATE TABLE Buildings ('
sql += 'IDX INTEGER PRIMARY KEY AUTOINCREMENT,'
sql += 'loc_id INTEGER,'
sql += 'quadkey TEXT);'
cur.execute(sql)
......@@ -216,12 +219,70 @@ def add_buildings(conn, building_filepath):
conn.commit()
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")
# Simple version
# Loop over all exposure-data locations (Voronoi cells)
sql_statement = "SELECT idx FROM Locations;"
print(sql_statement)
cur.execute(sql_statement)
rows = cur.fetchall()
for row in rows:
print("Location: " + str(row[0]))
sql_statement = "SELECT Taxonomy, Number, Structural, Night FROM Assets WHERE Location = %d" % row[0]
cur.execute(sql_statement)
assets = cur.fetchall()
for asset in assets:
print("Asset: %s, %f, %f, %f" % (asset[0], asset[1], asset[2], asset[3]))
taxonomies = numpy.array(assets)[:, 0]
print(taxonomies)
a = numpy.asarray(assets)[:, 1:].astype(numpy.float)
print(a)
sum = a.sum(axis=0)
print(sum)
proportions = a/sum[None, :]
print(proportions)
proportions[numpy.isnan(proportions)] = 0
print(proportions)
# Select all buildings in the Voronoi cell
sql_statement = "SELECT idx FROM Buildings WHERE loc_id = %d" % row[0]
print(sql_statement)
cur.execute(sql_statement)
buildings = cur.fetchall()
for building_index in buildings:
# Write exposure data for the building
print(building_index[0])
for counter in range(taxonomies.shape[0]):
print(taxonomies[counter])
print(proportions[counter,:])
sql_statement = "INSERT INTO Exposure (building_id, taxonomy, number, structural, night) VALUES (%d, %s, %f, %f, %f)" % (building_index[0], taxonomies[counter], proportions[counter,0], proportions[counter,0], proportions[counter,0])
cur.execute(sql_statement)
conn.commit()
def main():
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)
add_buildings(conn, '../data/obm.building.gpkg')
# 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)
# add_buildings(conn, '../data/obm.building.small.gpkg')
classify_buildings(conn)
main()
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment