test_processor.py 16 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#!/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 os
import logging
import numpy
22
import pandas
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
from gdecore.processor import GDEProcessor
from gdecore.configuration import Configuration
from gdecore.database_queries import DatabaseQueries

logger = logging.getLogger()


def test_post_process_obm_relations(test_db):
    # Preliminary steps to retrieve the relevant data from the test database
    # Database connection (the Configuration class will define the credentials based on whether
    # the code is running in the CI or locally)
    config = Configuration(
        os.path.join(os.path.dirname(__file__), "data", "config_for_testing_good.yml")
    )

    (
        returned_data_units_ids,
        returned_data_units_geometries,
        _,
    ) = DatabaseQueries.get_data_unit_ids_geometries_of_entity_and_occupancy_case(
        "ABC", "residential", 2, config.database_gde_tiles, "data_units"
    )  # auxiliary, to retrieve the geometry of the data unit

    geometry = returned_data_units_geometries[
        numpy.where(returned_data_units_ids == "ABC_10269")[0][0]
    ]

    # Group of residential buildings that do not belong to relations
    raw_obm_buildings = DatabaseQueries.get_OBM_buildings_in_data_unit_by_occupancy_case(
        "residential",
        geometry,
        config.database_obm_buildings,
        "obm_buildings",
    )

    returned_obm_buildings = GDEProcessor.post_process_obm_relations(raw_obm_buildings)

60
    assert returned_obm_buildings.shape[0] == 3
61
62
    assert 11223344 in returned_obm_buildings["osm_id"].to_numpy()
    assert 22334455 in returned_obm_buildings["osm_id"].to_numpy()
63
    assert 99001122 in returned_obm_buildings["osm_id"].to_numpy()
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

    # Group of commercial buildings, some in relations, some not
    raw_obm_buildings = DatabaseQueries.get_OBM_buildings_in_data_unit_by_occupancy_case(
        "commercial",
        geometry,
        config.database_obm_buildings,
        "obm_buildings",
    )

    returned_obm_buildings = GDEProcessor.post_process_obm_relations(raw_obm_buildings)

    assert returned_obm_buildings.shape[0] == 3
    expected_osm_ids = [55667788, -101010, -202020]
    for expected_id in expected_osm_ids:
        assert expected_id in returned_obm_buildings["osm_id"].to_numpy()

    which_55667788 = numpy.where(returned_obm_buildings["osm_id"].to_numpy() == 55667788)[0][0]
    assert numpy.isnan(returned_obm_buildings["storeys"].to_numpy()[which_55667788])
    assert returned_obm_buildings["occupancy"].to_numpy()[which_55667788] == "RES3"

    # Both parts in the same tile, both have number of storeys, both COM3
    which_101010 = numpy.where(returned_obm_buildings["osm_id"].to_numpy() == -101010)[0][0]
    assert returned_obm_buildings["quadkey"].to_numpy()[which_101010] == "122010321033023130"
    assert int(returned_obm_buildings["storeys"].to_numpy()[which_101010]) == 3
    assert returned_obm_buildings["occupancy"].to_numpy()[which_101010] == "COM3"

    # Parts are in different tiles, one has bumber of storeys, both COM2
    which_202020 = numpy.where(returned_obm_buildings["osm_id"].to_numpy() == -202020)[0][0]
    assert returned_obm_buildings["quadkey"].to_numpy()[which_202020] == "122010321033023132"
    assert int(returned_obm_buildings["storeys"].to_numpy()[which_202020]) == 4
    assert returned_obm_buildings["occupancy"].to_numpy()[which_202020] == "COM2"


def test_identify_unique_quadkey_L18(test_db):
    # Tested within test_post_process_obm_relations()
    pass


def test_select_max_of_array():
    # All elements are NaNs
    test_array = numpy.array([numpy.nan, numpy.nan, numpy.nan])
    assert numpy.isnan(GDEProcessor._select_max_of_array(test_array))

    # Some elements are NaNs
    test_array = numpy.array([2, numpy.nan, 7])
    assert GDEProcessor._select_max_of_array(test_array) == 7

    # No elements are NaNs
    test_array = numpy.array([2, 15, 7])
    assert GDEProcessor._select_max_of_array(test_array) == 15


def test_ensure_unique_occupancy():
    # Tested within test_post_process_obm_relations()
    pass
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
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410


def test_assign_building_classes_to_obm_buildings(test_db):
    """The test cases cover a range of combinations of outcomes from the two rules that are used
    to narrow down the building classes that are assigned to an OBM building. The first rule
    narrows down as a function of the number of storeys, the second rule narrows down as a
    function of occupancy details when the occupancy case is commercial and the occupancy falls
    within a set of cases.

    The test cases are the following:

    - Test Case 1: Residential building with 4 storeys:
      - The number of storeys first narrows down the possibilities.
      - The second rule does not apply because it is residential.
      - Two building classes are assigned at the end.
    - Test Case 2: Residential building with unknown number of storeys:
      - No narrowing down with the first rule, as the number of storeys is unknown
      - The second rule does not apply because it is residential.
      - As no narrowing down occurs, all building classes are assigned.
    - Test Case 3: Residential building with 15 storeys:
      - The number of storeys results in no possible classes left (all classes are incompatible)
      - The second rule does not apply because it is residential.
      - As all building classes are incompatible, all building classes are assigned.
    - Test Case 4: Commercial "RES3" building with unknown number of storeys:
      - No narrowing down with the first rule, as the number of storeys is unknown
      - The second rule leads to narrowing down.
      - Two building classes are assigned at the end.
    - Test Case 5: Commercial "COM3" building (from relation) with 3 storeys:
      - The number of storeys first narrows down the possibilities.
      - The second rule leads to further narrowing down.
      - One building class is assigned at the end.
    - Test Case 6: Commercial "COM2" building (from relation) with 4 storeys:
      - The number of storeys first narrows down the possibilities.
      - The second rule leads to further narrowing down but results in no classes left.
      - As all building classes are incompatible, all building classes are assigned.
    """

    # Database connection (the Configuration class will define the credentials based on whether
    # the code is running in the CI or locally)
    config = Configuration(
        os.path.join(os.path.dirname(__file__), "data", "config_for_testing_good.yml")
    )

    (
        returned_data_units_ids,
        returned_data_units_geometries,
        _,
    ) = DatabaseQueries.get_data_unit_ids_geometries_of_entity_and_occupancy_case(
        "ABC", "residential", 2, config.database_gde_tiles, "data_units"
    )  # auxiliary, to retrieve the geometry of the data unit

    geometry = returned_data_units_geometries[
        numpy.where(returned_data_units_ids == "ABC_10269")[0][0]
    ]

    # Group of residential buildings that do not belong to relations
    raw_obm_buildings = DatabaseQueries.get_OBM_buildings_in_data_unit_by_occupancy_case(
        "residential",
        geometry,
        config.database_obm_buildings,
        "obm_buildings",
    )

    obm_buildings = GDEProcessor.post_process_obm_relations(raw_obm_buildings)

    data_unit_building_classes = DatabaseQueries.get_building_classes_of_data_unit(
        "ABC_10269", "residential", 2, config.database_gde_tiles, "data_units_buildings"
    )

    returned_obm_buildings_building_classes = (
        GDEProcessor.assign_building_classes_to_obm_buildings(
            obm_buildings, data_unit_building_classes, "residential"
        )
    )

    assert len(returned_obm_buildings_building_classes.keys()) == 3

    # Test Case 1: Residential building with 4 storeys
    case_11223344 = returned_obm_buildings_building_classes[11223344]
    assert case_11223344.shape[0] == 2

    expected_class_names = ["A2/HBET:4-6", "B2/H:4"]
    expected_proportions = [0.667, 0.333]
    for i in range(len(expected_class_names)):
        assert (
            round(
                case_11223344[case_11223344.building_class_name == expected_class_names[i]][
                    "proportions"
                ].to_numpy()[0],
                3,
            )
            == expected_proportions[i]
        )

    # Test Case 2: Residential building with unknown number of storeys (all classes returned)
    case_22334455 = returned_obm_buildings_building_classes[22334455]
    assert case_22334455.shape[0] == data_unit_building_classes.shape[0]
    for bdg_class_name in data_unit_building_classes["building_class_name"].to_numpy():
        assert bdg_class_name in case_22334455["building_class_name"].to_numpy()

    # Test Case 3: Residential building with 15 storeys (returns all classes)
    case_99001122 = returned_obm_buildings_building_classes[99001122]
    assert case_99001122.shape[0] == data_unit_building_classes.shape[0]
    for bdg_class_name in data_unit_building_classes["building_class_name"].to_numpy():
        assert bdg_class_name in case_99001122["building_class_name"].to_numpy()

    # Group of commercial buildings, some in relations, some not
    raw_obm_buildings = DatabaseQueries.get_OBM_buildings_in_data_unit_by_occupancy_case(
        "commercial",
        geometry,
        config.database_obm_buildings,
        "obm_buildings",
    )

    obm_buildings = GDEProcessor.post_process_obm_relations(raw_obm_buildings)

    data_unit_building_classes = DatabaseQueries.get_building_classes_of_data_unit(
        "ABC_10269", "commercial", 2, config.database_gde_tiles, "data_units_buildings"
    )

    returned_obm_buildings_building_classes = (
        GDEProcessor.assign_building_classes_to_obm_buildings(
            obm_buildings, data_unit_building_classes, "commercial"
        )
    )

    assert len(returned_obm_buildings_building_classes.keys()) == 3

    # Test Case 4: Commercial building with unknown number of storeys, "RES3"
    case_55667788 = returned_obm_buildings_building_classes[55667788]
    assert case_55667788.shape[0] == 2

    expected_class_names = ["C1/HBET:1-2", "C2/HBET:3-"]
    expected_proportions = [0.286, 0.714]
    for i in range(len(expected_class_names)):
        assert (
            round(
                case_55667788[case_55667788.building_class_name == expected_class_names[i]][
                    "proportions"
                ].to_numpy()[0],
                3,
            )
            == expected_proportions[i]
        )

    # Test Case 5: Commercial building (from relation) with 3 storeys, "COM3"
    case_101010 = returned_obm_buildings_building_classes[-101010]
    assert case_101010.shape[0] == 1
    assert (
        round(
            case_101010[case_101010.building_class_name == "C6/HBET:3-5"][
                "proportions"
            ].to_numpy()[0],
            3,
        )
        == 1.0
    )

    # Test Case 6: Commercial building (from relation) with 4 storeys, "COM2"
    case_202020 = returned_obm_buildings_building_classes[-202020]
    assert case_202020.shape[0] == data_unit_building_classes.shape[0]
    for bdg_class_name in data_unit_building_classes["building_class_name"].to_numpy():
        assert bdg_class_name in case_202020["building_class_name"].to_numpy()


def test_narrow_down_by_storeys():

    building_class_name = ["A", "B", "C", "D"]
    settlement_type = ["urban", "urban", "urban", "urban"]
    occupancy_subtype = ["all", "all", "all", "all"]
    storeys_min = [1, 2, 5, 6]
    storeys_max = [3, 4, 5, 8]
    proportions = [0.5, 0.25, 0.20, 0.05]

    building_classes = pandas.DataFrame(
        {
            "building_class_name": building_class_name,
            "settlement_type": settlement_type,
            "occupancy_subtype": occupancy_subtype,
            "storeys_min": storeys_min,
            "storeys_max": storeys_max,
            "proportions": proportions,
        }
    )

    # One class possible
    returned_obm_building_classes = GDEProcessor.narrow_down_by_storeys(5, building_classes)

    assert returned_obm_building_classes.shape[0] == 1
    assert (
        round(
            returned_obm_building_classes[
                returned_obm_building_classes.building_class_name == "C"
            ]["proportions"].to_numpy()[0],
            3,
        )
        == 1.0
    )

    # Two classes possible
    returned_obm_building_classes = GDEProcessor.narrow_down_by_storeys(3, building_classes)

    assert returned_obm_building_classes.shape[0] == 2
    expected_class_names = ["A", "B"]
    expected_proportions = [0.667, 0.333]
    for i in range(len(expected_class_names)):
        assert (
            round(
                returned_obm_building_classes[
                    returned_obm_building_classes.building_class_name == expected_class_names[i]
                ]["proportions"].to_numpy()[0],
                3,
            )
            == expected_proportions[i]
        )

    # No class possible
    returned_obm_building_classes = GDEProcessor.narrow_down_by_storeys(12, building_classes)

    assert returned_obm_building_classes.shape[0] == 0

    # Input numpy.nan (all classes are returned)
    returned_obm_building_classes = GDEProcessor.narrow_down_by_storeys(
        numpy.nan, building_classes
    )

    assert returned_obm_building_classes.shape[0] == building_classes.shape[0]


def test_narrow_down_by_commercial_occupancy_details():

    building_class_name = ["A", "B", "C", "D"]
    settlement_type = ["urban", "urban", "urban", "urban"]
    occupancy_subtype = ["Offices", "Trade", "Offices", "Hotels"]
    storeys_min = [1, 2, 5, 6]
    storeys_max = [3, 4, 5, 9999]
    proportions = [0.5, 0.25, 0.20, 0.05]

    building_classes = pandas.DataFrame(
        {
            "building_class_name": building_class_name,
            "settlement_type": settlement_type,
            "occupancy_subtype": occupancy_subtype,
            "storeys_min": storeys_min,
            "storeys_max": storeys_max,
            "proportions": proportions,
        }
    )

    # One class possible
    returned_obm_building_classes = GDEProcessor.narrow_down_by_commercial_occupancy_details(
        "RES3", building_classes
    )

    assert returned_obm_building_classes.shape[0] == 1
    assert (
        round(
            returned_obm_building_classes[
                returned_obm_building_classes.building_class_name == "D"
            ]["proportions"].to_numpy()[0],
            3,
        )
        == 1.0
    )

    # Two classes possible
    returned_obm_building_classes = GDEProcessor.narrow_down_by_commercial_occupancy_details(
        "COM3", building_classes
    )

    assert returned_obm_building_classes.shape[0] == 2
    expected_class_names = ["A", "C"]
    expected_proportions = [0.714, 0.286]
    for i in range(len(expected_class_names)):
        assert (
            round(
                returned_obm_building_classes[
                    returned_obm_building_classes.building_class_name == expected_class_names[i]
                ]["proportions"].to_numpy()[0],
                3,
            )
            == expected_proportions[i]
        )

    # No filtering possible (therefore, return them all)
    returned_obm_building_classes = GDEProcessor.narrow_down_by_commercial_occupancy_details(
        "ALL", building_classes
    )

    assert returned_obm_building_classes.shape[0] == building_classes.shape[0]
    for bdg_class_name in building_classes["building_class_name"].to_numpy():
        assert bdg_class_name in returned_obm_building_classes["building_class_name"].to_numpy()