diff --git a/quadkey/__init__.py b/quadkey/__init__.py index bb07777..d0f50b1 100644 --- a/quadkey/__init__.py +++ b/quadkey/__init__.py @@ -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): @@ -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 diff --git a/quadkey/tile_system.py b/quadkey/tile_system.py index 2491eb4..dfd107b 100644 --- a/quadkey/tile_system.py +++ b/quadkey/tile_system.py @@ -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 @@ -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 @@ -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): diff --git a/tests/quadkey_tests.py b/tests/quadkey_tests.py index 895d703..b276289 100644 --- a/tests/quadkey_tests.py +++ b/tests/quadkey_tests.py @@ -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): diff --git a/tests/tile_system.py b/tests/tile_system.py index 527e928..b3280a1 100644 --- a/tests/tile_system.py +++ b/tests/tile_system.py @@ -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