# How to use the mercantile.xy function in mercantile

## To help you get started, we’ve selected a few mercantile examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

mapbox / mercantile / tests / test_funcs.py View on Github
``````def test_xy_south_pole():
"""Return -inf for y at South Pole"""
assert mercantile.xy(0.0, -90) == (0.0, float("-inf"))``````
mojodna / marblecutter / normal.py View on Github
``````def render_normal(tile, data, buffers):
# TODO does this exhibit problems that are addressed by adjusting heights according to latitude?

bounds = mercantile.bounds(*tile)
ll = mercantile.xy(*bounds[0:2])
ur = mercantile.xy(*bounds[2:4])

dx = (ur[0] - ll[0]) / 256
dy = (ur[1] - ll[1]) / 256

ygrad, xgrad = np.gradient(data, 2)
img = np.dstack((-1.0 / dx * xgrad, 1.0 / dy * ygrad,
np.ones(data.shape)))

# first, we normalise to unit vectors. this puts each element of img
# in the range (-1, 1). the "einsum" stuff is serious black magic, but
# what it (should be) saying is "for each i,j in the rows and columns,
# the output is the sum of img[i,j,k]*img[i,j,k]" - i.e: the square.
norm = np.sqrt(np.einsum('ijk,ijk->ij', img, img))

# the norm is now the "wrong shape" according to numpy, so we need to``````
mojodna / marblecutter / hillshade.py View on Github
``````def render_hillshade(tile, data, buffers, dx, dy, scale=1, resample=True, add_slopeshade=True):
bounds = mercantile.bounds(*tile)
ll = mercantile.xy(*bounds[0:2])
ur = mercantile.xy(*bounds[2:4])

dx = -1 * (ur[0] - ll[0]) / 256
dy = -1 * (ur[1] - ll[1]) / 256

# TODO slopeshade addition results in excessively dark images

# interpolate latitudes
# TODO do this earlier
bounds = mercantile.bounds(tile.x, tile.y, tile.z)
height = data.shape[0]
latitudes = np.interp(np.arange(height), [0, height - 1], [bounds.north, bounds.south])

factors = 1 / np.cos(np.radians(latitudes))

# convert to 2d array, rotate 270º, scale data
data = data * np.rot90(np.atleast_2d(factors), 3)``````
ealgis / ealgis / django / ealgis / mvt.py View on Github
``````def create_vectortile_sql(layer, bounds):
# Create extent
west, south = xy(bounds.west, bounds.south)
east, north = xy(bounds.east, bounds.north)
extent = "ST_MakeBox2D(ST_MakePoint({west}, {south}), ST_MakePoint({east}, {north}))".format(west=west, south=south, east=east, north=north)

# e.g. aus_census_2011_shapes.sa1
geom_table_name = "{schema_name}.{geometry_name}".format(geometry_name=layer["geometry"], schema_name=layer["schema"])
# e.g. aus_census_2011_shapes.sa1.geom_3857
geom_column_definition = "{}.geom_3857".format(geom_table_name)

# Replace the compiled geometry column definition with the zoom-level dependent version
# e.g. Replace "ST_AsEWKB(aus_census_2011_shapes.sa1.geom_3857)" with "ST_AsMVTGeom(ST_Simplify(aus_census_2011_shapes.sa1.geom_3857, TOLERANCE), EXTENT_OF_TILE)"

# Zoom 15 is our highest resolution (configured in OpenLayers), so we need to grab
# unsimplified geometries to allow us to re-use them as the user zooms in.
base_query = MapDefinition.layer_postgis_query(layer)
if z == 15 or layer["type"] in ["POINT", "MULTIPOINT"]:
data_query = base_query.replace(
"ST_AsEWKB({})".format(geom_column_definition),``````
mapbox / supermercado / supermercado / burntiles.py View on Github
``````def make_transform(tilerange, zoom):
ulx, uly = mercantile.xy(
*mercantile.ul(tilerange["x"]["min"], tilerange["y"]["min"], zoom)
)
lrx, lry = mercantile.xy(
*mercantile.ul(tilerange["x"]["max"], tilerange["y"]["max"], zoom)
)
xcell = (lrx - ulx) / float(tilerange["x"]["max"] - tilerange["x"]["min"])
ycell = (uly - lry) / float(tilerange["y"]["max"] - tilerange["y"]["min"])
return Affine(xcell, 0, ulx, 0, -ycell, uly)``````
ealgis / ealgis / django / ealgis / tiles.py View on Github
``````metres2Degrees = (2 * math.pi * 6378137) / 360

return (stdTileWidth / math.pow(2, z - 1)) / metres2Degrees

tolerance = tolerance()
places = 0
precision = 1.0

while (precision &gt; tolerance):
places += 1
precision /= 10

return places

srid = "3857"
west, south = mercantile.xy(bounds.west, bounds.south)
east, north = mercantile.xy(bounds.east, bounds.north)

decimalPlaces = setDecimalPlaces()

resolution = 6378137.0 * 2.0 * math.pi / 256.0 / math.pow(2.0, z)
tolerance = resolution / 20

# A bit of a hack - assign the minimum area a feature must have to be visible at each zoom level.
# if(z &lt;= 4):
#     min_area = 2500000
# elif(z &lt;= 8):
#     # min_area = 350000
#     min_area = 1000000
# elif(z &lt;= 12):
#     min_area = 50000
# elif(z &lt;= 16):``````
mapbox / supermercado / supermercado / uniontiles.py View on Github
``````def union(inputtiles, parsenames):

tiles = sutils.tile_parser(inputtiles, parsenames)

xmin, xmax, ymin, ymax = sutils.get_range(tiles)

zoom = sutils.get_zoom(tiles)

# make an array of shape (xrange + 3, yrange + 3)
burn = sutils.burnXYZs(tiles, xmin, xmax, ymin, ymax, 0)

nw = mercantile.xy(*mercantile.ul(xmin, ymin, zoom))

se = mercantile.xy(*mercantile.ul(xmax + 1, ymax + 1, zoom))

aff = Affine(
((se[0] - nw[0]) / float(xmax - xmin + 1)),
0.0,
nw[0],
0.0,
-((nw[1] - se[1]) / float(ymax - ymin + 1)),
nw[1],
)

unprojecter = sutils.Unprojecter()

unionedTiles = [
{``````
mapbox / untiler / untiler / __init__.py View on Github
``````def make_src_meta(bounds, size, creation_opts={}):
"""
Create metadata for output tiles
"""

ul = merc.xy(bounds.west, bounds.north)
lr = merc.xy(bounds.east, bounds.south)

aff = make_affine(size, size, ul, lr)

## default values
src_meta = {
'driver': 'GTiff',
'height': size,
'width': size,
'count': 4,
'dtype': np.uint8,
'affine': aff,
"crs": 'EPSG:3857',
'compress': 'JPEG',
'tiled': True,
'blockxsize': 256,``````

## mercantile

Web mercator XYZ tile utilities

BSD-3-Clause
Latest version published 3 years ago

58 / 100