forked from symerio/pgeocode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest_pgeocode.py
259 lines (203 loc) · 7.65 KB
/
test_pgeocode.py
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
# License 3-clause BSD
#
# Authors: Roman Yurchak <[email protected]>
import os
import urllib
import json
from zipfile import ZipFile
from io import BytesIO
import numpy as np
import pandas as pd
import pytest
from numpy.testing import assert_allclose, assert_array_equal
import pgeocode
from pgeocode import GeoDistance, Nominatim, haversine_distance
from pgeocode import _open_extract_url
@pytest.fixture
def temp_dir(tmpdir, monkeypatch):
monkeypatch.setattr(pgeocode, "STORAGE_DIR", str(tmpdir))
yield str(tmpdir)
def _normalize_str(x):
if x is np.nan:
return x
else:
return x.lower()
@pytest.mark.parametrize(
"country, pc1, location1, pc2, location2, distance12",
[
("FR", "91120", "Palaiseau", "67000", "Strasbourg", 400),
("GB", "WC2N 5DU", "London", "BT1 5GS", "Belfast", 518),
# ('AR', 'c1002', 'Buenos-Aires', '62091', 'Rio-Negro', 965), known failure # noqa
("AU", "6837", "Perth", "3000", "melbourne", 2722),
("AU", "6837", "Perth", "0221", "Barton", 3089),
("US", "60605", "Chicago", "94103", "San Francisco", 2984),
("CA", "M5R 1X8", "Toronto", "H2Z 1A7", "Montreal", 503),
("IE", "D01 R2PO", "Dublin", "T12 RW26", "Cork", 219),
],
)
def test_countries(country, pc1, location1, pc2, location2, distance12):
if country == "IE":
pytest.xfail("TODO: Investigate failure for IE")
nomi = Nominatim(country)
res = nomi.query_postal_code(pc1)
assert isinstance(res, pd.Series)
assert _normalize_str(location1) in _normalize_str(res.place_name)
assert "country_code" in res.index
res = nomi.query_postal_code(pc2)
assert isinstance(res, pd.Series)
assert _normalize_str(location2) in _normalize_str(res.place_name)
gdist = GeoDistance(country)
dist = gdist.query_postal_code(pc1, pc2)
assert isinstance(dist, float)
assert dist == pytest.approx(distance12, abs=5)
def test_download_dataset(temp_dir):
assert not os.path.exists(os.path.join(temp_dir, "FR.txt"))
nomi = Nominatim("fr")
# the data file was downloaded
assert os.path.exists(os.path.join(temp_dir, "FR.txt"))
res = nomi.query_postal_code("77160")
nomi2 = Nominatim("fr")
res2 = nomi.query_postal_code("77160")
assert_array_equal(nomi._data.columns, nomi2._data.columns)
assert_array_equal(nomi._data_frame.columns, nomi2._data_frame.columns)
assert nomi._data.shape == nomi._data.shape
assert nomi._data_frame.shape == nomi._data_frame.shape
assert len(res.place_name.split(",")) > 1
assert len(res2.place_name.split(",")) > 1
def test_nominatim_query_postal_code():
nomi = Nominatim("fr")
res = nomi.query_postal_code(["91120"])
assert isinstance(res, pd.DataFrame)
assert res.shape[0] == 1
assert res.place_name.values[0] == "Palaiseau"
res = nomi.query_postal_code("91120")
assert isinstance(res, pd.Series)
assert res.place_name == "Palaiseau"
res = nomi.query_postal_code(["33625", "31000", "99999"])
assert res.shape[0] == 3
assert not np.isfinite(res.iloc[2].latitude)
def test_nominatim_query_postal_code_multiple():
nomi = Nominatim("de", unique=False)
expected_places = [
"Wellen",
"Groß Rodensleben",
"Irxleben",
"Eichenbarleben",
"Klein Rodensleben",
"Niederndodeleben",
"Hohendodeleben",
"Ochtmersleben",
]
res = nomi.query_postal_code("39167")
assert isinstance(res, pd.DataFrame)
assert res.shape[0] == len(expected_places)
for place in res.place_name.values:
assert place in expected_places
@pytest.mark.slow
@pytest.mark.parametrize("country", pgeocode.COUNTRIES_VALID)
def test_nominatim_all_countries(country):
nomi = Nominatim(country)
res = nomi.query_postal_code("00000")
assert isinstance(res, pd.Series)
def test_nominatim_distance_postal_code():
gdist = GeoDistance("fr")
dist = gdist.query_postal_code("91120", "91120")
assert dist == 0
# distance between Palaiseau and Strasbourg
dist = gdist.query_postal_code("91120", "67000")
assert isinstance(dist, float)
assert dist == pytest.approx(400, abs=4.5)
assert np.isfinite(dist).all()
dist = gdist.query_postal_code("91120", ["31000", "67000"])
assert isinstance(dist, np.ndarray)
assert dist.shape == (2,)
assert np.isfinite(dist).all()
dist = gdist.query_postal_code(["31000", "67000"], "91120")
assert isinstance(dist, np.ndarray)
assert dist.shape == (2,)
assert np.isfinite(dist).all()
dist = gdist.query_postal_code(["31000", "67000"], ["67000", "31000"])
assert isinstance(dist, np.ndarray)
assert dist.shape == (2,)
assert np.diff(dist)[0] == 0
assert np.isfinite(dist).all()
def test_haversine_distance():
try:
from geopy.distance import great_circle
except ImportError:
raise pytest.skip("scikit-learn not installed")
rng = np.random.RandomState(42)
N = 100
x = rng.rand(N, 2) * 80
y = x * rng.rand(N, 2)
d_ref = np.zeros(N)
for idx, (x_coord, y_coord) in enumerate(zip(x, y)):
d_ref[idx] = great_circle(x_coord, y_coord).km
d_pred = haversine_distance(x, y)
# same distance +/- 3 km
assert_allclose(d_ref, d_pred, atol=3)
def test_open_extract_url(httpserver):
download_url = "/fr.txt"
# check download of uncompressed files
httpserver.expect_oneshot_request(download_url).respond_with_json({"a": 1})
with _open_extract_url(httpserver.url_for(download_url), "fr") as fh:
assert json.loads(fh.read()) == {"a": 1}
httpserver.check_assertions()
# check download of zipped files
# Create an in-memory zip file
answer = b"a=1"
with BytesIO() as fh:
with ZipFile(fh, "w") as fh_zip:
with fh_zip.open("FR.txt", "w") as fh_inner:
fh_inner.write(answer)
fh.seek(0)
res = fh.read()
download_url = "/fr.zip"
httpserver.expect_oneshot_request(download_url).respond_with_data(res)
with _open_extract_url(httpserver.url_for(download_url), "fr") as fh:
assert fh.read() == answer
@pytest.mark.parametrize(
"download_url",
[
"https://download.geonames.org/export/zip/{country}.zip",
"https://symerio.github.io/postal-codes-data/data/"
"geonames/{country}.txt",
],
ids=["geonames", "gitlab-pages"],
)
def test_cdn(temp_dir, monkeypatch, download_url):
monkeypatch.setattr(pgeocode, "DOWNLOAD_URL", [download_url])
assert not os.path.exists(os.path.join(temp_dir, "IE.txt"))
Nominatim("IE")
# the data file was downloaded
assert os.path.exists(os.path.join(temp_dir, "IE.txt"))
def test_url_returns_404(httpserver, monkeypatch, temp_dir):
download_url = "/fr.gzip"
httpserver.expect_oneshot_request(download_url).respond_with_data(
"", status=404
)
monkeypatch.setattr(
pgeocode, "DOWNLOAD_URL", [httpserver.url_for(download_url)]
)
# Nominatim("fr")
with pytest.raises(urllib.error.HTTPError, match="HTTP Error 404"):
Nominatim("fr")
httpserver.check_assertions()
def test_first_url_fails(httpserver, monkeypatch, temp_dir):
download_url = "/IE.txt"
httpserver.expect_oneshot_request(download_url).respond_with_data(
"", status=404
)
monkeypatch.setattr(
pgeocode,
"DOWNLOAD_URL",
[
httpserver.url_for(download_url),
"https://symerio.github.io/postal-codes-data/data/"
"geonames/{country}.txt",
],
)
msg = "IE.txt failed with: HTTP Error 404.*Trying next URL"
with pytest.warns(UserWarning, match=msg):
Nominatim("ie")
httpserver.check_assertions()