/
test_morecantile.py
605 lines (481 loc) · 18.1 KB
/
test_morecantile.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
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
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
"""Tests for morecantile."""
import math
import warnings
import mercantile
import pytest
from pyproj import CRS
import morecantile
from morecantile.errors import (
InvalidIdentifier,
InvalidZoomError,
PointOutsideTMSBounds,
)
from morecantile.utils import is_power_of_two, meters_per_unit
DEFAULT_GRID_COUNT = 13
def test_default_grids():
"""Morecantile.default_grids should return the correct list of grids."""
assert len(morecantile.tms.list()) == DEFAULT_GRID_COUNT
with pytest.raises(InvalidIdentifier):
morecantile.tms.get("ANotValidName")
def test_register():
"""Test register a new grid."""
assert len(morecantile.tms.list()) == DEFAULT_GRID_COUNT
crs = CRS.from_epsg(3031)
extent = [-948.75, -543592.47, 5817.41, -3333128.95] # From https:///epsg.io/3031
tms = morecantile.TileMatrixSet.custom(extent, crs, id="MyCustomGrid3031")
# Make sure we don't update the default tms (frozen set)
_ = morecantile.tms.register({"MyCustomGrid3031": tms})
assert len(morecantile.tms.list()) == DEFAULT_GRID_COUNT
defaults = morecantile.tms.register({"MyCustomGrid3031": tms})
assert len(defaults.list()) == DEFAULT_GRID_COUNT + 1
assert "MyCustomGrid3031" in defaults.list()
# Check it will raise an exception if TMS is already registered
with pytest.raises(Exception):
defaults = defaults.register({"MyCustomGrid3031": tms})
# Do not raise is overwrite=True
defaults = defaults.register({"MyCustomGrid3031": tms}, overwrite=True)
assert len(defaults.list()) == DEFAULT_GRID_COUNT + 1
# add tms in morecantile defaults (not something to do anyway)
epsg3031 = morecantile.TileMatrixSet.custom(extent, crs, id="epsg3031")
morecantile.defaults.default_tms["epsg3031"] = epsg3031
assert len(morecantile.defaults.default_tms.keys()) == DEFAULT_GRID_COUNT + 1
# make sure updating the default_tms dict has no effect on the default TileMatrixSets
assert len(morecantile.tms.list()) == DEFAULT_GRID_COUNT
# Update internal TMS dict
morecantile.tms.tms["MyCustomGrid3031"] = tms
assert len(morecantile.tms.list()) == DEFAULT_GRID_COUNT + 1
# make sure it doesn't propagate to the default dict
assert "MyCustomGrid3031" not in morecantile.defaults.default_tms
def test_TMSproperties():
"""Test TileSchema()."""
tms = morecantile.tms.get("WebMercatorQuad")
assert tms.crs._pyproj_crs == CRS.from_epsg(3857)
assert meters_per_unit(tms.crs._pyproj_crs) == 1.0
assert tms.minzoom == 0
assert tms.maxzoom == 24
def test_tile_coordinates():
"""Test coordinates to tile index utils."""
tms = morecantile.tms.get("WebMercatorQuad")
assert tms.tile(-179, 85, 5) == (0, 0, 5)
# Check equivalence between mercantile and morecantile
# wlon, wlat = mercantile.xy(20.0, 15.0)
assert tms.tile(20.0, 15.0, 5) == mercantile.tile(20.0, 15.0, 5)
@pytest.mark.parametrize(
"args", [(486, 332, 10), [(486, 332, 10)], [morecantile.Tile(486, 332, 10)]]
)
def test_bounds(args):
"""
TileMatrixSet.bounds should return the correct coordinates.
test form https://github.com/mapbox/mercantile/blob/master/tests/test_funcs.py
"""
expected = (-9.140625, 53.12040528310657, -8.7890625, 53.33087298301705)
tms = morecantile.tms.get("WebMercatorQuad")
bbox = tms.bounds(*args)
for a, b in zip(expected, bbox):
assert round(a - b, 6) == 0
assert bbox.left == bbox[0]
assert bbox.bottom == bbox[1]
assert bbox.right == bbox[2]
assert bbox.top == bbox[3]
@pytest.mark.parametrize(
"args", [(486, 332, 10), [(486, 332, 10)], [morecantile.Tile(486, 332, 10)]]
)
def test_xy_bounds(args):
"""
TileMatrixSet.xy_bounds should return the correct coordinates.
test form https://github.com/mapbox/mercantile/blob/master/tests/test_funcs.py
"""
expected = (
-1017529.7205322663,
7005300.768279833,
-978393.962050256,
7044436.526761846,
)
tms = morecantile.tms.get("WebMercatorQuad")
bounds = tms.xy_bounds(*args)
for a, b in zip(expected, bounds):
assert round(a - b, 6) == 0
def test_ul_tile():
"""
TileMatrixSet.ul should return the correct coordinates.
test form https://github.com/mapbox/mercantile/blob/master/tests/test_funcs.py
"""
tms = morecantile.tms.get("WebMercatorQuad")
xy = tms.ul(486, 332, 10)
expected = (-9.140625, 53.33087298301705)
for a, b in zip(expected, xy):
assert round(a - b, 6) == 0
def test_projul_tile():
"""
TileMatrixSet._ul should return the correct coordinates in input projection.
test form https://github.com/mapbox/mercantile/blob/master/tests/test_funcs.py
"""
tms = morecantile.tms.get("WebMercatorQuad")
xy = tms._ul(486, 332, 10)
expected = (-1017529.7205322663, 7044436.526761846)
for a, b in zip(expected, xy):
assert round(a - b, 6) == 0
def test_projtile():
"""TileSchema._tile should return the correct tile."""
tms = morecantile.tms.get("WebMercatorQuad")
assert tms._tile(1000, 1000, 1) == morecantile.Tile(1, 0, 1)
def test_feature():
"""TileSchema.feature should create proper geojson feature."""
tms = morecantile.tms.get("WebMercatorQuad")
feat = tms.feature(morecantile.Tile(1, 0, 1))
assert feat["bbox"]
assert feat["id"]
assert feat["geometry"]
assert len(feat["properties"].keys()) == 3
feat = tms.feature(
morecantile.Tile(1, 0, 1),
buffer=-10,
precision=4,
fid="1",
props={"some": "thing"},
)
assert feat["bbox"]
assert feat["id"] == "1"
assert feat["geometry"]
assert len(feat["properties"].keys()) == 4
with pytest.warns(UserWarning):
feat = tms.feature(
morecantile.Tile(1, 0, 1), projected=True, fid="1", props={"some": "thing"}
)
assert feat["bbox"]
assert feat["id"] == "1"
assert feat["geometry"]
assert len(feat["properties"].keys()) == 4
################################################################################
# replicate mercantile tests
# https://github.com/mapbox/mercantile/blob/master/tests/test_funcs.py
@pytest.mark.parametrize(
"args", [(486, 332, 10), [(486, 332, 10)], [morecantile.Tile(486, 332, 10)]]
)
def test_ul(args):
"""test args."""
tms = morecantile.tms.get("WebMercatorQuad")
expected = (-9.140625, 53.33087298301705)
lnglat = tms.ul(*args)
for a, b in zip(expected, lnglat):
assert round(a - b, 6) == 0
assert lnglat[0] == lnglat.x
assert lnglat[1] == lnglat.y
@pytest.mark.parametrize(
"args", [(486, 332, 10), [(486, 332, 10)], [mercantile.Tile(486, 332, 10)]]
)
def test_bbox(args):
"""test bbox."""
tms = morecantile.tms.get("WebMercatorQuad")
expected = (-9.140625, 53.12040528310657, -8.7890625, 53.33087298301705)
bbox = tms.bounds(*args)
for a, b in zip(expected, bbox):
assert round(a - b, 6) == 0
assert bbox.left == bbox[0]
assert bbox.bottom == bbox[1]
assert bbox.right == bbox[2]
assert bbox.top == bbox[3]
def test_xy_tile():
"""x, y for the 486-332-10 tile is correctly calculated."""
tms = morecantile.tms.get("WebMercatorQuad")
ul = tms.ul(486, 332, 10)
xy = tms.xy(*ul)
expected = (-1017529.7205322663, 7044436.526761846)
for a, b in zip(expected, xy):
assert round(a - b, 6) == 0
def test_xy_null_island():
"""x, y for (0, 0) is correctly calculated"""
tms = morecantile.tms.get("WebMercatorQuad")
xy = tms.xy(0.0, 0.0)
expected = (0.0, 0.0)
for a, b in zip(expected, xy):
assert round(a - b, 6) == 0
@pytest.mark.xfail
def test_xy_south_pole():
"""Return -inf for y at South Pole
Note: mercantile returns (0.0, inf)
"""
tms = morecantile.tms.get("WebMercatorQuad")
with pytest.warns(PointOutsideTMSBounds):
xy = tms.xy(0.0, -90)
assert xy.x == 0.0
assert xy.y == float("inf")
@pytest.mark.xfail
def test_xy_north_pole():
"""Return inf for y at North Pole.
Note: mercantile returns (0.0, -inf)
"""
tms = morecantile.tms.get("WebMercatorQuad")
with pytest.warns(PointOutsideTMSBounds):
xy = tms.xy(0.0, 90)
assert xy.x == 0.0
assert xy.y == float("inf")
def test_xy_truncate():
"""Input is truncated"""
tms = morecantile.tms.get("WebMercatorQuad")
assert tms.xy(-181.0, 0.0, truncate=True) == tms.xy(tms.bbox.left, 0.0)
@pytest.mark.xfail
def test_lnglat():
"""test lnglat."""
tms = morecantile.tms.get("WebMercatorQuad")
# Make sure not warning is raised
with warnings.catch_warnings():
warnings.simplefilter("error")
xy = (-8366731.739810849, -1655181.9927159143)
lnglat = tms.lnglat(*xy)
assert round(lnglat.x, 5) == -75.15963
assert round(lnglat.y, 5) == -14.70462
with pytest.warns(PointOutsideTMSBounds):
xy = (-28366731.739810849, -1655181.9927159143)
lnglat = tms.lnglat(*xy, truncate=True)
assert round(lnglat.x, 5) == -180.0 # in Mercantile (105.17731 in Morecantile)
assert round(lnglat.y, 5) == -14.70462 # in Mercantile
@pytest.mark.parametrize("tms_name", morecantile.tms.list())
def test_axis_inverted(tms_name):
"""Test axis inversion check"""
tms = morecantile.tms.get(tms_name)
if tms.orderedAxes:
assert morecantile.models.crs_axis_inverted(
tms.crs._pyproj_crs
) == morecantile.models.ordered_axis_inverted(tms.orderedAxes)
def test_lnglat_gdal3():
"""test lnglat."""
# PROJ>=7 returns (105.17731317609572, -14.704620000000013)
tms = morecantile.tms.get("WebMercatorQuad")
with pytest.warns(PointOutsideTMSBounds):
xy = (-28366731.739810849, -1655181.9927159143)
lnglat = tms.lnglat(*xy, truncate=True)
assert round(lnglat.x, 5) == 105.17731
assert round(lnglat.y, 5) == -14.70462
def test_lnglat_xy_roundtrip():
"""Test roundtrip."""
tms = morecantile.tms.get("WebMercatorQuad")
lnglat = (-105.0844, 40.5853)
roundtrip = tms.lnglat(*tms.xy(*lnglat))
for a, b in zip(roundtrip, lnglat):
assert round(a - b, 6) == 0
@pytest.mark.parametrize(
"args", [(486, 332, 10), [(486, 332, 10)], [mercantile.Tile(486, 332, 10)]]
)
def test_xy_bounds_mercantile(args):
"""test xy_bounds."""
tms = morecantile.tms.get("WebMercatorQuad")
expected = (
-1017529.7205322663,
7005300.768279833,
-978393.962050256,
7044436.526761846,
)
bounds = tms.xy_bounds(*args)
for a, b in zip(expected, bounds):
assert round(a - b, 6) == 0
def test_tile_not_truncated():
"""test tile."""
tms = morecantile.tms.get("WebMercatorQuad")
tile = tms.tile(20.6852, 40.1222, 9)
expected = (285, 193)
assert tile[0] == expected[0]
assert tile[1] == expected[1]
def test_tile_truncate():
"""Input is truncated"""
tms = morecantile.tms.get("WebMercatorQuad")
assert tms.tile(-181.0, 0.0, 9, truncate=True) == tms.tile(-180.0, 0.0, 9)
def test_tiles():
"""Test tiles from bbox."""
tms = morecantile.tms.get("WebMercatorQuad")
# replicate mercantile tests
# https://github.com/mapbox/mercantile/blob/master/tests/test_funcs.py#L115-L178
bounds = (-105, 39.99, -104.99, 40)
tiles = list(tms.tiles(*bounds, zooms=[14]))
expect = [
morecantile.Tile(x=3413, y=6202, z=14),
morecantile.Tile(x=3413, y=6203, z=14),
]
assert sorted(tiles) == sorted(expect)
# Single zoom
bounds = (-105, 39.99, -104.99, 40)
tiles = list(tms.tiles(*bounds, zooms=14))
expect = [
morecantile.Tile(x=3413, y=6202, z=14),
morecantile.Tile(x=3413, y=6203, z=14),
]
assert sorted(tiles) == sorted(expect)
# Input is truncated
assert list(tms.tiles(-181.0, 0.0, -170.0, 10.0, zooms=[2], truncate=True)) == list(
tms.tiles(-180.0, 0.0, -170.0, 10.0, zooms=[2])
)
assert list(tms.tiles(-180.0, -90.0, 180.0, 90.0, zooms=[0])) == [
morecantile.Tile(x=0, y=0, z=0)
]
assert list(tms.tiles(-180.0, -90.0, 180.0, 90.0, zooms=[0], truncate=True)) == [
morecantile.Tile(x=0, y=0, z=0)
]
# Antimeridian-crossing bounding boxes are handled
bounds = (175.0, 5.0, -175.0, 10.0)
assert len(list(tms.tiles(*bounds, zooms=[2]))) == 2
def test_tiles_for_tms_with_non_standard_row_col_order():
"""Test tiles from bbox when TMS has non-standard row/col alignment with lat/lon."""
crs = CRS.from_proj4(
"+proj=s2 +lat_0=0.0 +lon_0=-90.0 +ellps=WGS84 +UVtoST=quadratic"
)
extent = [0.0, 0.0, 1.0, 1.0]
s2f4 = morecantile.TileMatrixSet.custom(extent, crs, id="S2F4")
overlapping_tiles = s2f4.tiles(-100, 27, -95, 33, [6])
assert len(list(overlapping_tiles)) == 30
def test_global_tiles_clamped():
"""Y is clamped to (0, 2 ** zoom - 1)."""
tms = morecantile.tms.get("WebMercatorQuad")
tiles = list(tms.tiles(-180, -90, 180, 90, [1]))
assert len(tiles) == 4
assert min(t.y for t in tiles) == 0
assert max(t.y for t in tiles) == 1
def test_tiles_roundtrip_children():
"""tiles(bounds(tile)) gives the tile's children"""
tms = morecantile.tms.get("WebMercatorQuad")
t = morecantile.Tile(x=3413, y=6202, z=14)
res = list(tms.tiles(*tms.bounds(t), zooms=[15]))
assert len(res) == 4
@pytest.mark.parametrize(
"t",
[
morecantile.Tile(x=3413, y=6202, z=14),
morecantile.Tile(486, 332, 10),
morecantile.Tile(10, 10, 10),
],
)
def test_tiles_roundtrip(t):
"""Tiles(bounds(tile)) gives the tile."""
tms = morecantile.tms.get("WebMercatorQuad")
res = list(tms.tiles(*tms.bounds(t), zooms=[t.z]))
assert len(res) == 1
val = res.pop()
assert val.x == t.x
assert val.y == t.y
assert val.z == t.z
def test_tiles_nan_bounds():
"""
nan bounds should raise an error instead of getting clamped to avoid
unintentionally generating tiles for the entire TMS' extent.
"""
tms = morecantile.tms.get("WebMercatorQuad")
bounds = (-105, math.nan, -104.99, 40)
with pytest.raises(ValueError):
list(tms.tiles(*bounds, zooms=[14]))
def test_extend_zoom():
"""TileMatrixSet.ul should return the correct coordinates."""
tms = morecantile.tms.get("WebMercatorQuad")
merc = mercantile.xy_bounds(1000, 1000, 25)
with pytest.warns(UserWarning):
more = tms.xy_bounds(1000, 1000, 25)
for a, b in zip(more, merc):
assert round(a - b, 6) == 0
merc = mercantile.xy_bounds(2000, 2000, 26)
with pytest.warns(UserWarning):
more = tms.xy_bounds(2000, 2000, 26)
for a, b in zip(more, merc):
assert round(a - b, 6) == 0
merc = mercantile.xy_bounds(2000, 2000, 27)
with pytest.warns(UserWarning):
more = tms.xy_bounds(2000, 2000, 27)
for a, b in zip(more, merc):
assert round(a - b, 6) == 0
merc = mercantile.xy_bounds(2000, 2000, 30)
with pytest.warns(UserWarning):
more = tms.xy_bounds(2000, 2000, 30)
for a, b in zip(more, merc):
assert round(a - b, 6) == 0
def test_is_power_of_two():
"""is power ot 2?"""
assert is_power_of_two(8)
assert not is_power_of_two(7)
@pytest.mark.parametrize(
"t,res",
[
(morecantile.Tile(0, 0, 0), True),
(morecantile.Tile(1, 0, 0), False),
(morecantile.Tile(0, 0, -1), False),
],
)
def test_is_valid_tile(t, res):
"""test if tile are valid."""
tms = morecantile.tms.get("WebMercatorQuad")
assert tms.is_valid(t) == res
def test_neighbors():
"""test neighbors."""
tms = morecantile.tms.get("WebMercatorQuad")
x, y, z = 243, 166, 9
tiles = tms.neighbors(x, y, z)
assert len(tiles) == 8
assert all(t.z == z for t in tiles)
assert all(t.x - x in (-1, 0, 1) for t in tiles)
assert all(t.y - y in (-1, 0, 1) for t in tiles)
def test_neighbors_invalid():
"""test neighbors."""
tms = morecantile.tms.get("WebMercatorQuad")
x, y, z = 0, 166, 9
tiles = tms.neighbors(x, y, z)
assert len(tiles) == 8 - 3 # no top-left, left, bottom-left
assert all(t.z == z for t in tiles)
assert all(t.x - x in (-1, 0, 1) for t in tiles)
assert all(t.y - y in (-1, 0, 1) for t in tiles)
def test_root_neighbors_invalid():
"""test neighbors."""
tms = morecantile.tms.get("WebMercatorQuad")
x, y, z = 0, 0, 0
tiles = tms.neighbors(x, y, z)
assert len(tiles) == 0 # root tile has no neighbors
def test_parent():
"""test parent"""
tms = morecantile.tms.get("WebMercatorQuad")
parent = tms.parent(486, 332, 10)
assert parent[0] == morecantile.Tile(243, 166, 9)
with pytest.raises(InvalidZoomError):
tms.parent(486, 332, 10, zoom=11)
assert tms.parent(0, 0, 0) == []
def test_parent_multi():
"""test parent"""
tms = morecantile.tms.get("WebMercatorQuad")
parent = tms.parent(486, 332, 10, zoom=8)
assert parent[0] == morecantile.Tile(121, 83, 8)
def test_children():
"""test children."""
tms = morecantile.tms.get("WebMercatorQuad")
x, y, z = 243, 166, 9
children = tms.children(x, y, z)
assert len(children) == 4
assert morecantile.Tile(2 * x, 2 * y, z + 1) in children
assert morecantile.Tile(2 * x + 1, 2 * y, z + 1) in children
assert morecantile.Tile(2 * x + 1, 2 * y + 1, z + 1) in children
assert morecantile.Tile(2 * x, 2 * y + 1, z + 1) in children
def test_children_multi():
"""test children multizoom."""
tms = morecantile.tms.get("WebMercatorQuad")
children = tms.children(243, 166, 9, zoom=11)
assert len(children) == 16
targets = [
(972, 664, 11),
(973, 664, 11),
(973, 665, 11),
(972, 665, 11),
(974, 664, 11),
(975, 664, 11),
(975, 665, 11),
(974, 665, 11),
(974, 666, 11),
(975, 666, 11),
(975, 667, 11),
(974, 667, 11),
(972, 666, 11),
(973, 666, 11),
(973, 667, 11),
(972, 667, 11),
]
for target in targets:
assert target in children
def test_children_invalid_zoom():
"""invalid zoom."""
tms = morecantile.tms.get("WebMercatorQuad")
with pytest.raises(InvalidZoomError):
tms.children(243, 166, 9, zoom=8)
with pytest.raises(InvalidZoomError):
tms.children((243, 166, 9), zoom=8)