diff --git a/exposurejapan/database.py b/exposurejapan/database.py index fdb8a1e5958379d85e02e78fc6459b518e2c6a9f..fd02f271518e4092d0e507f886c084de3b958512 100644 --- a/exposurejapan/database.py +++ b/exposurejapan/database.py @@ -77,9 +77,9 @@ class JapanDatabase(SpatialiteDatabase): DwellingFloorspace : Stores the floorspace per dwelling depending on the building, dwelling, tenure types and construction material for each district - DwellingDistribution: Stores the number of dwellings of different sizes depending on - the building, dwelling, tenure types and construction material - for each district + DwellingDistribution: Stores the number of dwellings of different sizes depending + on the building, dwelling, tenure types and construction + material for each district BuildingType : Stores the different types of buildings DwellingType : Stores the different types of dwellings TenureType : Stores the different types of tenures @@ -129,6 +129,7 @@ class JapanDatabase(SpatialiteDatabase): sql_statement += "building_type_id INTEGER, " sql_statement += "dwelling_type_id INTEGER, " sql_statement += "tenure_type_id INTEGER, " + sql_statement += "construction_material_id INTEGER, " sql_statement += "number_dwelling REAL, " sql_statement += "number_household REAL, " sql_statement += "number_household_member REAL, " @@ -314,6 +315,7 @@ class JapanDatabase(SpatialiteDatabase): building_type_id, dwelling_type_id, tenure_type_id, + construction_material_id, number_dwelling, number_household, number_household_member, @@ -335,6 +337,8 @@ class JapanDatabase(SpatialiteDatabase): ID of the dwelling type. Corresponds to DwellingType.id tenure_type_id (int): ID of the tenure type. Corresponds to TenureType.id + construction_material_id (int): + ID of the construction material. Corresponds to ConstructionMaterial.id number_dwelling (float): Number of dwellings for the combination of the other parameters number_household (float): @@ -356,23 +360,40 @@ class JapanDatabase(SpatialiteDatabase): sql_statement = "INSERT INTO HouseholdData " sql_statement += "(district_id, building_type_id, dwelling_type_id, tenure_type_id, " - sql_statement += "number_dwelling, number_household, number_household_member, " + sql_statement += "construction_material_id, number_dwelling, " + sql_statement += "number_household, number_household_member, " sql_statement += "rooms_per_dwelling, tatami_per_dwelling, floorspace_per_dwelling, " sql_statement += "tatami_per_person, person_per_room) " - sql_statement += "VALUES (%d, %d, %d, %d, %f, %f, %f, %f, %f, %f, %f, %f)" % ( - district_id, - building_type_id, - dwelling_type_id, - tenure_type_id, - number_dwelling, - number_household, - number_household_member, - rooms_per_dwelling, - tatami_per_dwelling, - floorspace_per_dwelling, - tatami_per_person, - person_per_room, - ) + + if construction_material_id == 0: + sql_statement += "VALUES (%d, %d, %d, %d, %d, %f, %f, %f, %f, %f, %f, %f, %f)" % ( + district_id, + building_type_id, + dwelling_type_id, + tenure_type_id, + construction_material_id, + number_dwelling, + number_household, + number_household_member, + rooms_per_dwelling, + tatami_per_dwelling, + floorspace_per_dwelling, + tatami_per_person, + person_per_room, + ) + else: + sql_statement += ( + "VALUES (%d, %d, %d, %d, %d, NULL, %f, %f, NULL, NULL, NULL, NULL, NULL)" + % ( + district_id, + building_type_id, + dwelling_type_id, + tenure_type_id, + construction_material_id, + number_household, + number_household_member, + ) + ) self.cursor.execute(sql_statement) def insert_dwelling_floorspace( @@ -702,10 +723,16 @@ class JapanDatabase(SpatialiteDatabase): ) # Insert dwelling numbers for each construction material - for material_id in range(5): - dwelling_number = float(str(row[4 + material_id]).replace("-", "0")) + for construction_material_id in range(5): + dwelling_number = float( + str(row[4 + construction_material_id]).replace("-", "0") + ) self.insert_dwelling_number( - district_id, building_type_id, material_id, story_number_id, 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" @@ -738,14 +765,25 @@ class JapanDatabase(SpatialiteDatabase): row["Stories of building"], story_number_list ) - # Insert building numbers for each construction material - for material_index in range(3): - building_number = float(str(row[4 + material_index]).replace("-", "0")) - material_id = material_index # Map the column index to the material id - if material_id == 2: - material_id = 4 + # Insert building numbers for total, wooden and non-wooden construction material + for construction_material_index in range(3): + building_number = float( + str(row[4 + construction_material_index]).replace("-", "0") + ) + + # Map the column index to the construction material id + construction_material_id = construction_material_index + + # Re-assign construction_material_id for non-wooden + if construction_material_id == 2: + construction_material_id = 4 + self.insert_building_number( - district_id, building_type_id, material_id, story_number_id, building_number + district_id, + building_type_id, + construction_material_id, + story_number_id, + building_number, ) logger.debug( "Building number for district %s, type %s, %s added" @@ -783,33 +821,42 @@ class JapanDatabase(SpatialiteDatabase): row["Tenure of dwelling"], tenure_type_list ) - # Read the relevant values from the input data - number_dwelling = float(str(row[5]).replace("-", "0")) - number_household = float(str(row[6]).replace("-", "0")) - number_household_member = float(str(row[7]).replace("-", "0")) - rooms_per_dwelling = float(str(row[8]).replace("-", "0")) - tatami_per_dwelling = float(str(row[9]).replace("-", "0")) - floorspace_per_dwelling = float(str(row[10]).replace("-", "0")) - tatami_per_person = float(str(row[11]).replace("-", "0")) - person_per_room = float(str(row[12]).replace("-", "0")) - - # Insert household numbers and related values - self.insert_household_data( - district_id, - building_type_id, - dwelling_type_id, - tenure_type_id, - number_dwelling, - number_household, - number_household_member, - rooms_per_dwelling, - tatami_per_dwelling, - floorspace_per_dwelling, - tatami_per_person, - person_per_room, - ) + # Insert building numbers for total, wooden and non-wooden construction material + for construction_material_index in range(3): + number_dwelling = float(str(row[5]).replace("-", "0")) + rooms_per_dwelling = float(str(row[8]).replace("-", "0")) + tatami_per_dwelling = float(str(row[9]).replace("-", "0")) + floorspace_per_dwelling = float(str(row[10]).replace("-", "0")) + tatami_per_person = float(str(row[11]).replace("-", "0")) + person_per_room = float(str(row[12]).replace("-", "0")) + number_household = float(str(row[6]).replace("-", "0")) + number_household_member = float(str(row[7]).replace("-", "0")) + + # Map the column index to the construction material id + construction_material_id = construction_material_index + + # Re-assign construction material_id for non-wooden + if construction_material_id == 2: + construction_material_id = 4 + + # Insert household numbers and related values + self.insert_household_data( + district_id, + building_type_id, + dwelling_type_id, + tenure_type_id, + construction_material_id, + number_dwelling, + number_household, + number_household_member, + rooms_per_dwelling, + tatami_per_dwelling, + floorspace_per_dwelling, + tatami_per_person, + person_per_room, + ) logger.debug( - "Household number for district %s, type %s, %s, %s added" + "Household data for district %s, type %s, %s, %s added" % ( admin_id, building_type_list[building_type_id],