Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 19 additions & 0 deletions mercantile/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ def fake_decorator(func):
CE = 2 * math.pi * RE
EPSILON = 1e-14
LL_EPSILON = 1e-11
MAX_ZOOM_LEVEL = int(math.log2(sys.float_info.max)) - 1


class Tile(namedtuple("Tile", ["x", "y", "z"])):
Expand Down Expand Up @@ -190,6 +191,11 @@ def ul(*tile):
"""
tile = _parse_tile_arg(*tile)
xtile, ytile, zoom = tile
if zoom > MAX_ZOOM_LEVEL:
raise InvalidZoomError(
"zoom must be less than or equal to {}".format(MAX_ZOOM_LEVEL)
)

Z2 = math.pow(2, zoom)
lon_deg = xtile / Z2 * 360.0 - 180.0
lat_rad = math.atan(math.sinh(math.pi * (1 - 2 * ytile / Z2)))
Expand All @@ -212,6 +218,10 @@ def bounds(*tile):
"""
tile = _parse_tile_arg(*tile)
xtile, ytile, zoom = tile
if zoom > MAX_ZOOM_LEVEL:
raise InvalidZoomError(
"zoom must be less than or equal to {}".format(MAX_ZOOM_LEVEL)
)

Z2 = math.pow(2, zoom)

Expand Down Expand Up @@ -367,6 +377,10 @@ def xy_bounds(*tile):
"""
tile = _parse_tile_arg(*tile)
xtile, ytile, zoom = tile
if zoom > MAX_ZOOM_LEVEL:
raise InvalidZoomError(
"zoom must be less than or equal to {}".format(MAX_ZOOM_LEVEL)
)

tile_size = CE / math.pow(2, zoom)

Expand Down Expand Up @@ -413,6 +427,11 @@ def tile(lng, lat, zoom, truncate=False):

"""
x, y = _xy(lng, lat, truncate=truncate)
if zoom > MAX_ZOOM_LEVEL:
raise InvalidZoomError(
"zoom must be less than or equal to {}".format(MAX_ZOOM_LEVEL)
)

Z2 = math.pow(2, zoom)

if x <= 0:
Expand Down
13 changes: 13 additions & 0 deletions tests/test_funcs.py
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,19 @@ def test_tile_truncate():
)


@pytest.mark.parametrize(
'func',
[mercantile.ul, mercantile.bounds, mercantile.xy_bounds, mercantile.tile]
)
def test_invalid_zoom_level_64_bit(func):
"""Test that an error is raised when an invalid zoom level is provided."""
with pytest.raises(mercantile.InvalidZoomError) as exc_info:
func(3, 13, 1024)

err_msg, = exc_info.value.args
assert err_msg == 'zoom must be less than or equal to 1023'


def test_tiles():
bounds = (-105, 39.99, -104.99, 40)
tiles = list(mercantile.tiles(*bounds, zooms=[14]))
Expand Down