Commit ecfc1723 authored by shinde's avatar shinde
Browse files

Added dwelling numbers for districts with no information

parent 951851bd
Pipeline #35293 passed with stage
in 1 minute and 36 seconds
......@@ -89,8 +89,9 @@ CONSTRUCTION_MATERIAL_ID_NO_INFO = 4
STORY_NUMBER_ID_NO_INFO = 5
NUMBER_BUILDING_NO_INFO = 6
# Constant for length of building numbers with no information list
LENGTH_BUILDING_NUMBERS_NO_INFO = 656
# Constant for calculating dwelling numbers in administrative units with no information
DWELLING_DENSITY = 5
BUILDING_DENSITY_DWELLING_NUMBER = 6
# Constant for Japanese and European Geodetic CRS
JGD2000 = 4612
......
......@@ -1258,7 +1258,7 @@ class JapanDatabase(SpatialiteDatabase):
for row in self.calculate_frequency_distributions(attributes):
for admin in districts:
if row[constants.DISTRICT_ID] == admin[constants.DISTRICT_ID]:
# Calculate unit area
# Assign unit area
administrative_unit_area = admin[constants.ADMINISTRATIVE_UNIT_AREA]
# Calculate building density
......@@ -1271,9 +1271,9 @@ class JapanDatabase(SpatialiteDatabase):
else:
continue
# Reduce the building_numbers_info_list to only include building_type_id,
# construction_material_id and story_number_id = 0 (i.e. total) and districts present
# in population_list
# Create an empty list to store the building numbers and density for building_type_id,
# construction_material_id and story_number_id 0 (i.e. total)
# from dwelling_numbers_info_list
building_number_constrained = []
# Loop through the building_numbers_info_list to store the building numbers
......@@ -1327,7 +1327,7 @@ class JapanDatabase(SpatialiteDatabase):
building_number_constrained
)
if len(building_numbers_no_info_list) != length_admin_units_no_info_expected:
logger.info(
raise ValueError(
"ERROR: Administrative units with no information not correctly extracted"
)
......@@ -1355,7 +1355,7 @@ class JapanDatabase(SpatialiteDatabase):
)
numpy.seterr(divide="ignore", invalid="ignore")
# Calculate administrative unit area for administrative units with no information
# Assign administrative unit area to administrative units with no information
for admin in districts:
if building_no_info[constants.DISTRICT_ID] == admin[constants.DISTRICT_ID]:
administrative_unit_area_no_info = admin[constants.ADMINISTRATIVE_UNIT_AREA]
......@@ -1417,6 +1417,10 @@ class JapanDatabase(SpatialiteDatabase):
)
logger.info("Number of buildings added")
# Create an empty list to add dwelling attributes and numbers for districts (i.e.
# administrative units) with dwelling information
dwelling_numbers_info_list = []
# Read columns identification area code, district, building type, construction material
# type, story number type and number of dwellings from input csv file
dwelling_numbers_input = pandas.read_excel(
......@@ -1466,43 +1470,173 @@ class JapanDatabase(SpatialiteDatabase):
)
add_element_and_get_index(dwelling_attributes_tuple, attributes)
# Calculate the frequency distribution and insert dwelling attributes
# and numbers into the DwellingNumber table
# Calculate the dwelling-frequency distribution and dwelling density, and
# store them into dwelling_numbers_info_list
for row in self.calculate_frequency_distributions(attributes):
district_id = row[constants.DISTRICT_ID]
building_type_id = row[constants.BUILDING_TYPE_ID]
construction_material_id = row[constants.CONSTRUCTION_MATERIAL_ID]
story_number_id = row[constants.STORY_NUMBER_ID]
for admin in districts:
if row[constants.DISTRICT_ID] == admin[constants.DISTRICT_ID]:
# Assign unit area
administrative_unit_area = admin[constants.ADMINISTRATIVE_UNIT_AREA]
# Extract the building or dwelling number calculated with frequency
# distribution calculations
if (
len(row)
> constants.NUMBER_DWELLING_OR_NUMBER_BUILDING_FREQUENCY_DISTRIBUTION
):
dwelling_number = row[
constants.NUMBER_DWELLING_OR_NUMBER_BUILDING_FREQUENCY_DISTRIBUTION
]
# Calculate dwelling density
dwelling_density_info = numpy.true_divide(
row[constants.NUMBER_BUILDING], administrative_unit_area
)
numpy.seterr(divide="ignore", invalid="ignore")
dwelling_numbers = row + (dwelling_density_info,)
add_element_and_get_index(dwelling_numbers, dwelling_numbers_info_list)
# Create an empty list to store the dwelling numbers and density for building_type_id,
# construction_material_id and story_number_id 0 (i.e. total)
# from dwelling_numbers_info_list
dwelling_number_constrained = []
# Loop through the dwelling_numbers_info_list to store the dwelling numbers for
# building_type_id, construction_material_id and story_number_id
# 0 (i.e. total)
for dwelling in dwelling_numbers_info_list:
if (
dwelling[constants.BUILDING_TYPE_ID] == constants.TOTAL
and dwelling[constants.CONSTRUCTION_MATERIAL_ID] == constants.TOTAL
and dwelling[constants.STORY_NUMBER_ID] == constants.TOTAL
):
add_element_and_get_index(dwelling, dwelling_number_constrained)
else:
continue
# Extract the dwelling number which do not change for frequency
# distribution calculations
# Create list to store building numbers with total attributes
building_number_constrained = []
for building in building_numbers:
if (
building[constants.BUILDING_TYPE_ID] == constants.TOTAL
and building[constants.CONSTRUCTION_MATERIAL_ID] == constants.TOTAL
and building[constants.STORY_NUMBER_ID] == constants.TOTAL
):
add_element_and_get_index(building, building_number_constrained)
# Create an empty list for districts (i.e. administrative units) with no
# dwelling information
dwelling_numbers_no_info_list = []
# Create a list with all districts in the constrained building numbers list
building_districts = [
district[constants.DISTRICT_ID] for district in building_number_constrained
]
# Create a list with all districts in the constrained dwelling numbers list
dwelling_districts = [
district[constants.DISTRICT_ID] for district in dwelling_number_constrained
]
# Find the residual districts not present in the dwelling_number_constrained
# list but present in building_number_constrained
residual_districts = [
district for district in building_districts if district not in dwelling_districts
]
# Store all building attribute values for the residual_districts in
# dwelling_numbers_no_info_list
for district in residual_districts:
for building in building_number_constrained:
if building[constants.DISTRICT_ID] == district:
add_element_and_get_index(building, dwelling_numbers_no_info_list)
else:
dwelling_number = row[constants.NUMBER_DWELLING]
self.insert_dwelling_number(
district_id,
building_type_id,
construction_material_id,
story_number_id,
dwelling_number,
)
logger.debug(
"Dwelling number for district %s, type %s, %s added"
% (
admin_id,
building_type_list[building_type_id],
story_number_list[story_number_id],
continue
# Map the building_density from building_number_constrained to the dwellings in
# dwelling_number_constrained list
for building in building_number_constrained:
for dwelling in dwelling_number_constrained:
# Append building_numbers_list to add the population_density for each district
if building[constants.DISTRICT_ID] == dwelling[constants.DISTRICT_ID]:
building_density_dwelling_number = building[constants.BUILDING_DENSITY]
index = dwelling_number_constrained.index(dwelling)
dwelling_number_constrained[index] = dwelling + (
building_density_dwelling_number,
)
# Test if all administrative units with no information have been extracted
length_admin_units_no_info_expected = len(building_districts) - len(
dwelling_number_constrained
)
if len(building_numbers_no_info_list) != length_admin_units_no_info_expected:
raise ValueError(
"ERROR: Administrative units with no information not correctly extracted"
)
# Find the number of neighbors
max_neighbors = self.get_max_neighbours(building_districts)
# Find the dwelling numbers for dwelling_numbers_no_info_list
for dwelling_no_info in dwelling_numbers_no_info_list:
building_density_dwelling_number = dwelling_no_info[constants.BUILDING_DENSITY]
# Sort the list containing the differences of the building_density
sorted_diff_bldg_density = sorted(
dwelling_number_constrained,
key=lambda t: abs(
t[constants.BUILDING_DENSITY_DWELLING_NUMBER]
- building_density_dwelling_number
),
)[0:max_neighbors]
sum_dwell_den = sum(
[item[constants.DWELLING_DENSITY] for item in sorted_diff_bldg_density]
)
# Average of the first 10 dwelling densities in the sorted_diff_bldg_density
dwelling_density_no_info = numpy.true_divide(
sum_dwell_den, len(sorted_diff_bldg_density)
)
numpy.seterr(divide="ignore", invalid="ignore")
# Assign administrative unit area to administrative units with no information
for admin in districts:
if dwelling_no_info[constants.DISTRICT_ID] == admin[constants.DISTRICT_ID]:
administrative_unit_area_no_info = admin[constants.ADMINISTRATIVE_UNIT_AREA]
# Calculate dwelling numbers for administrative units with no information
# and add it to the dwelling_numbers_no_info_list list
dwelling_numbers_no_info = (
administrative_unit_area_no_info * dwelling_density_no_info
)
index = dwelling_numbers_no_info_list.index(dwelling_no_info)
# Reduce the row to only include district_id
dwelling_no_info_reduced = dwelling_no_info[
constants.DISTRICT_ID : constants.NUMBER_BUILDING # noqa: E203
]
dwelling_numbers_no_info_list[index] = dwelling_no_info_reduced + (
dwelling_numbers_no_info,
dwelling_density_no_info,
)
# Store dwelling_numbers for districts with and without dwelling
# information in a single list
dwelling_numbers = dwelling_numbers_info_list + dwelling_numbers_no_info_list
for dwelling in dwelling_numbers:
district_id = dwelling[constants.DISTRICT_ID]
building_type_id = dwelling[constants.BUILDING_TYPE_ID]
construction_material_id = dwelling[constants.CONSTRUCTION_MATERIAL_ID]
story_number_id = dwelling[constants.STORY_NUMBER_ID]
dwelling_number = dwelling[constants.NUMBER_BUILDING]
self.insert_dwelling_number(
district_id,
building_type_id,
construction_material_id,
story_number_id,
dwelling_number,
)
logger.debug(
"Dwelling number for district %s, type %s, %s added"
% (
admin_id,
building_type_list[building_type_id],
story_number_list[story_number_id],
)
)
logger.info("Number of dwellings added")
# List with dwelling attributes
......
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