Commit 080cb692 authored by Danijel Schorlemmer's avatar Danijel Schorlemmer
Browse files

Renamed `cursor` to `cur` to allow multiple cursors

parent f8fc80a1
Pipeline #43626 passed with stage
in 1 minute and 24 seconds
......@@ -48,7 +48,7 @@ class SpatialiteDatabase:
File path to the Spatialite extension.
connection (sqlite3.Connection):
Database connection
cursor (sqlite3.Cursor):
cur (sqlite3.Cursor):
Database cursor
"""
......@@ -57,7 +57,7 @@ class SpatialiteDatabase:
self.spatialite_filepath = spatialite_filepath
self.connection = None
self.cursor = None
self.cur = None
def connect(self, init_spatial_metadata=True):
"""
......@@ -108,7 +108,7 @@ class SpatialiteDatabase:
logger.debug("SQLite version: %s" % row[0])
logger.debug("Spatialite version: %s" % row[1])
self.cursor = self.connection.cursor()
self.cur = self.connection.cursor()
class ExposureDatabase(SpatialiteDatabase):
......@@ -300,7 +300,7 @@ class ExposureDatabase(SpatialiteDatabase):
osm_id,
)
try:
self.cursor.execute(sql_statement)
self.cur.execute(sql_statement)
except Exception as E:
logger.debug(E)
......@@ -330,8 +330,8 @@ class ExposureDatabase(SpatialiteDatabase):
sql_statement += "(osm_id IS NULL)"
else:
sql_statement += "(osm_id = %d)" % osm_id
self.cursor.execute(sql_statement)
entity_id = self.cursor.fetchone()[0]
self.cur.execute(sql_statement)
entity_id = self.cur.fetchone()[0]
return entity_id
def insert_asset(
......@@ -372,7 +372,7 @@ class ExposureDatabase(SpatialiteDatabase):
night,
occupancy,
)
self.cursor.execute(sql_statement)
self.cur.execute(sql_statement)
def insert_building(self, osm_id, quadkey, occupancy, geom):
"""
......@@ -399,7 +399,7 @@ class ExposureDatabase(SpatialiteDatabase):
occupancy,
geom,
)
self.cursor.execute(sql_statement)
self.cur.execute(sql_statement)
def insert_tile(self, quadkey, built_area_size, built_up_ratio, completeness, geom_wkt):
"""
......@@ -428,7 +428,7 @@ class ExposureDatabase(SpatialiteDatabase):
completeness,
geom,
)
self.cursor.execute(sql_statement)
self.cur.execute(sql_statement)
def insert_tile_from_quadkey(self, quadkey):
"""
......@@ -453,7 +453,7 @@ class ExposureDatabase(SpatialiteDatabase):
quadkey,
geom,
)
self.cursor.execute(sql_statement)
self.cur.execute(sql_statement)
def insert_taxonomy(self, taxonomy_string):
"""
......@@ -465,7 +465,7 @@ class ExposureDatabase(SpatialiteDatabase):
"""
sql_statement = "INSERT INTO Taxonomy (taxonomy_string) VALUES ('%s')" % taxonomy_string
self.cursor.execute(sql_statement)
self.cur.execute(sql_statement)
def insert_district(self, admin_id, admin_name):
"""
......@@ -481,7 +481,7 @@ class ExposureDatabase(SpatialiteDatabase):
sql_statement = "INSERT INTO District "
sql_statement += "(admin_id, admin_name) "
sql_statement += "VALUES ('%s', '%s')" % (admin_id, admin_name)
self.cursor.execute(sql_statement)
self.cur.execute(sql_statement)
def insert_damage_assessment(self, entity_id, method_id, date=None, comment=""):
"""
......@@ -509,8 +509,8 @@ class ExposureDatabase(SpatialiteDatabase):
sql_statement += "(entity_id, method_id, date, comment) "
sql_statement += "VALUES (%d, %d, %d, '%s') " % (entity_id, method_id, date, comment)
sql_statement += "RETURNING id"
self.cursor.execute(sql_statement)
return self.cursor.fetchone()[0]
self.cur.execute(sql_statement)
return self.cur.fetchone()[0]
def insert_damage(self, assessment_id, damage_state_id, damage_probability):
"""
......@@ -532,7 +532,7 @@ class ExposureDatabase(SpatialiteDatabase):
damage_state_id,
damage_probability,
)
self.cursor.execute(sql_statement)
self.cur.execute(sql_statement)
def insert_damage_state(self, damage_scale_name, damage_state_name):
"""
......@@ -550,7 +550,7 @@ class ExposureDatabase(SpatialiteDatabase):
% (damage_scale_name, damage_state_name)
)
try:
self.cursor.execute(sql_statement)
self.cur.execute(sql_statement)
except Exception as E:
logger.debug(E.__class__)
return self.get_damage_state_id(damage_scale_name, damage_state_name)
......@@ -572,8 +572,8 @@ class ExposureDatabase(SpatialiteDatabase):
sql_statement = "SELECT id FROM DamageState "
sql_statement += "WHERE (damage_scale_name = '%s') " % damage_scale_name
sql_statement += "AND (damage_state_name = '%s')" % damage_state_name
self.cursor.execute(sql_statement)
result = self.cursor.fetchone()
self.cur.execute(sql_statement)
result = self.cur.fetchone()
if not result:
logger.debug(
"Dataset for %s, %s in DamageState does not exist"
......@@ -594,7 +594,7 @@ class ExposureDatabase(SpatialiteDatabase):
sql_statement = "INSERT INTO DamageMethod (name) VALUES ('%s')" % name
try:
self.cursor.execute(sql_statement)
self.cur.execute(sql_statement)
except Exception as E:
logger.debug(E.__class__)
return self.get_damage_method_id(name)
......@@ -612,8 +612,8 @@ class ExposureDatabase(SpatialiteDatabase):
"""
sql_statement = "SELECT id FROM DamageMethod WHERE name = '%s'" % name
self.cursor.execute(sql_statement)
result = self.cursor.fetchone()
self.cur.execute(sql_statement)
result = self.cur.fetchone()
if not result:
logger.debug("Dataset for %s in DamageMethod does not exist" % name)
return None
......@@ -664,8 +664,8 @@ class ExposureDatabase(SpatialiteDatabase):
"""
sql_statement = "SELECT quadkey, AsText(geom) FROM Building WHERE osm_id = %d" % osm_id
self.cursor.execute(sql_statement)
return self.cursor.fetchone()
self.cur.execute(sql_statement)
return self.cur.fetchone()
def quadkey_exists(self, quadkey):
"""
......@@ -680,8 +680,8 @@ class ExposureDatabase(SpatialiteDatabase):
"""
sql_statement = "SELECT EXISTS (SELECT 1 FROM Tile WHERE quadkey = '%s')" % quadkey
self.cursor.execute(sql_statement)
return self.cursor.fetchone()[0]
self.cur.execute(sql_statement)
return self.cur.fetchone()[0]
def taxonomy_string_exists(self, taxonomy_string):
"""
......@@ -699,8 +699,8 @@ class ExposureDatabase(SpatialiteDatabase):
"SELECT EXISTS (SELECT 1 FROM Taxonomy WHERE taxonomy_string = '%s')"
% taxonomy_string
)
self.cursor.execute(sql_statement)
return self.cursor.fetchone()[0]
self.cur.execute(sql_statement)
return self.cur.fetchone()[0]
def get_taxonomy_id(self, taxonomy_string):
"""
......@@ -715,8 +715,8 @@ class ExposureDatabase(SpatialiteDatabase):
"""
sql_statement = "SELECT id FROM Taxonomy WHERE taxonomy_string = '%s'" % taxonomy_string
self.cursor.execute(sql_statement)
return self.cursor.fetchone()[0]
self.cur.execute(sql_statement)
return self.cur.fetchone()[0]
def district_exists(self, admin_id):
"""
......@@ -733,8 +733,8 @@ class ExposureDatabase(SpatialiteDatabase):
sql_statement = (
"SELECT EXISTS (SELECT 1 FROM District WHERE admin_id = '%s')" % admin_id
)
self.cursor.execute(sql_statement)
return self.cursor.fetchone()[0]
self.cur.execute(sql_statement)
return self.cur.fetchone()[0]
def get_district_id(self, admin_id):
"""
......@@ -749,8 +749,8 @@ class ExposureDatabase(SpatialiteDatabase):
"""
sql_statement = "SELECT id FROM District WHERE admin_id = '%s'" % admin_id
self.cursor.execute(sql_statement)
return self.cursor.fetchone()[0]
self.cur.execute(sql_statement)
return self.cur.fetchone()[0]
def import_buildings_from_gpkg(
self, gpkg_filepath, table_name="buildings", osm_id_field_name="osm_id"
......@@ -781,8 +781,8 @@ class ExposureDatabase(SpatialiteDatabase):
sql_statement = "SELECT %s, ST_AsText(geom), " % osm_id_field_name
sql_statement += "X(ST_Centroid(geom)), Y(ST_Centroid(geom))"
sql_statement += "FROM BuildingDatabase.%s" % table_name
self.cursor.execute(sql_statement)
buildings = self.cursor.fetchall()
self.cur.execute(sql_statement)
buildings = self.cur.fetchall()
for building in buildings:
quadkey = Tile.for_latitude_longitude(
longitude=building[2], latitude=building[3], zoom=18
......@@ -965,10 +965,10 @@ class ExposureDatabase(SpatialiteDatabase):
sql_statement += "INNER JOIN District ON Entity.district_id = District.id "
sql_statement += "INNER JOIN Building ON Entity.osm_id = Building.osm_id "
sql_statement += "WHERE Entity.osm_id IS NOT NULL"
self.cursor.execute(sql_statement)
self.cur.execute(sql_statement)
line_number = 0
for asset in self.cursor:
for asset in self.cur:
writer.writerow(asset[0:14])
line_number += 1
if line_number % 100000 == 0:
......@@ -995,9 +995,9 @@ class ExposureDatabase(SpatialiteDatabase):
sql_statement += "INNER JOIN Taxonomy ON Asset.taxonomy_id = Taxonomy.id "
sql_statement += "INNER JOIN District ON Entity.district_id = District.id "
sql_statement += "WHERE Entity.osm_id IS NULL"
self.cursor.execute(sql_statement)
self.cur.execute(sql_statement)
for asset in self.cursor:
for asset in self.cur:
write_asset = list(asset[0:12]) + ["-1", "POINT EMPTY"]
writer.writerow(write_asset)
line_number += 1
......
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