File: test_database.py

package info (click to toggle)
python-pyproj 3.7.1-2
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 1,720 kB
  • sloc: python: 13,468; sh: 273; makefile: 90
file content (270 lines) | stat: -rw-r--r-- 8,066 bytes parent folder | download | duplicates (5)
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
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
import pytest

from pyproj.aoi import AreaOfInterest, BBox
from pyproj.database import (
    Unit,
    get_authorities,
    get_codes,
    get_database_metadata,
    get_units_map,
    query_crs_info,
    query_utm_crs_info,
)
from pyproj.enums import PJType


def test_backwards_compatible_import_paths():
    from pyproj import (  # noqa: F401 pylint: disable=unused-import
        get_authorities,
        get_codes,
        get_units_map,
    )


def test_units_map__default():
    units_map = get_units_map()
    assert isinstance(units_map["metre"], Unit)
    assert units_map["metre"].name == "metre"
    assert units_map["metre"].auth_name == "EPSG"
    assert units_map["metre"].code == "9001"
    assert units_map["metre"].category == "linear"
    assert units_map["metre"].conv_factor == 1
    assert units_map["metre"].proj_short_name == "m"
    assert not units_map["metre"].deprecated
    any_deprecated = False
    for item in units_map.values():
        any_deprecated = any_deprecated or item.deprecated
    assert not any_deprecated


@pytest.mark.parametrize(
    "category",
    [
        "linear",
        "linear_per_time",
        "angular",
        "angular_per_time",
        "scale",
        "scale_per_time",
        "time",
    ],
)
def test_units_map__category(category):
    units_map = get_units_map(category=category)
    assert len(units_map) > 1
    for item in units_map.values():
        assert item.category == category


@pytest.mark.parametrize("auth_name", ["EPSG", "PROJ"])
def test_units_map__auth_name(auth_name):
    units_map = get_units_map(auth_name=auth_name)
    assert len(units_map) > 1
    for item in units_map.values():
        assert item.auth_name == auth_name


@pytest.mark.parametrize("deprecated", ["zzz", "True", True])
def test_units_map__deprecated(deprecated):
    units_map = get_units_map(allow_deprecated=deprecated)
    assert len(units_map) > 1
    any_deprecated = False
    for item in units_map.values():
        any_deprecated = any_deprecated or item.deprecated
    assert any_deprecated


@pytest.mark.parametrize("auth_name, category", [(None, 1), (1, None)])
def test_units_map__invalid(auth_name, category):
    with pytest.raises(TypeError):
        get_units_map(auth_name=auth_name, category=category)


def test_get_authorities():
    assert "EPSG" in get_authorities()


@pytest.mark.parametrize(
    "auth, pj_type, deprecated",
    [
        ("IGNF", PJType.ELLIPSOID, False),
        ("EPSG", PJType.CRS, False),
        ("EPSG", PJType.CRS, True),
        ("PROJ", PJType.ELLIPSOID, False),
        ("IGNF", "ELLIPSOID", False),
        ("EPSG", "CRS", False),
        ("EPSG", "crs", True),
        ("PROJ", "ellipsoid", False),
    ],
)
def test_get_codes(auth, pj_type, deprecated):
    assert get_codes(auth, pj_type, deprecated)


@pytest.mark.parametrize(
    "auth, pj_type",
    [("blob", "BOUND_CRS"), ("PROJ", PJType.BOUND_CRS), ("ITRF", PJType.BOUND_CRS)],
)
def test_get_codes__empty(auth, pj_type):
    assert not get_codes(auth, pj_type)


def test_get_codes__derived_projected_crs():
    assert not get_codes("EPSG", PJType.DERIVED_PROJECTED_CRS)


def test_get_codes__invalid_auth():
    with pytest.raises(TypeError):
        get_codes(123, PJType.BOUND_CRS)


def test_get_codes__invalid_code():
    with pytest.raises(ValueError):
        get_codes("ITRF", "frank")


@pytest.mark.parametrize(
    "auth, pj_type, deprecated",
    [
        (None, None, False),
        ("EPSG", PJType.PROJECTED_CRS, False),
        ("EPSG", PJType.PROJECTED_CRS, True),
        ("IGNF", [PJType.GEOGRAPHIC_3D_CRS, PJType.GEOGRAPHIC_2D_CRS], False),
        ("EPSG", "PROJECTED_CRS", False),
        ("EPSG", "Projected_Crs", True),
    ],
)
def test_query_crs_info(auth, pj_type, deprecated):
    crs_info_list = query_crs_info(auth, pj_type, allow_deprecated=deprecated)
    assert crs_info_list
    any_deprecated = any(crs_info.deprecated for crs_info in crs_info_list)
    if deprecated:
        assert any_deprecated
    else:
        assert not any_deprecated


def test_query_crs_info__derived_projected_crs():
    assert not query_crs_info(pj_types=PJType.DERIVED_PROJECTED_CRS)


@pytest.mark.parametrize(
    "auth, pj_type",
    [
        ("blob", "BOUND_CRS"),
        ("IGNF", PJType.ELLIPSOID),
        ("PROJ", PJType.BOUND_CRS),
        ("ITRF", PJType.BOUND_CRS),
    ],
)
def test_query_crs_info__empty(auth, pj_type):
    assert not query_crs_info(auth, pj_type)


def test_query_crs_info__invalid_auth():
    with pytest.raises(TypeError):
        query_crs_info(123, PJType.BOUND_CRS)


def test_query_crs_info__invalid_code():
    with pytest.raises(ValueError):
        query_crs_info("ITRF", "frank")


def test_query_crs_info__aoi():
    aoi = BBox(west=-40, south=50, east=-20, north=70)
    crs_info_list = query_crs_info(
        auth_name="ESRI",
        pj_types=PJType.PROJECTED_CRS,
        area_of_interest=AreaOfInterest(
            west_lon_degree=aoi.west,
            south_lat_degree=aoi.south,
            east_lon_degree=aoi.east,
            north_lat_degree=aoi.north,
        ),
    )
    assert crs_info_list
    not_contains_present = False
    for crs_info in crs_info_list:
        bbox = BBox(*crs_info.area_of_use.bounds)
        assert bbox.intersects(aoi)
        assert crs_info.auth_name == "ESRI"
        assert crs_info.type == PJType.PROJECTED_CRS
        assert not crs_info.deprecated
        if not bbox.contains(aoi):
            not_contains_present = True
    assert not_contains_present


def test_query_crs_info__aoi_contains():
    aoi = BBox(west=-40, south=50, east=-20, north=70)
    crs_info_list = query_crs_info(
        auth_name="IGNF",
        pj_types=[PJType.PROJECTED_CRS],
        area_of_interest=AreaOfInterest(
            west_lon_degree=aoi.west,
            south_lat_degree=aoi.south,
            east_lon_degree=aoi.east,
            north_lat_degree=aoi.north,
        ),
        contains=True,
    )
    assert crs_info_list
    for crs_info in crs_info_list:
        assert BBox(*crs_info.area_of_use.bounds).contains(aoi)
        assert crs_info.auth_name == "IGNF"
        assert crs_info.type == PJType.PROJECTED_CRS
        assert not crs_info.deprecated


@pytest.mark.parametrize("datum_name", ["WGS 84", "WGS84", "NAD27", "NAD83"])
def test_query_utm_crs_info__aoi_datum_name(datum_name):
    aoi = BBox(west=-93.581543, south=42.032974, east=-93.581543, north=42.032974)
    crs_info_list = query_utm_crs_info(
        datum_name=datum_name,
        area_of_interest=AreaOfInterest(
            west_lon_degree=aoi.west,
            south_lat_degree=aoi.south,
            east_lon_degree=aoi.east,
            north_lat_degree=aoi.north,
        ),
    )
    assert len(crs_info_list) == 1
    crs_info = crs_info_list[0]
    bbox = BBox(*crs_info.area_of_use.bounds)
    assert bbox.intersects(aoi)
    assert "UTM zone" in crs_info.name
    assert datum_name.replace(" ", "") in crs_info.name.replace(" ", "")
    assert crs_info.auth_name == "EPSG"
    assert crs_info.type == PJType.PROJECTED_CRS
    assert not crs_info.deprecated


def test_query_utm_crs_info__aoi_contains():
    aoi = BBox(west=41, south=50, east=42, north=51)
    crs_info_list = query_utm_crs_info(
        area_of_interest=AreaOfInterest(
            west_lon_degree=aoi.west,
            south_lat_degree=aoi.south,
            east_lon_degree=aoi.east,
            north_lat_degree=aoi.north,
        ),
        contains=True,
    )
    assert crs_info_list
    for crs_info in crs_info_list:
        assert BBox(*crs_info.area_of_use.bounds).contains(aoi)
        assert "UTM zone" in crs_info.name
        assert crs_info.auth_name == "EPSG"
        assert crs_info.type == PJType.PROJECTED_CRS
        assert not crs_info.deprecated


def test_get_database_metadata():
    epsg_version = get_database_metadata("EPSG.VERSION")
    assert epsg_version
    assert isinstance(epsg_version, str)


def test_get_database_metadata__invalid():
    assert get_database_metadata("doesnotexist") is None