Last active
June 8, 2022 13:24
-
-
Save wolfiex/c7603c71ebc966a8530bb0132977f6f5 to your computer and use it in GitHub Desktop.
Map-Tile Tools in Python
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
''' | |
A collection of Tile tools in python. | |
Author: Daniel Ellis 2022 | |
Contact: daniel.ellis.research (a-t) gmail (dot) com | |
Article: https://medium.com/p/e54de570d0bd | |
''' | |
import mpmath as mp | |
def get_tile(lat_deg,lon_deg,zoom): | |
''' | |
Get the corresponding tile to a given longitude and latitude | |
''' | |
lat_rad = mp.radians(lat_deg) #lat_deg * mp.pi / 180 | |
n = 2 ** zoom | |
xtile = n * ((lon_deg + 180) / 360) | |
ytile = n * (1 - (mp.log(mp.tan(lat_rad) + mp.sec(lat_rad)) / mp.pi)) / 2 | |
return 'tile: %d/%d/%d '%(zoom,xtile,ytile) | |
def tile_translator(z,x,y,newzoom): | |
'''Find the linking (left top) most tile from another zoom level''' | |
n = 2 ** z | |
x /= n | |
y /= n | |
n2 = 2 ** newzoom | |
x *= n2 | |
y *= n2 | |
return '%d/%d/%d'%(newzoom,x,y) | |
def get_children(z,x,y): | |
''' Return the children of a tile ''' | |
tile = list(map(int,(tile_translator(z,x,y,z+1).split('/')))) | |
return [[tile[0],tile[1]+i,tile[2]+j] for i in [0,1] for j in [0,1]] | |
def tile2lon(z,x,y) : | |
return x / 2**z * 360 - 180; | |
def tile2lat(z,x,y) : | |
n = mp.pi - 2 * mp.pi * y / 2**z; | |
return float((180 / mp.pi) * (mp.atan(0.5 * (mp.exp(n) - mp.exp(-n))))) | |
def tile_bbox(z,x,y): | |
''' | |
Return the bounding box of a tile | |
''' | |
w = tile2lon(z,x,y) | |
s = tile2lat(z,x,y) | |
e = tile2lon(z,x+1,y) | |
n = tile2lat(z,x,y+1) | |
return [w,s,e,n] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment