Skip to content

Commit

Permalink
tile_system tests pass
Browse files Browse the repository at this point in the history
  • Loading branch information
U-NORTHAMERICA\taheroux authored and U-NORTHAMERICA\taheroux committed Mar 6, 2014
1 parent 9584216 commit 156974a
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 20 deletions.
3 changes: 2 additions & 1 deletion quadkey/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ def __init__(self, geo, level):
# assert lat, lon and level are valid
self.key = QuadKey.get_quadkey(geo, level)
self.geo = geo
self.latititude = geo[0]
self.longitude = geo[1]
self.level = level

def __str__(self):
Expand All @@ -26,7 +28,6 @@ def get_quadkey(geo, level):
pixel = TileSystem.geo_to_pixel(geo, level)
tile = TileSystem.pixel_to_tile(pixel)
quadkey = TileSystem.tile_to_quadkey(tile, level)
print geo, pixel, tile, quadkey
return quadkey

@staticmethod
Expand Down
10 changes: 7 additions & 3 deletions quadkey/tile_system.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
from util import precondition
from math import sin, cos, atan, exp, log, pi
from decimal import *

def valid_level(level):
LEVEL_RANGE = (1,23)
return LEVEL_RANGE[0] <= level <= LEVEL_RANGE[1]

getcontext().prec = 6

class TileSystem:
"""
Class with static method to build quadkeys from lat, lon, levels
Expand Down Expand Up @@ -44,16 +47,17 @@ def map_scale(lat, level, dpi):
@precondition(lambda geo, lvl: valid_level(lvl))
def geo_to_pixel(geo, level):
"""Transform from geo coordinates to pixel coordinates"""
lat, lon = geo
lat, lon = float(geo[0]), float(geo[1])
lat = TileSystem.clip(lat, TileSystem.LATITUDE_RANGE)
lon = TileSystem.clip(lon, TileSystem.LONGITUDE_RANGE)
x = (lon + 180) / 360
sin_lat = sin(lat * pi / 180)
y = 0.5 - log((1 + sin_lat) / (1 - sin_lat) / (4 * pi))
y = 0.5 - log((1 + sin_lat) / (1 - sin_lat)) / (4 * pi)
# might need to cast to uint
map_size = TileSystem.map_size(level)
pixel_x = int(TileSystem.clip(x * map_size + 0.5, (0, map_size - 1)))
pixel_y = int(TileSystem.clip(y * map_size + 0.5, (0, map_size - 1)))
# print '\n'+str( ((lat, lon), sin_lat, (x, y), map_size, (pixel_x, pixel_y)) )+'\n'
return pixel_x, pixel_y

@staticmethod
Expand All @@ -67,7 +71,7 @@ def pixel_to_geo(pixel, level):
y = 0.5 - ( TileSystem.clip(pixel_y, (0, map_size - 1)) / map_size)
lat = 90 - 360 * atan(exp(-y * 2 * pi)) / pi
lon = 360 * x
return lat, lon
return round(lat,6), round(lon,6)

@staticmethod
def pixel_to_tile(pixel):
Expand Down
4 changes: 2 additions & 2 deletions tests/quadkey_tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@ def testToString(self):

def testGetQuadKey(self):
geo = (40, -105)
level = 9
key = '023101012'
level = 7
key = '0231010'
self.assertEqual(key, QuadKey.get_quadkey(geo, level))

def testGetCoordinates(self):
Expand Down
38 changes: 24 additions & 14 deletions tests/tile_system.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,34 +18,44 @@ def testMapSize(self):
TileSystem.map_size(0)

def testGroundResolution(self):
TileSystem.ground_resolution(45, 7)
geo = (40., -105.)
res = 936.86657226219847
TileSystem.ground_resolution(geo[0], 7)

def testMapScale(self):
TileSystem.map_scale(45, 7, 45)
geo = (40., -105.)
level = 7
dpi = 96
scale = 3540913.0290224822
self.assertEqual(scale, TileSystem.map_scale(geo[0], level, dpi))

def testGeoToPixel(self):
geo = (-105, 45)
geo = (40., -105.)
level = 7
TileSystem.geo_to_pixel(geo, level)
pixel = (6827, 12405)
self.assertEqual(pixel, TileSystem.geo_to_pixel(geo, level))

def testPixelToGeo(self):
pixel = (50, 45)
pixel = (6827, 12405)
level = 7
TileSystem.pixel_to_geo(pixel, level)
geo = (40.002372, -104.996338)
self.assertEqual(geo, TileSystem.pixel_to_geo(pixel, level))

def testPixelToTile(self):
pixel = (50, 45)
TileSystem.pixel_to_tile(pixel)
pixel = (6827, 12405)
tile = (26, 48)
self.assertEqual(tile, TileSystem.pixel_to_tile(pixel))

def testTileToPixel(self):
tile = (3,3)
TileSystem.tile_to_pixel(tile)
tile = (26, 48)
pixel = (6656, 12288)
self.assertEqual(pixel, TileSystem.tile_to_pixel(tile))

def testTileToQuadkey(self):
tile = (3,3)
tile = (26, 48)
level = 7
TileSystem.tile_to_quadkey(tile, level)
key = "0231010"
self.assertEqual(key, TileSystem.tile_to_quadkey(tile, level))

def testQuadkeyToTile(self):
quadkey = '00'
TileSystem.quadkey_to_tile(quadkey)
pass

0 comments on commit 156974a

Please sign in to comment.