Commit 54bf8633 authored by Danijel Schorlemmer's avatar Danijel Schorlemmer
Browse files

Replaced the use of geopandas with Spatialite to read the buildings geopackage file

parent 45515949
Pipeline #20838 passed with stage
in 1 minute and 19 seconds
...@@ -20,10 +20,10 @@ ...@@ -20,10 +20,10 @@
import logging import logging
import sqlite3 import sqlite3
import csv import csv
import geopandas
import numpy import numpy
from collections import namedtuple from collections import namedtuple
from shapely import wkt from shapely import wkt
from shapely.affinity import translate
from pygeotile.tile import Tile from pygeotile.tile import Tile
# Initialize log # Initialize log
...@@ -397,38 +397,35 @@ class CoquimboDatabase(Database): ...@@ -397,38 +397,35 @@ class CoquimboDatabase(Database):
File path to a geopackage with OpenStreetMap buildings. File path to a geopackage with OpenStreetMap buildings.
""" """
buildings = geopandas.read_file(buildings_filepath) building_db = Database(buildings_filepath, self.spatialite_filepath)
for index, building in buildings.iterrows(): building_db.create_connection_and_cursor()
sql_statement = "SELECT AsWKT(CastAutomagic(geom)) FROM obm"
building_db.cursor.execute(sql_statement)
for building in building_db.cursor:
footprint = wkt.loads(building[0])
centroid = footprint.centroid
sql_statement = "SELECT location_id, geom FROM Voronoi WHERE " sql_statement = "SELECT location_id, geom FROM Voronoi WHERE "
sql_statement += "within(GeomFromText('%s', 4326), " % building.geometry.centroid sql_statement += "within(GeomFromText('%s', 4326), " % centroid.wkt
sql_statement += "voronoi.geom)" sql_statement += "voronoi.geom)"
self.cursor.execute(sql_statement) self.cursor.execute(sql_statement)
voronoi = self.cursor.fetchall() # TODO: fetchone()
try: try:
location_id = voronoi[0][0] location_id = self.cursor.fetchone()[0]
centroid = "POINT(%f %f)" % ( except TypeError:
building.geometry.centroid.x,
building.geometry.centroid.y,
)
except IndexError:
# If centroid lies on a boundary of the Voronoi cells, move the centroid # If centroid lies on a boundary of the Voronoi cells, move the centroid
centroid = "POINT(%f %f)" % ( centroid = translate(centroid, xoff=0.000001, yoff=0.000001)
building.geometry.centroid.x + 0.000001,
building.geometry.centroid.y + 0.000001,
)
sql_statement = "SELECT location_id, geom FROM Voronoi WHERE " sql_statement = "SELECT location_id, geom FROM Voronoi WHERE "
sql_statement += "within(GeomFromText('%s', 4326), " % centroid sql_statement += "within(GeomFromText('%s', 4326), " % centroid.wkt
sql_statement += "voronoi.geom)" sql_statement += "voronoi.geom)"
self.cursor.execute(sql_statement) self.cursor.execute(sql_statement)
voronoi = self.cursor.fetchall() # TODO: fetchone() location_id = self.cursor.fetchone()[0]
location_id = voronoi[0][0]
quadkey = Tile.for_latitude_longitude( quadkey = Tile.for_latitude_longitude(
longitude=float(building.geometry.centroid.x), longitude=float(centroid.coords[0][0]),
latitude=float(building.geometry.centroid.y), latitude=float(centroid.coords[0][1]),
zoom=18, zoom=18,
).quad_tree ).quad_tree
building_geometry = "GeomFromText('%s', 4326)" % building.geometry building_geometry = "GeomFromText('%s', 4326)" % footprint
centroid_geometry = "GeomFromText('%s', 4326)" % centroid centroid_geometry = "GeomFromText('%s', 4326)" % centroid
building_id = self.insert_building( building_id = self.insert_building(
location_id, quadkey, building_geometry, centroid_geometry location_id, quadkey, building_geometry, centroid_geometry
......
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