Skip to content

Instantly share code, notes, and snippets.

@wolfiex
Last active June 8, 2022 13:24
Show Gist options
  • Save wolfiex/c7603c71ebc966a8530bb0132977f6f5 to your computer and use it in GitHub Desktop.
Save wolfiex/c7603c71ebc966a8530bb0132977f6f5 to your computer and use it in GitHub Desktop.

Revisions

  1. wolfiex revised this gist Apr 2, 2022. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion tiletools.py
    Original file line number Diff line number Diff line change
    @@ -37,7 +37,7 @@ def tile_translator(z,x,y,newzoom):


    def get_children(z,x,y):
    ''' Retun the children of a tile '''
    ''' 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]]

  2. wolfiex created this gist Apr 2, 2022.
    63 changes: 63 additions & 0 deletions tiletools.py
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,63 @@
    '''
    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):
    ''' Retun 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]