<s> """<STR_LIT>""" <EOL> import math <EOL> class GlobalMercator ( object ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , tileSize = <NUM_LIT> ) : <EOL> "<STR_LIT>" <EOL> self . tileSize = tileSize <EOL> self . initialResolution = <NUM_LIT:2> * math . pi * <NUM_LIT> / self . tileSize <EOL> self . originShift = <NUM_LIT:2> * math . pi * <NUM_LIT> / <NUM_LIT> <EOL> def LatLonToMeters ( self , lat , lon ) : <EOL> "<STR_LIT>" <EOL> mx = lon * self . originShift / <NUM_LIT> <EOL> my = math . log ( math . tan ( ( <NUM_LIT> + lat ) * math . pi / <NUM_LIT> ) ) / ( math . pi / <NUM_LIT> ) <EOL> my = my * self . originShift / <NUM_LIT> <EOL> return mx , my <EOL> def MetersToLatLon ( self , mx , my ) : <EOL> "<STR_LIT>" <EOL> lon = ( mx / self . originShift ) * <NUM_LIT> <EOL> lat = ( my / self . originShift ) * <NUM_LIT> <EOL> lat = <NUM_LIT> / math . pi * ( <NUM_LIT:2> * math . atan ( math . exp ( lat * math . pi / <NUM_LIT> ) ) - math . pi / <NUM_LIT> ) <EOL> return lat , lon <EOL> def PixelsToMeters ( self , px , py , zoom ) : <EOL> "<STR_LIT>" <EOL> res = self . Resolution ( zoom ) <EOL> mx = px * res - self . originShift <EOL> my = py * res - self . originShift <EOL> return mx , my <EOL> def MetersToPixels ( self , mx , my , zoom ) : <EOL> "<STR_LIT>" <EOL> res = self . Resolution ( zoom ) <EOL> px = ( mx + self . originShift ) / res <EOL> py = ( my + self . originShift ) / res <EOL> return px , py <EOL> def PixelsToTile ( self , px , py ) : <EOL> "<STR_LIT>" <EOL> tx = int ( math . ceil ( px / float ( self . tileSize ) ) - <NUM_LIT:1> ) <EOL> ty = int ( math . ceil ( py / float ( self . tileSize ) ) - <NUM_LIT:1> ) <EOL> return tx , ty <EOL> def PixelsToRaster ( self , px , py , zoom ) : <EOL> "<STR_LIT>" <EOL> mapSize = self . tileSize << zoom <EOL> return px , mapSize - py <EOL> def MetersToTile ( self , mx , my , zoom ) : <EOL> "<STR_LIT>" <EOL> px , py = self . MetersToPixels ( mx , my , zoom ) <EOL> return self . PixelsToTile ( px , py ) <EOL> def TileBounds ( self , tx , ty , zoom ) : <EOL> "<STR_LIT>" <EOL> minx , miny = self . PixelsToMeters ( tx * self . tileSize , ty * self . tileSize , zoom ) <EOL> maxx , maxy = self . PixelsToMeters ( ( tx + <NUM_LIT:1> ) * self . tileSize , ( ty + <NUM_LIT:1> ) * self . tileSize , zoom ) <EOL> return ( minx , miny , maxx , maxy ) <EOL> def TileLatLonBounds ( self , tx , ty , zoom ) : <EOL> "<STR_LIT>" <EOL> bounds = self . TileBounds ( tx , ty , zoom ) <EOL> minLat , minLon = self . MetersToLatLon ( bounds [ <NUM_LIT:0> ] , bounds [ <NUM_LIT:1> ] ) <EOL> maxLat , maxLon = self . MetersToLatLon ( bounds [ <NUM_LIT:2> ] , bounds [ <NUM_LIT:3> ] ) <EOL> return ( minLat , minLon , maxLat , maxLon ) <EOL> def Resolution ( self , zoom ) : <EOL> "<STR_LIT>" <EOL> return self . initialResolution / ( <NUM_LIT:2> ** zoom ) <EOL> def ZoomForPixelSize ( self , pixelSize ) : <EOL> "<STR_LIT>" <EOL> for i in range ( <NUM_LIT:30> ) : <EOL> if pixelSize > self . Resolution ( i ) : <EOL> return i - <NUM_LIT:1> if i != <NUM_LIT:0> else <NUM_LIT:0> <EOL> def GoogleTile ( self , tx , ty , zoom ) : <EOL> "<STR_LIT>" <EOL> return tx , ( <NUM_LIT:2> ** zoom - <NUM_LIT:1> ) - ty <EOL> def QuadTree ( self , tx , ty , zoom ) : <EOL> "<STR_LIT>" <EOL> quadKey = "<STR_LIT>" <EOL> ty = ( <NUM_LIT:2> ** zoom - <NUM_LIT:1> ) - ty <EOL> for i in range ( zoom , <NUM_LIT:0> , - <NUM_LIT:1> ) : <EOL> digit = <NUM_LIT:0> <EOL> mask = <NUM_LIT:1> << ( i - <NUM_LIT:1> ) <EOL> if ( tx & mask ) != <NUM_LIT:0> : <EOL> digit += <NUM_LIT:1> <EOL> if ( ty & mask ) != <NUM_LIT:0> : <EOL> digit += <NUM_LIT:2> <EOL> quadKey += str ( digit ) <EOL> return quadKey <EOL> class GlobalGeodetic ( object ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , tileSize = <NUM_LIT> ) : <EOL> self . tileSize = tileSize <EOL> def LatLonToPixels ( self , lat , lon , zoom ) : <EOL> "<STR_LIT>" <EOL> res = <NUM_LIT> / <NUM_LIT> / <NUM_LIT:2> ** zoom <EOL> px = ( <NUM_LIT> + lat ) / res <EOL> py = ( <NUM_LIT> + lon ) / res <EOL> return px , py <EOL> def PixelsToTile ( self , px , py ) : <EOL> "<STR_LIT>" <EOL> tx = int ( math . ceil ( px / float ( self . tileSize ) ) - <NUM_LIT:1> ) <EOL> ty = int ( math . ceil ( py / float ( self . tileSize ) ) - <NUM_LIT:1> ) <EOL> return tx , ty <EOL> def Resolution ( self , zoom ) : <EOL> "<STR_LIT>" <EOL> return <NUM_LIT> / <NUM_LIT> / <NUM_LIT:2> ** zoom <EOL> def TileBounds ( tx , ty , zoom ) : <EOL> "<STR_LIT>" <EOL> res = <NUM_LIT> / <NUM_LIT> / <NUM_LIT:2> ** zoom <EOL> return ( <EOL> tx * <NUM_LIT> * res - <NUM_LIT> , <EOL> ty * <NUM_LIT> * res - <NUM_LIT> , <EOL> ( tx + <NUM_LIT:1> ) * <NUM_LIT> * res - <NUM_LIT> , <EOL> ( ty + <NUM_LIT:1> ) * <NUM_LIT> * res - <NUM_LIT> <EOL> ) <EOL> if __name__ == "<STR_LIT:__main__>" : <EOL> import sys , os <EOL> def Usage ( s = "<STR_LIT>" ) : <EOL> print "<STR_LIT>" <EOL> print <EOL> if s : <EOL> print s <EOL> print <EOL> print "<STR_LIT>" <EOL> print "<STR_LIT>" <EOL> print "<STR_LIT>" <EOL> print "<STR_LIT>" <EOL> sys . exit ( <NUM_LIT:1> ) <EOL> profile = '<STR_LIT>' <EOL> zoomlevel = None <EOL> lat , lon , latmax , lonmax = None , None , None , None <EOL> boundingbox = False <EOL> argv = sys . argv <EOL> i = <NUM_LIT:1> <EOL> while i < len ( argv ) : <EOL> arg = argv [ i ] <EOL> if arg == '<STR_LIT>' : <EOL> i = i + <NUM_LIT:1> <EOL> profile = argv [ i ] <EOL> if zoomlevel is None : <EOL> zoomlevel = int ( argv [ i ] ) <EOL> elif lat is None : <EOL> lat = float ( argv [ i ] ) <EOL> elif lon is None : <EOL> lon = float ( argv [ i ] ) <EOL> elif latmax is None : <EOL> latmax = float ( argv [ i ] ) <EOL> elif lonmax is None : <EOL> lonmax = float ( argv [ i ] ) <EOL> else : <EOL> Usage ( "<STR_LIT>" ) <EOL> i = i + <NUM_LIT:1> <EOL> if profile != '<STR_LIT>' : <EOL> Usage ( "<STR_LIT>" ) <EOL> if zoomlevel == None or lat == None or lon == None : <EOL> Usage ( "<STR_LIT>" ) <EOL> if latmax is not None and lonmax is None : <EOL> Usage ( "<STR_LIT>" ) <EOL> if latmax != None and lonmax != None : <EOL> if latmax < lat : <EOL> Usage ( "<STR_LIT>" ) <EOL> if lonmax < lon : <EOL> Usage ( "<STR_LIT>" ) <EOL> boundingbox = ( lon , lat , lonmax , latmax ) <EOL> tz = zoomlevel <EOL> mercator = GlobalMercator ( ) <EOL> mx , my = mercator . LatLonToMeters ( lat , lon ) <EOL> print "<STR_LIT>" <EOL> print ( mx , my ) <EOL> tminx , tminy = mercator . MetersToTile ( mx , my , tz ) <EOL> if boundingbox : <EOL> mx , my = mercator . LatLonToMeters ( latmax , lonmax ) <EOL> print "<STR_LIT>" <EOL> print ( mx , my ) <EOL> tmaxx , tmaxy = mercator . MetersToTile ( mx , my , tz ) <EOL> else : <EOL> tmaxx , tmaxy = tminx , tminy <EOL> for ty in range ( tminy , tmaxy + <NUM_LIT:1> ) : <EOL> for tx in range ( tminx , tmaxx + <NUM_LIT:1> ) : <EOL> tilefilename = "<STR_LIT>" % ( tz , tx , ty ) <EOL> print tilefilename , "<STR_LIT>" <EOL> gx , gy = mercator . GoogleTile ( tx , ty , tz ) <EOL> print "<STR_LIT>" , gx , gy <EOL> quadkey = mercator . QuadTree ( tx , ty , tz ) <EOL> print "<STR_LIT>" , quadkey , '<STR_LIT:(>' , int ( quadkey , <NUM_LIT:4> ) , '<STR_LIT:)>' <EOL> bounds = mercator . TileBounds ( tx , ty , tz ) <EOL> print <EOL> print "<STR_LIT>" , bounds <EOL> wgsbounds = mercator . TileLatLonBounds ( tx , ty , tz ) <EOL> print "<STR_LIT>" , wgsbounds <EOL> print "<STR_LIT>" % ( <EOL> bounds [ <NUM_LIT:0> ] , bounds [ <NUM_LIT:1> ] , bounds [ <NUM_LIT:2> ] , bounds [ <NUM_LIT:3> ] , "<STR_LIT>" , tz , tx , ty ) <EOL> print </s>
