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

Implemented the import of dwelling numbers

parent 7294f5ce
Pipeline #21589 passed with stage
in 1 minute and 31 seconds
......@@ -18,6 +18,7 @@
import logging
import sqlite3
import pandas
# Initialize log
......@@ -109,6 +110,35 @@ class JapanDatabase(Database):
self.connection.execute(sql_statement)
logger.debug("Table District created")
# Create table DwellingNumber
sql_statement = "CREATE TABLE DwellingNumber ("
sql_statement += "id INTEGER PRIMARY KEY AUTOINCREMENT, "
sql_statement += "district_id INTEGER, "
sql_statement += "building_type_id INTEGER, "
sql_statement += "construction_material_id INTEGER, "
sql_statement += "number_stories_id INTEGER, "
sql_statement += "number_dwelling REAL)"
self.connection.execute(sql_statement)
logger.debug("Table DwellingNumber created")
# Create table BuildingType
sql_statement = "CREATE TABLE BuildingType ("
sql_statement += "id INTEGER PRIMARY KEY, "
sql_statement += "description TEXT)"
self.connection.execute(sql_statement)
# Create table ConstructionMaterial
sql_statement = "CREATE TABLE ConstructionMaterial ("
sql_statement += "id INTEGER PRIMARY KEY, "
sql_statement += "description TEXT)"
self.connection.execute(sql_statement)
# Create table NumberStories
sql_statement = "CREATE TABLE NumberStories ("
sql_statement += "id INTEGER PRIMARY KEY, "
sql_statement += "description TEXT)"
self.connection.execute(sql_statement)
def insert_district(self, admin_id, admin_name, geom):
"""
Inserts the ID, name, and geometry of a district to the District table.
......@@ -127,6 +157,90 @@ class JapanDatabase(Database):
sql_statement += "VALUES ('%s', '%s', %s)" % (admin_id, admin_name, geom)
self.cursor.execute(sql_statement)
def insert_dwelling_number(
self,
district_id,
building_type_id,
construction_material_id,
number_stories_id,
number_dwelling,
):
"""
Inserts a full dataset to the BuildingNumber table.
Args:
district_id (int):
ID of the district. Corresponds to District.id
building_type_id (int):
ID of the building type. Corresponds to BuildingType.id
construction_material_id (int):
ID of the construction material. Corresponds to ConstructionMaterial.id
number_stories_id (int):
ID of the number of stories type. Corresponds to NumberStories.id
number_dwelling (float):
Number of buildings for the combination of the other parameters
"""
sql_statement = "INSERT INTO DwellingNumber "
sql_statement += "(district_id, building_type_id, construction_material_id, "
sql_statement += "number_stories_id, number_dwelling) "
sql_statement += "VALUES (%d, %d, %d, %d, %f)" % (
district_id,
building_type_id,
construction_material_id,
number_stories_id,
number_dwelling,
)
self.cursor.execute(sql_statement)
def insert_building_type(self, building_type_id, description):
"""
Inserts a building-type description to the BuildingType table.
Args:
building_type_id (int):
ID of the building type
description (str):
Description of the building type
"""
sql_statement = "INSERT INTO BuildingType "
sql_statement += "(id, description) "
sql_statement += "VALUES (%d, '%s')" % (building_type_id, description)
self.cursor.execute(sql_statement)
def insert_construction_material(self, construction_material_id, description):
"""
Inserts a construction-material description to the ConstructionMaterial table.
Args:
construction_material_id (int):
ID of the construction material
description (str):
Description of the construction material
"""
sql_statement = "INSERT INTO ConstructionMaterial "
sql_statement += "(id, description) "
sql_statement += "VALUES (%d, '%s')" % (construction_material_id, description)
self.cursor.execute(sql_statement)
def insert_number_stories(self, number_stories_id, description):
"""
Inserts a story-number description to the NumberStories table.
Args:
number_stories_id (int):
ID of the number of stories entry
description (str):
Description of the number of stories entry
"""
sql_statement = "INSERT INTO NumberStories "
sql_statement += "(id, description) "
sql_statement += "VALUES (%d, '%s')" % (number_stories_id, description)
self.cursor.execute(sql_statement)
def read_districts_and_boundaries(self, district_boundary_filepath):
"""
Imports all districts and boundaries from a prepared geopackage file
......@@ -155,3 +269,79 @@ class JapanDatabase(Database):
logger.debug("District %d with boundary added" % row[0])
self.connection.commit()
logger.info("Districts and boundaries added")
def import_exposure_data(self, dwelling_numbers_filepath):
"""
Imports all exposure data from the Excel files provided by E-Stat, Japan. The following
files are needed:
List of numbers of dwellings by municipality (dwelling_numbers_filepath)
Args:
dwelling_numbers_filepath (str):
File path to the file of number_of_dwellings)
"""
building_type_list = []
number_stories_list = []
construction_material_list = [
"0_Total",
"1_Wooden",
"101_Wooden (excluding wooden and fire-proofed)",
"102_Wooden and fire-proofed",
"2_Non-wooden",
]
dwelling_numbers_input = pandas.read_excel(
dwelling_numbers_filepath, header=10, usecols=[4, 5, 7, 9, 10, 11, 12, 13, 14]
)
for index, row in dwelling_numbers_input.iterrows():
admin_id = int((row["Area classification"].split("_"))[0])
# Identify district_id based on admin_id from the District table
sql_statement = "SELECT id FROM District "
sql_statement += "WHERE admin_id = %d" % admin_id
self.cursor.execute(sql_statement)
result = self.cursor.fetchone()
if result is None: # Only data for which a district exist matter
continue
district_id = result[0]
# Get ID of building type
building_type = row["Type of building"]
if building_type not in building_type_list:
building_type_list.append(building_type)
building_type_id = building_type_list.index(building_type)
# Get ID of number of stories
number_stories = row["Stories of building"]
if number_stories not in number_stories_list:
number_stories_list.append(number_stories)
number_stories_id = number_stories_list.index(number_stories)
# Insert building numbers for each construction material
for material_id in range(5):
dwelling_number = float(str(row[4 + material_id]).replace("-", "0"))
self.insert_dwelling_number(
district_id,
building_type_id,
material_id,
number_stories_id,
dwelling_number,
)
# Add the building types to the database
for building_type in building_type_list:
building_type_id = building_type_list.index(building_type)
self.insert_building_type(building_type_id, building_type)
# Add the number of stories to the database
for number_stories in number_stories_list:
number_stories_id = number_stories_list.index(number_stories)
self.insert_number_stories(number_stories_id, number_stories)
# Add the number of stories to the database
for construction_material in construction_material_list:
construction_material_id = construction_material_list.index(construction_material)
self.insert_construction_material(construction_material_id, construction_material)
self.connection.commit()
......@@ -43,6 +43,7 @@ def main():
exit()
db.create_tables()
db.read_districts_and_boundaries("../data/estat_bound_municipal.gpkg")
db.import_exposure_data("../data/e008_3e.xlsx")
# Leave the program
sys.exit()
......
Markdown is supported
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