diff --git a/exposurelib/database.py b/exposurelib/database.py index 7795aebc2164cd53264b4099982286865287192f..6a0851cec141380ef87c8b1aca4a82260c63fe2e 100644 --- a/exposurelib/database.py +++ b/exposurelib/database.py @@ -409,6 +409,30 @@ class AbstractExposure(AbstractDatabase, ABC): """ self.cursor.execute(sql_statement) + def insert_assets(self, assets): + """ + Inserts multiple assets into the `Asset` table. The `entity_id` points to the entity + (either a building or a tile) to which this asset belongs. + + Args: + assets (list): + A list containing for each asset: + entity_id (int): + ID of the entity this asset belongs to (corresponds to `Entity.id`) + taxonomy_id (int): + ID of the taxonomy of the asset (corresponds to `Taxonomy.id`) + number (float): + Number of buildings represented by the asset (can be a fraction of a + building) + structural (float): + Structural value of the asset + night (float): + Number of people in the asset at night time + """ + + for entity_id, taxonomy_id, number, structural, night in assets: + self.insert_asset(entity_id, taxonomy_id, number, structural, night) + def delete_assets(self, entity_id): """ Deletes all assets of an entity. @@ -1995,3 +2019,36 @@ class PostGISExposure(PostGISDatabase, AbstractExposure): """ super().__init__(host, dbname, port, username, password, timeout) + + def insert_assets(self, assets): + """ + Inserts multiple assets into the `Asset` table. The `entity_id` points to the entity + (either a building or a tile) to which this asset belongs. + + Args: + assets (list): + A list containing for each asset: + entity_id (int): + ID of the entity this asset belongs to (corresponds to `Entity.id`) + taxonomy_id (int): + ID of the taxonomy of the asset (corresponds to `Taxonomy.id`) + number (float): + Number of buildings represented by the asset (can be a fraction of a + building) + structural (float): + Structural value of the asset + night (float): + Number of people in the asset at night time + """ + + vals = ",".join( + f"({entity_id},{taxonomy_id},{number},{structural},{night})" + for entity_id, taxonomy_id, number, structural, night in assets + ) + + sql_statement = f""" + INSERT INTO Asset + (entity_id, taxonomy_id, number, structural, night) + VALUES {vals} + """ + self.cursor.execute(sql_statement)