Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def test_ul_tile_roundtrip(t):
"""ul and tile roundtrip"""
lnglat = mercantile.ul(t)
tile = mercantile.tile(lnglat.lng, lnglat.lat, t.z)
assert tile.z == t.z
assert tile.x == t.x
assert tile.y == t.y
def get_xyz(raster_file, zoom):
import rasterio
import rasterio.warp
import mercantile
with rasterio.open(str(raster_file)) as src:
raster_bounds = rasterio.warp.transform_bounds(src.crs, 'epsg:4326', *src.bounds)
raster_center_x = (raster_bounds[0] + raster_bounds[2]) / 2
raster_center_y = (raster_bounds[1] + raster_bounds[3]) / 2
tile = mercantile.tile(raster_center_x, raster_center_y, zoom)
return (tile.x, tile.y, zoom)
def tile_exists(bounds: Sequence[float], tile_x: int, tile_y: int, tile_z: int) -> bool:
"""Check if an XYZ tile is inside the given physical bounds."""
mintile = mercantile.tile(bounds[0], bounds[3], tile_z)
maxtile = mercantile.tile(bounds[2], bounds[1], tile_z)
return mintile.x <= tile_x <= maxtile.x and mintile.y <= tile_y <= maxtile.y
def read_tile(meta, tile, renderer='hillshade', scale=1, **kwargs):
"""Fetch tile data and render as a PNG."""
renderer = get_renderer(renderer)
maxzoom = int(meta['maxzoom'])
minzoom = int(meta['minzoom'])
if not minzoom <= tile.z <= maxzoom:
raise InvalidTileRequest(
'Invalid zoom: {} outside [{}, {}]'.format(
tile.z, minzoom, maxzoom))
sw = mercantile.tile(*meta['bounds'][0:2], zoom=tile.z)
ne = mercantile.tile(*meta['bounds'][2:4], zoom=tile.z)
if not sw.x <= tile.x <= ne.x:
raise InvalidTileRequest(
'Invalid x coordinate: {} outside [{}, {}]'.format(
tile.x, sw.x, ne.x))
if not ne.y <= tile.y <= sw.y:
raise InvalidTileRequest(
'Invalid y coordinate: {} outside [{}, {}]'.format(
tile.y, sw.y, ne.y))
buffer = getattr(renderer, 'BUFFER', 0)
scale = getattr(renderer, 'SCALE', scale)
(data, buffers) = render_tile(meta, tile, scale=scale, buffer=buffer)
raise ValueError('min_lat must be < max_lat')
if min_lng >= max_lng:
raise ValueError('min_lng must be < max_lng')
is_tms = False
if '{-y}' in tile_schema:
tile_schema = tile_schema.replace('{-y}', '{y}')
is_tms = True
tmp_dir_obj = tempfile.TemporaryDirectory()
tmp_dir = tmp_dir_obj.name
# Get range of tiles that cover bounds.
output_path = get_local_path(output_uri, tmp_dir)
tile_sz = 256
t = mercantile.tile(min_lng, max_lat, zoom)
xmin, ymin = t.x, t.y
t = mercantile.tile(max_lng, min_lat, zoom)
xmax, ymax = t.x, t.y
# The supplied bounds are contained within the "tile bounds" -- ie. the
# bounds of the set of tiles that covers the supplied bounds. Therefore,
# we need to crop out the imagery that lies within the supplied bounds.
# We do this by computing a top, bottom, left, and right offset in pixel
# units of the supplied bounds against the tile bounds. Getting the offsets
# in pixel units involves converting lng/lat to web mercator units since we
# assume that is the CRS of the tiles. These offsets are then used to crop
# individual tiles and place them correctly into the output raster.
nw_merc_x, nw_merc_y = lnglat2merc(min_lng, max_lat)
left_pix_offset, top_pix_offset = merc2pixel(xmin, ymin, zoom, nw_merc_x,
nw_merc_y)
raise ValueError('min_lng must be < max_lng')
is_tms = False
if '{-y}' in tile_schema:
tile_schema = tile_schema.replace('{-y}', '{y}')
is_tms = True
tmp_dir_obj = tempfile.TemporaryDirectory()
tmp_dir = tmp_dir_obj.name
# Get range of tiles that cover bounds.
output_path = get_local_path(output_uri, tmp_dir)
tile_sz = 256
t = mercantile.tile(min_lng, max_lat, zoom)
xmin, ymin = t.x, t.y
t = mercantile.tile(max_lng, min_lat, zoom)
xmax, ymax = t.x, t.y
# The supplied bounds are contained within the "tile bounds" -- ie. the
# bounds of the set of tiles that covers the supplied bounds. Therefore,
# we need to crop out the imagery that lies within the supplied bounds.
# We do this by computing a top, bottom, left, and right offset in pixel
# units of the supplied bounds against the tile bounds. Getting the offsets
# in pixel units involves converting lng/lat to web mercator units since we
# assume that is the CRS of the tiles. These offsets are then used to crop
# individual tiles and place them correctly into the output raster.
nw_merc_x, nw_merc_y = lnglat2merc(min_lng, max_lat)
left_pix_offset, top_pix_offset = merc2pixel(xmin, ymin, zoom, nw_merc_x,
nw_merc_y)
se_merc_x, se_merc_y = lnglat2merc(max_lng, min_lat)
se_left_pix_offset, se_top_pix_offset = merc2pixel(xmax, ymax, zoom,
def tile_exists(self, z, x, y):
"""Check if a mercator tile is within raster bounds."""
mintile = mercantile.tile(self.bounds[0], self.bounds[3], z)
maxtile = mercantile.tile(self.bounds[2], self.bounds[1], z)
return (
(x <= maxtile.x + 1)
and (x >= mintile.x)
and (y <= maxtile.y + 1)
and (y >= mintile.y)
)
def validate(self, tile):
if not self.minzoom <= tile.z <= self.maxzoom:
raise InvalidTileRequest(
"Invalid zoom: {} outside [{}, {}]".format(
tile.z, self.minzoom, self.maxzoom
)
)
sw = mercantile.tile(*self.bounds[0:2], zoom=tile.z)
ne = mercantile.tile(*self.bounds[2:4], zoom=tile.z)
if not sw.x <= tile.x <= ne.x:
raise InvalidTileRequest(
"Invalid x coordinate: {} outside [{}, {}]".format(tile.x, sw.x, ne.x)
)
if not ne.y <= tile.y <= sw.y:
raise InvalidTileRequest(
"Invalid y coordinate: {} outside [{}, {}]".format(tile.y, sw.y, ne.y)
)
bounds : list
WGS84 bounds (left, bottom, right, top).
x : int
Mercator tile Y index.
y : int
Mercator tile Y index.
z : int
Mercator tile ZOOM level.
Returns
-------
out : boolean
if True, the z-x-y mercator tile in inside the bounds.
"""
mintile = mercantile.tile(bounds[0], bounds[3], tile_z)
maxtile = mercantile.tile(bounds[2], bounds[1], tile_z)
return (
(tile_x <= maxtile.x + 1)
and (tile_x >= mintile.x)
and (tile_y <= maxtile.y + 1)
and (tile_y >= mintile.y)
)