Created
September 4, 2011 06:03
-
-
Save tucotuco/1192356 to your computer and use it in GitHub Desktop.
This file contains 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
def get_tile_origin(self, tile_x, tile_y, zoom): | |
'''Return the lat, lng of the northwest corner of the tile.''' | |
tile_width = 360.0/pow(2,zoom) # degrees | |
tile_height = tile_width/2 # degrees | |
n = 90 - tile_y * tile_height | |
w = -180 + tile_x * tile_width | |
return (n,w) | |
def get_bb(self, tile_x, tile_y, zoom): | |
"""Return bounding coordinates (n, e, s, w) of the tile.""" | |
tile_width = 360.0/pow(2,zoom) # degrees | |
tile_height = tile_width/2 # degrees | |
n = 90 - tile_y * tile_height | |
s = n - tile_height | |
w = -180 + tile_x * tile_width | |
e = w + tile_width | |
return (n, e, s, w) | |
def world_coord_from_point(self, zoom, lat, lng): | |
'''Return the number of pixels in x, and y given 0,0 at lat=90, lng=-180''' | |
pixel_x = int(256*(lng+180)/360.0 * pow(2,zoom)) | |
pixel_y = int(256*(90-lat)/180.0 * pow(2,zoom)) | |
return (pixel_x, pixel_y) | |
def tile_from_point(self, zoom, lat, lng): | |
''' Return a tuple containing the x, y tile index.''' | |
pixel_x, pixel_y = self.world_coord_from_point(zoom, lat, lng) | |
tile_x = int(pixel_x / 256.0) | |
tile_y = int(pixel_y / 256.0) | |
return (tile_x, tile_y) | |
def get_tile_coordinate(self, zoom, lat, lng): | |
"""Returns the offset in pixels from origin of the tile in the nw corner.""" | |
tile_x, tile_y = self.tile_from_point(zoom, lat, lng) | |
n, w = self.get_tile_origin(tile_x, tile_y, zoom) | |
world_x, world_y = self.world_coord_from_point(zoom, lat, lng) | |
left, top = self.world_coord_from_point(zoom,n,w) | |
x_offset = world_x - left | |
y_offset = world_y - top | |
return (x_offset, y_offset) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment