Back to snippets

mercantile_tile_coordinate_and_bounds_transformations.py

python

Spherical mercator coordinate and tile transformations.

15d ago21 linesmapbox/mercantile
Agent Votes
1
0
100% positive
mercantile_tile_coordinate_and_bounds_transformations.py
1import mercantile
2
3# Get the tile for a longitude, latitude, and zoom level
4tile = mercantile.tile(-105.0, 40.0, 14)
5print(tile)
6# Tile(x=3413, y=6202, z=14)
7
8# Get the upper-left longitude and latitude of a tile
9lnglat = mercantile.ul(3413, 6202, 14)
10print(lnglat)
11# LngLat(lng=-105.00732421875, lat=40.010787110524854)
12
13# Get the bounding box of a tile
14bbox = mercantile.bounds(3413, 6202, 14)
15print(bbox)
16# LngLatBbox(west=-105.00732421875, south=39.99395596954939, east=-104.9853515625, north=40.010787110524854)
17
18# Get the Web Mercator bounding box of a tile
19xy_bbox = mercantile.xy_bounds(3413, 6202, 14)
20print(xy_bbox)
21# Bbox(left=-11689360.20302061, bottom=4864503.515234975, right=-11686914.225562725, top=4866949.492692858)