Skip to content

Commit

Permalink
Merge 339c752 into f6de211
Browse files Browse the repository at this point in the history
  • Loading branch information
sgillies committed Apr 8, 2021
2 parents f6de211 + 339c752 commit 4563de4
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 80 deletions.
81 changes: 33 additions & 48 deletions mercantile/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
from collections.abc import Sequence


__version__ = "1.1.6"
__version__ = "1.2.0dev"

__all__ = [
"Bbox",
Expand All @@ -37,8 +37,7 @@
"tiles",
"ul",
"xy_bounds",
"x_minmax",
"y_minmax",
"minmax",
]


Expand Down Expand Up @@ -269,7 +268,7 @@ def lnglat(x, y, truncate=False):


def neighbors(*tile, **kwargs):
"""Get the neighbors of a tile
"""The neighbors of a tile
The neighbors function makes no guarantees regarding neighbor tile ordering.
Expand All @@ -288,7 +287,6 @@ def neighbors(*tile, **kwargs):
Examples
--------
>>> neighbors(Tile(486, 332, 10))
[Tile(x=485, y=331, z=10), Tile(x=485, y=332, z=10), Tile(x=485, y=333, z=10), Tile(x=486, y=331, z=10), Tile(x=486, y=333, z=10), Tile(x=487, y=331, z=10), Tile(x=487, y=332, z=10), Tile(x=487, y=333, z=10)]
Expand Down Expand Up @@ -588,19 +586,24 @@ def children(*tile, **kwargs):
tile : Tile or sequence of int
May be be either an instance of Tile or 3 ints, X, Y, Z.
zoom : int, optional
Returns all children at zoom *zoom*, in depth-first clockwise winding order.
If unspecified, returns the immediate (i.e. zoom + 1) children of the tile.
Returns all children at zoom *zoom*, in depth-first clockwise
winding order. If unspecified, returns the immediate (i.e. zoom
+ 1) children of the tile.
Returns
-------
list
Raises
------
InvalidZoomError
If the zoom level is not an integer greater than the zoom level
of the input tile.
Examples
--------
>>> children(Tile(0, 0, 0))
[Tile(x=0, y=0, z=1), Tile(x=0, y=1, z=1), Tile(x=1, y=0, z=1), Tile(x=1, y=1, z=1)]
>>> children(Tile(0, 0, 0), zoom=2)
[Tile(x=0, y=0, z=2), Tile(x=0, y=1, z=2), Tile(x=0, y=2, z=2), Tile(x=0, y=3, z=2), ...]
Expand All @@ -620,6 +623,7 @@ def children(*tile, **kwargs):
target_zoom = zoom if zoom is not None else ztile + 1

tiles = [tile]

while tiles[0][2] < target_zoom:
xtile, ytile, ztile = tiles.pop(0)
tiles += [
Expand All @@ -628,6 +632,7 @@ def children(*tile, **kwargs):
Tile(xtile * 2 + 1, ytile * 2 + 1, ztile + 1),
Tile(xtile * 2, ytile * 2 + 1, ztile + 1),
]

return tiles


Expand Down Expand Up @@ -818,25 +823,8 @@ def feature(
return feat


def x_minmax(zoom):
"""Returns the min and max x tile coordinates for a specific zoom level
Parameters
----------
zoom : int
The web mercator zoom level.
Returns
-------
Tuple of (min, max)
"""

return (0, 2 ** zoom - 1)


def x_minmax(zoom):
"""Returns the min and max x tile coordinates for a specific zoom level
def minmax(zoom):
"""Minimum and maximum tile coordinates for a zoom level
Parameters
----------
Expand All @@ -845,31 +833,28 @@ def x_minmax(zoom):
Returns
-------
Tuple of (min, max) integers
"""

if zoom != int(zoom) or int(zoom) < 0:
raise InvalidZoomError("zoom must be a positive integer")

return (0, 2 ** zoom - 1)


def y_minmax(zoom):
"""Returns the min and max y tile coordinates for a specific zoom level
minimum : int
Minimum tile coordinate (note: always 0).
maximum : int
Maximum tile coordinate (2 ** zoom - 1).
Parameters
----------
zoom : int
The web mercator zoom level.
Raises
------
InvalidZoomError
If zoom level is not a positive integer.
Returns
-------
Tuple of (min, max) integers
Examples
--------
>>> minmax(1)
(0, 1)
>>> minmax(-1)
Traceback (most recent call last):
...
InvalidZoomError: zoom must be a positive integer
"""

if zoom != int(zoom) or int(zoom) < 0:
if not isinstance(zoom, int) or int(zoom) < 0:
raise InvalidZoomError("zoom must be a positive integer")

return (0, 2 ** zoom - 1)
45 changes: 13 additions & 32 deletions tests/test_funcs.py
Original file line number Diff line number Diff line change
Expand Up @@ -509,40 +509,21 @@ def test__xy_south_of_limit(lat):
assert y > 1


def test_x_minmax():
assert mercantile.x_minmax(zoom=0) == (0, 0)
assert mercantile.x_minmax(zoom=1) == (0, 1)
def test_minmax():
"""Exercise minmax from zoom levels 0 to 28"""
assert mercantile.minmax(zoom=0) == (0, 0)
assert mercantile.minmax(zoom=1) == (0, 1)

for z in range(0, 28):
xmin, xmax = mercantile.x_minmax(z)
minimum, maximum = mercantile.minmax(z)

assert xmin == 0
assert xmax >= 0
assert xmax == 2 ** z - 1
assert minimum == 0
assert maximum >= 0
assert maximum == 2 ** z - 1


def test_y_minmax():
assert mercantile.y_minmax(zoom=0) == (0, 0)
assert mercantile.y_minmax(zoom=1) == (0, 1)

for z in range(0, 28):
ymin, ymax = mercantile.y_minmax(z)

assert ymin == 0
assert ymax >= 0
assert ymax == 2 ** z - 1


def test_xy_minmax():
for z in range(0, 28):
xmin, xmax = mercantile.x_minmax(z)
ymin, ymax = mercantile.y_minmax(z)

assert xmin == 0
assert ymin == 0

nx = xmin + xmax + 1
ny = ymin + ymax + 1

assert nx == 2 ** z
assert ny == 2 ** z
@pytest.mark.parametrize("z", [1.2, "lol", -1])
def test_minmax_error(z):
"""Get an exception when zoom is invalid"""
with pytest.raises(mercantile.InvalidZoomError):
mercantile.minmax(z)

0 comments on commit 4563de4

Please sign in to comment.