Commit b9bb84e6 authored by Danijel Schorlemmer's avatar Danijel Schorlemmer
Browse files

Released the restriction that all assets of one location need to be sequential in the SARA file

parent 2dfec5f9
Pipeline #20950 passed with stage
in 1 minute and 21 seconds
......@@ -118,7 +118,7 @@ class CoquimboDatabase(Database):
# Create table Location
sql_statement = "CREATE TABLE Location ("
sql_statement += "id INTEGER PRIMARY KEY AUTOINCREMENT, "
sql_statement += "id INTEGER PRIMARY KEY, "
sql_statement += "occupancy TEXT, "
sql_statement += "admin_id INTEGER, "
sql_statement += "admin_name TEXT)"
......@@ -170,11 +170,13 @@ class CoquimboDatabase(Database):
self.connection.execute(sql_statement)
logger.debug("Table BuildingAsset created")
def insert_location(self, occupancy, geom, admin_id, admin_name):
def insert_location(self, index, occupancy, geom, admin_id, admin_name):
"""
Inserts a dataset to the Location table.
Args:
index (int):
Primary index
occupancy (str):
Occupancy type of the SARA data for this location
geom (point):
......@@ -189,11 +191,17 @@ class CoquimboDatabase(Database):
"""
sql_statement = "INSERT INTO Location "
sql_statement += "(occupancy, geom, admin_id, admin_name) "
sql_statement += "VALUES ('%s', %s, %d, '%s')" % (occupancy, geom, admin_id, admin_name)
sql_statement += "(id, occupancy, geom, admin_id, admin_name) "
sql_statement += "VALUES (%d, '%s', %s, %d, '%s')" % (
index,
occupancy,
geom,
admin_id,
admin_name,
)
self.cursor.execute(sql_statement)
return self.cursor.lastrowid
# return self.cursor.lastrowid
def insert_asset(
self,
......@@ -300,16 +308,13 @@ class CoquimboDatabase(Database):
File path to a SARA exposure file.
"""
location_list = []
with open(exposure_filepath) as csv_file:
csv_reader = csv.reader(csv_file, delimiter=",")
next(csv_reader) # Skip first line
# Impossible location to identify the first location as new one
last_longitude = 1000
last_latitude = 1000
location_count = 0
asset_count = 0
for row in csv_reader:
# Create location in database
# Is the location within the Coquimbo bounding box
if (
(float(row[0]) < self.coquimbo_bounding_box.lon_min)
or (float(row[0]) > self.coquimbo_bounding_box.lon_max)
......@@ -317,14 +322,12 @@ class CoquimboDatabase(Database):
or (float(row[1]) > self.coquimbo_bounding_box.lat_max)
):
continue
if not ((last_longitude == row[0]) and (last_latitude == row[1])):
# Create new location entry
location_count += 1
location = "GeomFromText('POINT(%s %s)', 4326)" % (row[0], row[1])
location_id = self.insert_location(row[4], location, int(row[2]), row[3])
logger.debug("Location %d created" % location_count)
last_longitude = row[0]
last_latitude = row[1]
# Check if location changed to make sure the correct location index is used
location = [float(row[0]), float(row[1])]
if location not in location_list:
location_list.append(location)
location_id = location_list.index(location)
# Add asset to current location
structural_per_building = float(row[7]) / float(row[6])
......@@ -339,9 +342,22 @@ class CoquimboDatabase(Database):
night_per_building,
)
asset_count += 1
logger.debug("Asset %d for location %d created" % (asset_count, location_count))
logger.debug("Asset %d for location %d created" % (asset_count, location_id))
# Create entries in the Location table
for location in location_list:
location_id = location_list.index(location)
self.insert_location(
location_id,
row[4],
"GeomFromText('POINT(%s %s)', 4326)" % (location[0], location[1]),
int(row[2]),
row[3],
)
logger.debug("Location %d created" % location_id)
self.connection.commit()
logger.info("Number of locations created: %d." % location_count)
logger.info("Number of locations created: %d." % (location_id + 1))
logger.info("Number of assets created: %d." % asset_count)
def add_voronoi_cells(self):
......
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