Commit c7061ae3 authored by shinde's avatar shinde
Browse files

Changed to PopulationDistribution and added function for readdistrict_id

parent 6f1bd8af
Pipeline #27868 failed with stage
in 4 minutes and 44 seconds
#!/usr/bin/env python1
#!/usr/bin/env python3
# Copyright (C) 2021:
# Helmholtz-Zentrum Potsdam Deutsches GeoForschungsZentrum GFZ
......@@ -165,16 +165,16 @@ class JapanDatabase(SpatialiteDatabase):
self.connection.execute(sql_statement)
logger.debug("Table DwellingDistribution created")
# Create table Population
sql_statement = "CREATE TABLE Population ("
sql_statement += "id INTEGER PRIMARY KEY, "
sql_statement += "district_id INTEGER, "
sql_statement += "total INTEGER, "
sql_statement += "male INTEGER, "
sql_statement += "female INTEGER, "
sql_statement += "number_household INTEGER)"
# Create table PopulationDistribution
sql_statement = "CREATE TABLE PopulationDistribution ("
sql_statement += "id INTEGER PRIMARY KEY, "
sql_statement += "district_id INTEGER, "
sql_statement += "total INTEGER, "
sql_statement += "male INTEGER, "
sql_statement += "female INTEGER, "
sql_statement += "number_household INTEGER)"
self.connection.execute(sql_statement)
logger.debug("Table Population created")
logger.debug("Table PopulationDistribution created")
# Create table BuildingType
sql_statement = "CREATE TABLE BuildingType ("
......@@ -459,9 +459,9 @@ class JapanDatabase(SpatialiteDatabase):
)
self.cursor.execute(sql_statement)
def insert_population(self, district_id, total, male, female, number_household):
def insert_population_distribution(self, district_id, total, male, female, number_household):
"""
Inserts a full dataset to the Population table.
Inserts a full dataset to the PopulationDistribution table.
Args:
district_id (int):
......@@ -475,7 +475,8 @@ class JapanDatabase(SpatialiteDatabase):
number_household (int):
Total number of households of the district
"""
sql_statement = "INSERT INTO Population "
sql_statement = "INSERT INTO PopulationDistribution "
sql_statement += "(district_id, total, male, female, "
sql_statement += "number_household) "
sql_statement += "VALUES (%d, %d, %d, %d, %d)" % (
......@@ -612,13 +613,21 @@ class JapanDatabase(SpatialiteDatabase):
self.connection.commit()
logger.info("Districts and boundaries added")
def read_district_id(self, admin_id):
# 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()
return result
def import_exposure_data(
self,
dwelling_numbers_filepath,
building_numbers_filepath,
household_numbers_filepath,
dwelling_sizes_filepath,
population_filepath,
population_distribution_filepath,
):
"""
Imports all exposure data from the Excel files provided by E-Stat, Japan. The following
......@@ -634,8 +643,8 @@ class JapanDatabase(SpatialiteDatabase):
File path to the file of numbers of households
dwelling_sizes_filepath (str):
File path to the file of dwelling sizes
population_filepath (str):
File path to the path of population
population_distribution_filepath (str):
File path to the path of population distribution
"""
# Tables for building classifications
......@@ -668,13 +677,10 @@ class JapanDatabase(SpatialiteDatabase):
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
district_id_result = self.read_district_id(admin_id)
if district_id_result is None: # Only data for which a district exist matter
continue
district_id = result[0]
district_id = district_id_result[0]
# Get ID of building type and number of stories
building_type_id = add_element_and_get_index(
......@@ -708,13 +714,10 @@ class JapanDatabase(SpatialiteDatabase):
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
district_id_result = self.read_district_id(admin_id)
if district_id_result is None: # Only data for which a district exist matter
continue
district_id = result[0]
district_id = district_id_result[0]
# Get ID of building type and number of stories
building_type_id = add_element_and_get_index(
......@@ -753,13 +756,10 @@ class JapanDatabase(SpatialiteDatabase):
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
district_id_result = self.read_district_id(admin_id)
if district_id_result is None: # Only data for which a district exist matter
continue
district_id = result[0]
district_id = district_id_result[0]
# Get ID of building type, dwelling type, and tenure type
building_type_id = add_element_and_get_index(
......@@ -818,13 +818,10 @@ class JapanDatabase(SpatialiteDatabase):
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
district_id_result = self.read_district_id(admin_id)
if district_id_result is None: # Only data for which a district exist matter
continue
district_id = result[0]
district_id = district_id_result[0]
# Get ID of building type, dwelling type, and tenure type
building_type_id = add_element_and_get_index(
......@@ -885,28 +882,25 @@ class JapanDatabase(SpatialiteDatabase):
)
logger.info("Number and sizes of dwellings added")
# Read Population
population_input = pandas.read_csv(population_filepath, usecols=[2, 9, 10, 11, 12])
for index, row in population_input.iterrows():
# Read PopulationDistribution
population_distribution_input = pandas.read_csv(population_distribution_filepath, usecols=[2, 9, 10, 11, 12])
for index, row in population_distribution_input.iterrows():
admin_id = int(row["KEY_CODE"])
# 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
district_id_result = self.read_district_id(admin_id)
if district_id_result is None: # Only data for which a district exist matter
continue
district_id = result[0]
district_id = district_id_result[0]
# Prepare and insert the data to the Population table
# Prepare and insert the data to the PopulationDistribution table
total = int(row[1])
male = int(row[2])
female = int(row[3])
number_household = int(row[4])
self.insert_population(district_id, total, male, female, number_household)
logger.debug("Population for district %s added" % admin_id)
logger.info("Population added")
self.insert_population_distribution(district_id, total, male, female, number_household)
logger.debug("PopulationDistribution for district %s added" % admin_id)
logger.info("Population data added")
# Add the building types to the database
for building_type_id, building_type in enumerate(building_type_list):
......
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