Commit 274d7a1a authored by Simantini Shinde's avatar Simantini Shinde
Browse files

Fixed import_building_numbers function

parent 84d3e5e1
Pipeline #39103 passed with stage
in 1 minute and 29 seconds
......@@ -496,16 +496,17 @@ class JapanDatabase(SpatialiteDatabase):
self, building_numbers_filepath, building_type_list, story_number_list
):
"""
Imports all building data from the 2018 building statistics file provided
Imports all building data from the 2018 building statistics files provided
by E-Stat, Japan. The following file is needed:
List of numbers of buildings by municipality (building_numbers_filepath)
The building data provide the building numbers for a combination of different
building attributes; building types (total, detached house, tenement house,
apartments and others), construction material type (total, wooden, non-wooden)
and story number type (total, 1 story, 2 stories and over, 2 stories,
3 to 5 stories and 6 stories) except for building type `Other` which is
given for story number `total` only.
building attributes:
- building types (total, detached house, tenement house, apartments and others)
- construction material type (total, wooden, non-wooden)
- story number type (total, 1 story, 2 stories and over, 2 stories, 3 to 5 stories
and 6 stories) except for building type `Other` which is
given for story number `total` only.
These building numbers are not available for all districts in Japan
i.e. the districts with population less than 15000 are ignored in the
......@@ -522,73 +523,66 @@ class JapanDatabase(SpatialiteDatabase):
"""
# Create a list with construction material types total, wooden and non-wooden
# from the building numbers input CSV file
building_number_construction_material_list = ["0_Total", "1_Wooden", "2_Non-wooden"]
# Read columns identification area code, district, building type, construction material
# type, story number type and number of buildings from input csv file
# type, story number type and number of buildings from input Excel file
building_numbers_input = pandas.read_excel(
building_numbers_filepath, header=10, usecols=[4, 5, 7, 9, 10, 11, 12]
)
# Iterate through each district
for i in building_numbers_input["Area classification"].unique():
# Create a Pandas dataframe for each district and its building attributes
building_attributes = building_numbers_input.loc[
building_numbers_input["Area classification"] == i
]
for index, row in building_attributes.iterrows():
admin_id = int((row["Area classification"].split("_"))[0])
for index, row in building_numbers_input.iterrows():
admin_id = int((row["Area classification"].split("_"))[0])
# Identify district_id based on admin_id from the District table
district_id_result = self.get_district_id(admin_id)
if district_id_result is None: # Only data for which a district exist matter
continue
district_id = district_id_result[0]
# Identify district_id based on admin_id from the District table
district_id_result = self.get_district_id(admin_id)
if district_id_result is None: # Only data for which a district exist matter
continue
district_id = district_id_result[0]
# Get ID of building type and number of stories
building_type_id = add_element_and_get_index(
row["Type of building"], building_type_list
)
story_number_id = add_element_and_get_index(
row["Stories of building"], story_number_list
# Get ID of building type and number of stories
building_type_id = add_element_and_get_index(
row["Type of building"], building_type_list
)
story_number_id = add_element_and_get_index(
row["Stories of building"], story_number_list
)
# Insert building numbers for total, wooden and non-wooden construction material
for construction_material_index in range(
len(building_number_construction_material_list)
):
building_number = float(
str(row[constants.NUMBER_BUILDING + construction_material_index]).replace(
"-", "0"
)
)
# Insert building numbers for total, wooden and non-wooden construction material
for construction_material_index in range(
len(building_number_construction_material_list)
# Map the column index to the construction_material_id
construction_material_id = construction_material_index
# Re-assign construction_material_id for non-wooden from the
# construction_material_list
if (
construction_material_id
== constants.WOODEN_EXCLUDING_WOODEN_AND_FIRE_PROOFED
):
building_number = float(
str(
row[constants.NUMBER_BUILDING + construction_material_index]
).replace("-", "0")
)
construction_material_id = constants.NON_WOODEN
# Map the column index to the construction_material_id
construction_material_id = construction_material_index
# Re-assign construction_material_id for non-wooden from the
# construction_material_list
if (
construction_material_id
== constants.WOODEN_EXCLUDING_WOODEN_AND_FIRE_PROOFED
):
construction_material_id = constants.NON_WOODEN
# Insert the data to the BuildingNumber table
sql_statement = (
"INSERT INTO BuildingNumber (district_id, building_type_id, "
"construction_material_id, story_number_id, number_building) "
"VALUES (%d, %d, %d, %d, %f)"
% (
district_id,
building_type_id,
construction_material_id,
story_number_id,
building_number,
)
# Insert the data to the BuildingNumber table
sql_statement = (
"INSERT INTO BuildingNumber (district_id, building_type_id, "
"construction_material_id, story_number_id, number_building) "
"VALUES (%d, %d, %d, %d, %f)"
% (
district_id,
building_type_id,
construction_material_id,
story_number_id,
building_number,
)
self.cursor.execute(sql_statement)
)
self.cursor.execute(sql_statement)
self.connection.commit()
logger.info("Building numbers added")
......
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