Created
January 5, 2012 18:19
-
-
Save ostronom/1566471 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
| class YandexEncoder(object): | |
| million = 10**6 | |
| points = [] | |
| alphabet = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-_=' | |
| def __init__(self, geom): | |
| #self._encode(geom) | |
| self.points = [ | |
| self.lift( (37.593578,55.735094) ), | |
| self.lift( (37.592159,55.732469) ), | |
| self.lift( (37.589374,55.734162) ) | |
| ] | |
| """ | |
| self.points = [ | |
| self.lift( (37.64,55.797924) ), | |
| self.lift( (37.499924,55.699554) ), | |
| self.lift( (37.843247,55.705758) ) | |
| ] | |
| """ | |
| def lift(self, t): | |
| return map(lambda w: int(self.million*w), t) | |
| def _encode(self, geom): | |
| if geom.geom_typeid == 0: | |
| self.points.append( self.lift(geom.coords) ) | |
| elif geom.geom_typeid in [1,2]: | |
| for x in geom.coords: | |
| self.points.append( self.lift(x) ) | |
| else: | |
| for x in xrange(geom.num_geom): | |
| self._encode(geom[x]) | |
| def toQWord(self, num): | |
| pholder = [0 for _ in range(0,32)] | |
| if num < 0: num += 4294967296 | |
| i = 1 | |
| while num > 0: | |
| num, r = divmod(num, 2) | |
| pholder[32-i] = r | |
| i += 1 | |
| return pholder | |
| def align(self, qword): | |
| return reduce(lambda a,c: c + a, [qword[i:i+8] for i in range(0,32,8)]) | |
| def toAlignedQWord(self, num): | |
| return self.align(self.toQWord(num)) | |
| def get_alpha(self, n): | |
| return self.alphabet[sum([d*(2**i) for i,d in enumerate(reversed(n))])] | |
| def encode(self): | |
| chunk_size = 6 | |
| result = [] | |
| encoded = [] | |
| begin = (0,0) | |
| for x,y in self.points: | |
| diffx, diffy = x - begin[0], y - begin[1] | |
| encoded.extend(self.toAlignedQWord(diffx)) | |
| encoded.extend(self.toAlignedQWord(diffy)) | |
| begin = (x, y) | |
| i = 0 | |
| while True: | |
| chunk = encoded[i:i+chunk_size] | |
| if len(chunk) < chunk_size: | |
| break | |
| print i, ':', i+chunk_size, '-' , chunk, ' -> ', sum([d*(2**z) for z,d in enumerate(reversed(chunk))]), self.get_alpha(chunk) | |
| result.append(self.get_alpha(chunk)) | |
| i += chunk_size | |
| diff = len(encoded) - i | |
| if diff > 0: | |
| diff = diff / 2 | |
| for p in range(0,diff): | |
| result.append('=') | |
| return ''.join(result) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment