Commit a9a4fe01 authored by Simantini Shinde's avatar Simantini Shinde
Browse files

Calculated dwelling numbers for district with no info

parent 158ff89e
Pipeline #39292 passed with stage
in 1 minute and 43 seconds
......@@ -1005,6 +1005,148 @@ class JapanDatabase(SpatialiteDatabase):
self.connection.commit()
logger.info("Fixed rounding up errors for dwelling numbers")
def calculate_dwelling_numbers_district_no_info(self):
"""
Calculates the dwelling numbers for districts that do not have dwelling information in
the dwelling dataset.
The dwelling numbers are calculated in query `ToUpdateDwellingNumber` by multiplying
the building numbers for districts with no building information with a factor of the
number of dwellings per building as taken from the districts with building numbers
closest to the building numbers of the district with no building information.
The innermost query `DistrictWithNoInformation` initially finds the districts not
present in the DwellingNumber table by comparing it to the BuildingNumber and
PopulationDistribution tables. These districts are then mapped to their corresponding
building numbers. This final table is cross-joined with all districts from
PopulationDistribution and BuildingNumber and their corresponding building numbers
from the BuildingNumber (i.e. with building information) table in the query
SourceBuildingNumber.
The next outer query `MappingToDistrictWithNoInfo` calculates the absolute value
of the difference between the building numbers of districts with and without
information and sorts it in ascending order to find the districts with information
which are closest in building numbers to the districts without building information.
The following outer query `ClosestMappingToDistrictWithNoInfo` selects only the
first ten districts from districts in query MappingToDistrictWithNoInfo sorted
by the aforementioned difference.
The next outer query `MappingBuildingExposureToDistrictWithNoInfo` maps the building
attributes and their dwelling numbers from the DwellingNumber table to the districts
with no information and calculates the dwelling numbers for districts with no dwelling
information. The mapping is done on the district_id of the ten closest districts
(with information) found in the query `ClosestMappingToDistrictWithNoInfo`.
The final query `ToUpdateDwellingNumber` selects the district_id, building exposure
attributes and dwelling number to be inserted into the DwellingNumber table.
"""
sql_statement = """
INSERT INTO DwellingNumber
(
district_id,
building_type_id,
construction_material_id,
story_number_id,
number_dwelling
)
SELECT ToUpdateDwellingNumber.to_update_id,
ToUpdateDwellingNumber.building_type_id,
ToUpdateDwellingNumber.construction_material_id,
ToUpdateDwellingNumber.story_number_id,
ToUpdateDwellingNumber.to_update_number_dwelling
FROM
(
SELECT MappingBuildingExposureToDistrictWithNoInfo.to_update_id,
MappingBuildingExposureToDistrictWithNoInfo.source_id,
MappingBuildingExposureToDistrictWithNoInfo.building_type_id,
MappingBuildingExposureToDistrictWithNoInfo.construction_material_id,
MappingBuildingExposureToDistrictWithNoInfo.story_number_id,
MappingBuildingExposureToDistrictWithNoInfo.number_dwelling,
sum(MappingBuildingExposureToDistrictWithNoInfo.number_dwelling),
AVG(MappingBuildingExposureToDistrictWithNoInfo.number_dwelling)/
MappingBuildingExposureToDistrictWithNoInfo.source_number_building
* MappingBuildingExposureToDistrictWithNoInfo.to_update_number_building
AS to_update_number_dwelling
FROM
(
SELECT *
FROM
(
SELECT MappingToDistrictWithNoInfo.to_update_id,
MappingToDistrictWithNoInfo.source_id,
MappingToDistrictWithNoInfo.rn,
MappingToDistrictWithNoInfo.to_update_number_building,
MappingToDistrictWithNoInfo.source_number_building
FROM
(
SELECT DistrictWithNoInformation.to_update_id,
DistrictWithNoInformation.source_id,
row_number() OVER (
PARTITION BY DistrictWithNoInformation.to_update_id
) AS rn,
DistrictWithNoInformation.to_update_number_building,
DistrictWithNoInformation.source_number_building
FROM
(
SELECT B.district_id AS to_update_id,
B.number_building AS to_update_number_building,
SourceBuildingNumber.district_id AS source_id,
SourceBuildingNumber.number_building AS source_number_building,
ABS(B.number_building -
SourceBuildingNumber.number_building) AS difference
FROM BuildingNumber AS B
INNER JOIN PopulationDistribution AS PD
ON B.district_id = PD.district_id
CROSS JOIN
(
SELECT BN.district_id, BN.number_building,
BN.building_type_id, BN.construction_material_id,
BN.story_number_id
FROM BuildingNumber AS BN
INNER JOIN
(
SELECT DISTINCT district_id
FROM DwellingNumber
) AS DN
ON BN.district_id = DN.district_id
INNER JOIN PopulationDistribution AS PD
ON PD.district_id = DN.district_id
WHERE BN.building_type_id = 0
AND BN.construction_material_id = 0
AND BN.story_number_id = 0
) AS SourceBuildingNumber
WHERE B.district_id NOT IN
(
SELECT district_id
FROM DwellingNumber
)
AND B.building_type_id = 0
AND B.construction_material_id = 0
AND B.story_number_id = 0
ORDER BY B.district_id, difference ASC
) AS DistrictWithNoInformation
) AS MappingToDistrictWithNoInfo
WHERE rn < 11
) AS ClosestMappingToDistrictWithNoInfo
INNER JOIN DwellingNumber
ON ClosestMappingToDistrictWithNoInfo.source_id = DwellingNumber.district_id
ORDER BY ClosestMappingToDistrictWithNoInfo.to_update_id
) AS MappingBuildingExposureToDistrictWithNoInfo
Group by MappingBuildingExposureToDistrictWithNoInfo.to_update_id,
MappingBuildingExposureToDistrictWithNoInfo.building_type_id,
MappingBuildingExposureToDistrictWithNoInfo.construction_material_id,
MappingBuildingExposureToDistrictWithNoInfo.story_number_id
) AS ToUpdateDwellingNumber
"""
logger.debug(sql_statement)
self.cursor.execute(sql_statement)
self.connection.commit()
logger.info(
"Added dwelling numbers for districts with no information in DwellingNumber table"
)
def import_exposure_data(
self,
population_distribution_filepath,
......@@ -1068,6 +1210,9 @@ class JapanDatabase(SpatialiteDatabase):
# and update the DwellingNumber table
self.calculate_dwelling_numbers_frequency_distributions()
# Calculate the dwelling numbers for districts with no dwelling information
self.calculate_dwelling_numbers_district_no_info()
# Add the building types to the database
for building_type_id, building_type in enumerate(building_type_list):
self.insert_building_type(building_type_id, building_type)
......
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