TransWikia.com

How can I convert Bing's "quadtree" tile addresses to ZXY tile addresses in Python?

Geographic Information Systems Asked on April 28, 2021

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:

(source: s-msft.com)

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.

2 Answers

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]

Correct answer by das-g on April 28, 2021

There is a PyPI package available for this problem.

pip install pyGeoTile

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

Usage:

from pygeotile.tile import Tile

Tile.from_quad_tree('203').google
# (1, 5)

Answered by murthy10 on April 28, 2021

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP