test_to_geosummary.py 4.75 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
#!/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 pandas
import geopandas
from shapely.wkt import loads
from gdeexporter.to_geosummary import export_to_GeoSummary
from gdeexporter.tileexposure import TileExposure


def test_export_to_GeoSummary():
    # User-defined costs and people columns
    cost_cases = {"structural": "total"}
    people_cases = {"day": "day", "night": "night", "transit": "transit"}

    # User-defined input parameters for export_to_GeoSummary
    buildings_to_export = ["OBM", "remainder"]
    output_path = os.path.join(
        os.path.dirname(__file__), "data", "temp_test_export_to_GeoSummary"
    )
    quadkeys_group = "quadkeys_list"
    occupancy_case = "residential"

    # Create temporary directory
    os.mkdir(output_path)

    # Three quadtiles will be created and used to write output file
    # First quadtile has both remainder and OBM buildings, second quadtile has remainder
    # buildings only, third quadtile has OBM buildings only
    quadkeys = ["122010321033023130", "122010321033023121", "122010321033023132"]
    obm_buildings_input = [
        "test_oq_input_OBM_buildings_122010321033023130.csv",
        "",
        "test_oq_input_OBM_buildings_122010321033023132.csv",
    ]
    remainder_buildings_input = [
        "test_oq_input_remainder_buildings_122010321033023130.csv",
        "test_oq_input_remainder_buildings_122010321033023121.csv",
        "",
    ]

    # Expected name of output file
    expected_name_output = "%s_%s_geosummary_tiles.gpkg" % (quadkeys_group, occupancy_case)

    # Expected contents of output file
    expected_output = "test_geosummary_tiles_expected_output.csv"

    # Path to expected results
    expected_results_path = os.path.join(os.path.dirname(__file__), "data")

    # Create TileExposure objects and call export_to_GeoSummary each time
    for i, quadkey in enumerate(quadkeys):
        quadtile = TileExposure(quadkey, cost_cases, people_cases)
        if obm_buildings_input[i] != "":
            quadtile.obm_buildings = pandas.read_csv(
                os.path.join(os.path.dirname(__file__), "data", obm_buildings_input[i])
            )

        if remainder_buildings_input[i] != "":
            quadtile.remainder_buildings = pandas.read_csv(
                os.path.join(os.path.dirname(__file__), "data", remainder_buildings_input[i])
            )

        export_to_GeoSummary(
            quadtile,
            buildings_to_export,
            cost_cases,
            people_cases,
            output_path,
            quadkeys_group,
            occupancy_case,
        )

    # Check that the output file has been created
    assert os.path.exists(os.path.join(output_path, expected_name_output))

    # Check contents of file with quadtile geometries
    returned_output = geopandas.read_file(os.path.join(output_path, expected_name_output))
    expected_output = pandas.read_csv(
        os.path.join(expected_results_path, expected_output),
        sep=";",
        dtype={"quadkey": str},
    )

    assert returned_output.shape[0] == expected_output.shape[0]
    assert returned_output.shape[1] == expected_output.shape[1]

    for j, quadkey in enumerate(expected_output["quadkey"].values):
        assert quadkey in returned_output["quadkey"].values
        filter = returned_output["quadkey"] == quadkey
        for col_name in expected_output.columns:
            if col_name == "geometry":
                returned_bounds = returned_output[filter]["geometry"].values[0].bounds
                expected_bounds = loads(expected_output["geometry"].values[j]).bounds
                for bound in range(4):
                    assert round(returned_bounds[bound], 5) == round(expected_bounds[bound], 5)
            elif col_name == "quadkey":
                continue
            else:
                assert round(returned_output[filter][col_name].values[0], 5) == round(
                    expected_output[col_name].values[j], 5
                )

    # Delete created output files
    os.remove(os.path.join(output_path, expected_name_output))

    # Delete temporary directory
    os.rmdir(output_path)