2

Given a bounding box using WGS84 lat long pairs (represented any way, not important to me) and a range of zoom levels, I'd like to be able to generate a CSV representing all standard z/x/y TMS addresses for a web map in a spherical Mercator projection.

For example, given the city of Seattle, with a BBOX of: (47.75, -122.14), (47.53, -122.53) and desired zoom levels 12-16, I'd like to see a delimited list something like:

z  |  x   |  y
----------------
12 | 656  | 1429
12 | 655  | 1429
...
14 | 2626 | 5724

Does anyone know of any way to do this, preferably in Python?

2

I just came across this Python library by Sean Gillies that makes this easy.

https://github.com/mapbox/mercantile

The following will get you the tile at the specified lng/lat.

"""
pip install mercantile
"""
lng, lat, zoom = -122.14, 47.75, 12
print(mercantile.tile(lng, lat, zoom))

# Tile(x=658, y=1428, z=12)

The tiles method will get a list of tiles for bounds as you required:

bounds = (-122.14, 47.53, -122.53, 47.75)
all_tiles = mercantile.tiles(*bounds, zooms=[3])
for t in all_tiles:
    print t

Outputs:

Tile(x=0, y=2, z=3)
Tile(x=1, y=2, z=3)
Tile(x=1, y=2, z=3)
Tile(x=2, y=2, z=3)
Tile(x=3, y=2, z=3)
Tile(x=4, y=2, z=3)
Tile(x=5, y=2, z=3)
Tile(x=6, y=2, z=3)
Tile(x=7, y=2, z=3)
1

Mike Migurski wrote a sweet seeding script for TileStache that does just this: https://github.com/migurski/TileStache/blob/master/scripts/tilestache-seed.py#L89-L90

Found that script after had already extracted the coordinate generate logic from a script written by @tcmw for Development Seed. https://raw.github.com/developmentseed/TileLive/master/tileseed.py Simply piped out the results to STDOUT.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.