handler.py 9.13 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
#!/usr/bin/env python3

# Copyright (C) 2022:
#   Helmholtz-Zentrum Potsdam Deutsches GeoForschungsZentrum GFZ
#
# This program is free software: you can redistribute it and/or modify it
# under the terms of the GNU Affero General Public License as published by
# the Free Software Foundation, either version 3 of the License, or (at
# your option) any later version.
#
# This program is distributed in the hope that it will be useful, but
# WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Affero
# General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with this program. If not, see http://www.gnu.org/licenses/.

import logging
from gdeexporter.tileexposure import TileExposure
from gdeexporter.database_queries import DatabaseQueries


logger = logging.getLogger()


class ExportHandler:
    """This class handles the main processing activities of the gde-exporter."""

    @staticmethod
    def process_quadkey_occupancy_group(config, aggregated_source_id, group_attributes):
        """
        This function processes a particular quadkey group and occupancy case, both of which are
        passed as arguments under 'group_attributes', to enable parallelisation.

        Args:
            config (Configuration):
                Instance of the gdeexporter.configuration.Configuration class.
            aggregated_source_id (int):
                ID of the source of the aggregated exposure model for which the processing will
                take place.
            group_attributes (tuple of (quadkeys_group, occupancy_case)):
                Tuple with two elements:
                    quadkeys_group:
                        Name of the quadkey group for which the processing will take place. It
                        needs to be a key of the config.quadkeys_to_process dictionary. The
                        content of config.quadkeys_to_process[quadkeys_group] is a list of
                        quadkeys.
                    occupancy_case (str):
                        Occupancy case for which the processing will take place.

        Returns:
            summary_values (dict):
                Dictionary summarising the number of buildings processed for the input quadkey
                group and occupancy case, with the following keys:
                    processed_quadkeys (int):
                        Number of quadkeys processed (includes quadkeys with no buildings).
                    OBM_buildings (int):
                        Number of GDE-processed OBM buildings.
                    aggregated_buildings (float):
                        Number of aggregated buildings.
                    remainder_buildings (float):
                        Number of remainder buildings.
                    total_buildings (float):
                        Number of total buildings (remainder plus OBM).
        """

        quadkeys_group = group_attributes[0]
        occupancy_case = group_attributes[1]

        logger.info(
            "Processing of %s quadkeys from group '%s' and occupancy case '%s' has started"
            % (len(config.quadkeys_to_process[quadkeys_group]), quadkeys_group, occupancy_case)
        )

        summary_values = {}
        summary_values["processed_quadkeys"] = 0
        summary_values["OBM_buildings"] = 0
        summary_values["aggregated_buildings"] = 0.0
        summary_values["remainder_buildings"] = 0.0
        summary_values["total_buildings"] = 0.0

        for quadkey in config.quadkeys_to_process[quadkeys_group]:
            quadtile = TileExposure(quadkey, config.cost_cases, config.people_cases)

            if config.geographic_selection["selection_mode"].lower() == "data_unit_id":
                data_unit_ids = [quadkeys_group]
            else:
                data_unit_ids = DatabaseQueries.retrieve_data_unit_ids(
                    quadkey,
                    aggregated_source_id,
                    config.exposure_entities_to_run,
                    occupancy_case,
                    config.database_gde_tiles,
                    "data_unit_tiles",
                )

            for data_unit_id in data_unit_ids:
                # Retrieve building classes associated with this data unit, occupancy case and
                # aggregated source ID
                building_classes = DatabaseQueries.get_building_classes_of_data_unit(
                    data_unit_id,
                    occupancy_case,
                    aggregated_source_id,
                    config.database_gde_tiles,
                    "data_units_buildings",
                )

                exposure_entity_code = data_unit_id[:3]
                # Retrieve cost assumptions
                cost_assumptions = DatabaseQueries.get_exposure_entities_costs_assumptions(
                    config.cost_cases,
                    exposure_entity_code,
                    occupancy_case,
                    aggregated_source_id,
                    config.database_gde_tiles,
                    "exposure_entities_costs_assumptions",
                )
                # Retrieve distribution of people at different times of the day
                people_distribution = (
                    DatabaseQueries.get_exposure_entities_population_time_distribution(
                        config.people_cases,
                        exposure_entity_code,
                        occupancy_case,
                        aggregated_source_id,
                        config.database_gde_tiles,
                        "exposure_entities_population_time_distribution",
                    )
                )

                # Retrieve number of aggregated, OBM and remainder buildings in the tile
                (
                    number_aggregated,
                    number_obm,
                    number_remainder,
                ) = DatabaseQueries.get_numbers_buildings_for_data_unit_tile(
                    quadkey,
                    aggregated_source_id,
                    occupancy_case,
                    data_unit_id,
                    config.database_gde_tiles,
                    "data_unit_tiles",
                )
                if number_aggregated < -1.0 or number_obm < -1.0 or number_remainder < -1.0:
                    logger.error(
                        "get_numbers_buildings_for_data_unit_tile could not retrieve number "
                        "of aggregated, remainder and OBM buildings for quadkey = '%s' and "
                        "data unit ID = '%s'" % (quadkey, data_unit_id)
                    )

                # Append aggregated buildings to quadtile.aggregated_buildings
                if number_aggregated > 1e-6:  # If smaller, consider equal to zero
                    quadtile.append_lumped_buildings(
                        "aggregated_buildings",
                        building_classes,
                        number_aggregated,
                        cost_assumptions,
                        people_distribution,
                        data_unit_id,
                    )

                # Append remainder buildings to quadtile.remainder_buildings
                if number_remainder > 1e-6:  # If smaller, consider equal to zero
                    quadtile.append_lumped_buildings(
                        "remainder_buildings",
                        building_classes,
                        number_remainder,
                        cost_assumptions,
                        people_distribution,
                        data_unit_id,
                    )

                # Retrieve OBM buildings
                obm_buildings, obm_geometries = DatabaseQueries.get_GDE_buildings(
                    quadkey,
                    data_unit_id,
                    occupancy_case,
                    aggregated_source_id,
                    config.export_OBM_footprints,
                    config.database_gde_tiles,
                    "gde_buildings",
                )

                if obm_buildings.shape[0] > 0:
                    # Append OBM buildings to quadtile.obm_buildings
                    quadtile.append_OBM_buildings(
                        obm_buildings,
                        building_classes,
                        cost_assumptions,
                        people_distribution,
                        data_unit_id,
                    )

                    # Append obm_geometries to quadtile.obm_buildings_geometries (dictionary)
                    quadtile.obm_buildings_geometries.update(obm_geometries)

            # Add to summary values
            summary_values["aggregated_buildings"] += (
                quadtile.aggregated_buildings["number"].to_numpy().sum()
            )
            summary_values["remainder_buildings"] += (
                quadtile.remainder_buildings["number"].to_numpy().sum()
            )
            summary_values["total_buildings"] += (
                quadtile.total_buildings["number"].to_numpy().sum()
            )
            summary_values["OBM_buildings"] += quadtile.obm_buildings["number"].to_numpy().sum()

        summary_values["processed_quadkeys"] += len(config.quadkeys_to_process[quadkeys_group])

        return summary_values