Bing uses a quadtree-based addressing scheme to come up with a single number (or all-digit string) as the address of a tile. Each digit/character (starting from left / most significant) identifies the branch to take along the tree, starting from the root node. This gives one digit per (slippy-map) zoomlevel:

Google Maps, OpenStreetMap and others use the ZXY addressing scheme, where a tile is identified by 3 numbers:

  • the (slippy-map) zoomlevel (z, not to be confused with a z-coordinate like elevation)
  • the 0-based count of the tile from left (western) edge to the right (east) (x)
  • the 0-based count of the tile from upper (northern) edge down (south) (y)

Determining the z from a quadtree address is trivial, but how do I calculate x and y of the ZXY scheme from a quadtree address? I'm using 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


quad_to_xy('203')  # [1, 5]

There is a PyPI package available for this problem.

pip install pyGeoTile

From the Github project: https://github.com/geometalab/pyGeoTile


from pygeotile.tile import Tile

# (1, 5)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.