How can I convert Bing's "quadtree" tile addresses to ZXY tile addresses in Python?
from functools import reduce # required in Python 3
def quad_to_xy(quadtree_coordinate):
return [reduce(lambda result, bit: (result << 1) | bit, bits, 0)
for bits in zip(*(reversed(divmod(digit, 2))
for digit in (int(c) for c in str(quadtree_coordinate))))]
or somewhat more readable:
from functools import reduce # required in Python 3
def quad_to_xy(quadtree_coordinate):
digits = (int(c) for c in str(quadtree_coordinate))
quadtree_path = (mod_div(digit, 2) for digit in digits)
x_path, y_path = zip(*quadtree_path)
return [bit_iterable_to_int(path) for path in (x_path, y_path)]
def mod_div(dividend, divisor):
return reversed(divmod(dividend, divisor))
# The following is inspired by http://stackoverflow.com/a/12461400/674064
def bit_iterable_to_int(iterable):
return reduce(append_bit, iterable, 0)
def append_bit(bits, bit):
return (bits << 1) | bit
usage:
quad_to_xy('203') # [1, 5]