Skip to content

Commit

Permalink
cahnged QuadKey interface
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 02e9518 commit 82bd182
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 29 deletions.
9 changes: 5 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,9 @@ QuadKey

Quad key object used for Geospatial segmentation

from quadkey import QuadKey

quadkey = QuadKey(-105, 50, 17)
quadkey.lat
quadkey.lon
copy = QuadKey.from_str(quadkey.key)
qk = QuadKey((-105, 40) 17)
print qk.key # => 02310101232121212
assert qk.level is 17
copy = QuadKey.from_str(qk.key)
47 changes: 27 additions & 20 deletions quadkey/__init__.py
Original file line number Diff line number Diff line change
@@ -1,36 +1,43 @@
from util import precondition
from tile_system import TileSystem
from tile_system import TileSystem, valid_key

class QuadKey:

def __init__(self, geo, level):
# assert lat, lon and level are valid
self.key = QuadKey.get_quadkey(geo, level)
self.geo = geo
self.latitude = geo[0]
self.longitude = geo[1]
self.level = level
@precondition(lambda c, key: valid_key(key))
def __init__(self, key):
"""
A quadkey must be between 1 and 23 digits and can only contain digit[0-3]
"""
self.key = key
self.level = len(key)

def is_parent(self, parent):
return self != parent and parent.key[:len(self.key)-1] == self.key

def __eq__(self, other):
return self.key == other.key

def __ne__(self, other):
return not self.__eq__(other)

def __str__(self):
return self.key

def __repr__(self):
return self.key

@classmethod
def from_str(cls, key):
level = len(key)
geo = QuadKey.get_coordinates(key)
return cls(geo, level)

@staticmethod
def get_quadkey(geo, level):
def from_geo(geo, level):
"""
Constucts a quadkey representation from geo and level
geo => (lat, lon)
If lat or lon are outside of bounds, they will be clipped
If level is outside of bounds, an AssertionError is raised
"""
pixel = TileSystem.geo_to_pixel(geo, level)
tile = TileSystem.pixel_to_tile(pixel)
quadkey = TileSystem.tile_to_quadkey(tile, level)
return quadkey
key = TileSystem.tile_to_quadkey(tile, level)
return QuadKey(key)

@staticmethod
def get_coordinates(key):
pass

7 changes: 5 additions & 2 deletions quadkey/tile_system.py
Original file line number Diff line number Diff line change
@@ -1,18 +1,21 @@
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
@precondition(lambda key: valid_level(len(key)))
def valid_key(key):
return TileSystem.KEY_PATTERN.match(key) is not None

class TileSystem:
"""
Class with static method to build quadkeys from lat, lon, levels
see http://msdn.microsoft.com/en-us/library/bb259689.aspx
"""
import re
KEY_PATTERN = re.compile("^[0-3]+$")

EARTH_RADIUS = 6378137
LATITUDE_RANGE = (-85.05112878, 85.05112878)
Expand Down
15 changes: 12 additions & 3 deletions tests/quadkey_tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,11 @@
class QuadKeyTest(TestCase):

def testInit(self):
pass
qk = QuadKey('0321201120')
with self.assertRaises(AssertionError):
qk = QuadKey('')
with self.assertRaises(AssertionError):
qk = QuadKey('0156510012')

def testFromString(self):
pass
Expand All @@ -16,8 +20,13 @@ def testToString(self):
def testGetQuadKey(self):
geo = (40, -105)
level = 7
key = '0231010'
self.assertEqual(key, QuadKey.get_quadkey(geo, level))
key = QuadKey('0231010')
self.assertEqual(key, QuadKey.from_geo(geo, level))

def testGetCoordinates(self):
pass

def testIsParent(self):
child = QuadKey('0011')
parent = QuadKey('00')
self.assertTrue(child.is_parent(parent))

0 comments on commit 82bd182

Please sign in to comment.