test_database_queries.py 12.6 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
#!/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
20
import numpy
21
22
import pytest
import shapely
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
from gdecore.configuration import Configuration
from gdecore.database_queries import DatabaseQueries


def test_retrieve_aggregated_source_id(test_db):
    # 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_aggregated_source_id = DatabaseQueries.retrieve_aggregated_source_id(
        "second_source", config.database_gde_tiles, "aggregated_sources"
    )

    assert returned_aggregated_source_id == 2
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


def test_retrieve_all_exposure_entities_of_aggregated_source_id(test_db):
    # 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")
    )

    # aggregated_source_id exists, several exposure entities associated with it
    returned_exposure_entities = (
        DatabaseQueries.retrieve_all_exposure_entities_of_aggregated_source_id(
            2, config.database_gde_tiles, "data_units"
        )
    )

    assert len(returned_exposure_entities) == 2
    assert "ABC" in returned_exposure_entities
    assert "DEF" in returned_exposure_entities

    # aggregated_source_id exists, no exposure entities associated with it
    returned_exposure_entities = (
        DatabaseQueries.retrieve_all_exposure_entities_of_aggregated_source_id(
            3, config.database_gde_tiles, "data_units"
        )
    )

    assert len(returned_exposure_entities) == 0

    # aggregated_source_id does not exist
    returned_exposure_entities = (
        DatabaseQueries.retrieve_all_exposure_entities_of_aggregated_source_id(
            9999, config.database_gde_tiles, "data_units"
        )
    )

    assert len(returned_exposure_entities) == 0
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


def test_get_data_unit_ids_geometries_of_entity_and_occupancy_case(test_db):
    # 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")
    )

    # data units will be retrieved, all with associated geometries
    (
        returned_data_units_ids,
        returned_data_units_geometries,
        returned_data_units_ids_no_geometry,
    ) = DatabaseQueries.get_data_unit_ids_geometries_of_entity_and_occupancy_case(
        "ABC", "residential", 2, config.database_gde_tiles, "data_units"
    )

    expected_data_units_ids = ["ABC_10269", "ABC_10278", "ABC_10277"]
    expected_bounds_west = [15.04625, 15.05455, 15.05455]
    expected_bounds_east = [15.05455, 15.0629, 15.0629]
    expected_bounds_south = [37.475893, 37.475893, 37.4675485]
    expected_bounds_north = [37.48424, 37.48424, 37.475893]

    assert len(returned_data_units_ids) == 3
    assert len(returned_data_units_geometries) == 3
    assert len(returned_data_units_ids_no_geometry) == 0

    for i, data_unit_id in enumerate(expected_data_units_ids):
        assert data_unit_id in returned_data_units_ids

        which = numpy.where(returned_data_units_ids == data_unit_id)[0][0]

        assert returned_data_units_geometries[which].bounds[0] == expected_bounds_west[i]
        assert returned_data_units_geometries[which].bounds[1] == expected_bounds_south[i]
        assert returned_data_units_geometries[which].bounds[2] == expected_bounds_east[i]
        assert returned_data_units_geometries[which].bounds[3] == expected_bounds_north[i]

    # data units will not be found
    (
        returned_data_units_ids,
        returned_data_units_geometries,
        returned_data_units_ids_no_geometry,
    ) = DatabaseQueries.get_data_unit_ids_geometries_of_entity_and_occupancy_case(
        "ABC", "commercial", 2, config.database_gde_tiles, "data_units"
    )

    assert len(returned_data_units_ids) == 0
    assert len(returned_data_units_geometries) == 0
    assert len(returned_data_units_ids_no_geometry) == 0

    # data units will be retrieved, without associated geometries
    (
        returned_data_units_ids,
        returned_data_units_geometries,
        returned_data_units_ids_no_geometry,
    ) = DatabaseQueries.get_data_unit_ids_geometries_of_entity_and_occupancy_case(
        "DEF", "residential", 2, config.database_gde_tiles, "data_units"
    )

    expected_data_unit_id_no_geometry = "DEF_00000"

    assert len(returned_data_units_ids) == 0
    assert len(returned_data_units_geometries) == 0
    assert len(returned_data_units_ids_no_geometry) == 1
    assert returned_data_units_ids_no_geometry[0] == expected_data_unit_id_no_geometry
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


def test_get_OBM_buildings_in_data_unit_by_occupancy_case(test_db):
    # 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")
    )

    expected_columns = ["osm_id", "relation_id", "quadkey", "storeys", "occupancy", "geometry"]

    # Auxiliary, to retrieve the geometry of the data unit
    (
        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"
    )

    # Test cases in which buildings will be retrieved
    geometry = returned_data_units_geometries[
        numpy.where(returned_data_units_ids == "ABC_10269")[0][0]
    ]

    returned_obm_buildings = DatabaseQueries.get_OBM_buildings_in_data_unit_by_occupancy_case(
        "residential",
        geometry,
        config.database_obm_buildings,
        "obm_buildings",
    )

174
    assert returned_obm_buildings.shape[0] == 3
175
176
177
178
179
180
181
    for col_name in expected_columns:
        assert col_name in returned_obm_buildings.columns
    assert 11223344 in returned_obm_buildings["osm_id"].to_numpy()
    assert 22334455 in returned_obm_buildings["osm_id"].to_numpy()
    # Check building whose footprint intersects the data unit but whose centroid is outside
    assert 88990011 not in returned_obm_buildings["osm_id"].to_numpy()

182
183
184
185
186
    # Check that relation_id, quadkey and occupancy are strings
    for column_name in ["relation_id", "quadkey", "occupancy"]:
        for i in range(returned_obm_buildings.shape[0]):
            assert isinstance(returned_obm_buildings[column_name].to_numpy()[i], str)

187
188
189
190
191
192
193
    returned_obm_buildings = DatabaseQueries.get_OBM_buildings_in_data_unit_by_occupancy_case(
        "commercial",
        geometry,
        config.database_obm_buildings,
        "obm_buildings",
    )

194
    assert returned_obm_buildings.shape[0] == 5
195
196
    for col_name in expected_columns:
        assert col_name in returned_obm_buildings.columns
197
198
199
200
201
202
203
204
205

    expected_osm_ids = [33445566, 44556677, 55667788, 66778899, 77889900]
    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["relation_id"].to_numpy()[which_55667788] == "nan") or (
        returned_obm_buildings["relation_id"].to_numpy()[which_55667788] == "None"
    )
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

    # Test case in which no buildings will be retrieved
    geometry = returned_data_units_geometries[
        numpy.where(returned_data_units_ids == "ABC_10277")[0][0]
    ]

    returned_obm_buildings = DatabaseQueries.get_OBM_buildings_in_data_unit_by_occupancy_case(
        "residential",
        geometry,
        config.database_obm_buildings,
        "obm_buildings",
    )

    assert returned_obm_buildings.shape[0] == 0

    # Test case in which the geometry passed is not a Polygon or MultiPolygon
    with pytest.raises(TypeError) as excinfo:
        returned_obm_buildings = (
            DatabaseQueries.get_OBM_buildings_in_data_unit_by_occupancy_case(
                "commercial",
                shapely.geometry.Point(0.0, 0.0),
                config.database_obm_buildings,
                "obm_buildings",
            )
        )
    assert "TypeError" in str(excinfo.type)
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


def test_get_building_classes_of_data_unit(test_db):
    # 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")
    )

    expected_columns = [
        "building_class_name",
        "settlement_type",
        "occupancy_subtype",
        "storeys_min",
        "storeys_max",
        "proportions",
    ]

    # Residential buildings
    returned_building_classes = DatabaseQueries.get_building_classes_of_data_unit(
        "ABC_10269", "residential", 2, config.database_gde_tiles, "data_units_buildings"
    )

    expected_bdg_class_names = [
        "A1/HBET:1-3",
        "A2/HBET:4-6",
258
        "A3/HBET:7-12",
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
        "B1/HBET:1-3",
        "B2/H:4",
    ]

    assert returned_building_classes.shape[0] == len(expected_bdg_class_names)
    for col_name in expected_columns:
        assert col_name in returned_building_classes.columns
    assert round(returned_building_classes["proportions"].sum(), 5) == 1.0
    for name in expected_bdg_class_names:
        assert name in returned_building_classes["building_class_name"].to_numpy()
    assert (
        returned_building_classes[
            returned_building_classes.building_class_name == "A2/HBET:4-6"
        ]["storeys_min"].to_numpy()[0]
        == 4
    )
    assert (
        returned_building_classes[
            returned_building_classes.building_class_name == "A2/HBET:4-6"
        ]["storeys_max"].to_numpy()[0]
        == 6
    )
    assert (
        round(
            returned_building_classes[
                returned_building_classes.building_class_name == "B1/HBET:1-3"
            ]["proportions"].to_numpy()[0],
            2,
        )
        == 0.25
    )

    # Commercial buildings
    returned_building_classes = DatabaseQueries.get_building_classes_of_data_unit(
        "ABC_10269", "commercial", 2, config.database_gde_tiles, "data_units_buildings"
    )

    expected_bdg_class_names = [
        "C1/HBET:1-2",
        "C2/HBET:3-",
        "C3/H:1",
        "C4/HBET:2-3",
        "C5/HBET:1-2",
        "C6/HBET:3-5",
    ]

    assert returned_building_classes.shape[0] == len(expected_bdg_class_names)
    for col_name in expected_columns:
        assert col_name in returned_building_classes.columns
    assert round(returned_building_classes["proportions"].sum(), 5) == 1.0
    for name in expected_bdg_class_names:
        assert name in returned_building_classes["building_class_name"].to_numpy()
    assert (
        returned_building_classes[
            returned_building_classes.building_class_name == "C2/HBET:3-"
        ]["storeys_min"].to_numpy()[0]
        == 3
    )
    assert (
        returned_building_classes[
            returned_building_classes.building_class_name == "C2/HBET:3-"
        ]["storeys_max"].to_numpy()[0]
        == 9999
    )
    assert (
        round(
            returned_building_classes[
                returned_building_classes.building_class_name == "C5/HBET:1-2"
            ]["proportions"].to_numpy()[0],
            2,
        )
        == 0.20
    )

    # Industrial buildings (no entries to be found)
    returned_building_classes = DatabaseQueries.get_building_classes_of_data_unit(
        "ABC_10269", "industrial", 2, config.database_gde_tiles, "data_units_buildings"
    )

    assert returned_building_classes.shape[0] == 0
    for col_name in expected_columns:
        assert col_name in returned_building_classes.columns
    assert round(returned_building_classes["proportions"].sum(), 5) == 0.0