Some tasks require to you to communicate or store your geographical location. In a local application, you can store it as two floating point numbers and never worry about anything else.

For this purpose, we can use Quadtiles. In our Python program, we can represent a quadtile as the four corners of a rectangular area. I’ll call these xFrom, xTo, yFrom and yTo, but it might be more accurate to use terminology like latitude and longitude.

In [2]:
Quadtile = namedtuple("Quadtile", "xFrom xTo yFrom yTo")

Quadtiles are about splitting the current Quad into 4 tiles, picking one of them and repeating. Since we want to be able to point to any location on Earth, our initial Quad will always be the whole world.

In [3]:
WORLD = Quadtile(-180, 180, 90, -90)

Out [3]:
Quadtile(xFrom=-180, xTo=180, yFrom=90, yTo=-90)

Operations on Quads

Getting the center point

When working with locations, we often need a single point instead of a Quad, so let’s write a function to get the center point. We will calculate this by getting the midpoint value of the X and Y ranges.

In [4]:
def quad_center(quad):
    x = (quad.xFrom + quad.xTo) / 2
    y = (quad.yFrom + quad.yTo) / 2
    return (x, y)
In [5]:
Out [5]:
(0.0, 0.0)

Makes sense, the center of the world is at (0, 0). This is the value we will get when we try to compute the location represented by the empty string, i.e. the default location.

Splitting quads

In [6]:
def quad_split_x(quad, right):
    mid = (quad.xFrom + quad.xTo) / 2
    if right:
        xf = mid
        xt = quad.xTo
        xf = quad.xFrom
        xt = mid
    return Quadtile(xf, xt, quad.yFrom, quad.yTo)

quad_center(quad_split_x(WORLD, False))
quad_center(quad_split_x(WORLD, True))
Out [6]:
(-90.0, 0.0)
Out [6]:
(90.0, 0.0)

Now let’s do it in the other axis.

In [7]:
def quad_split_y(quad, bottom):
    mid = (quad.yFrom + quad.yTo) / 2
    if not bottom:
        yf = mid
        yt = quad.yTo
        yf = quad.yFrom
        yt = mid
    return Quadtile(quad.xFrom, quad.xTo, yf, yt)

quad_center(quad_split_y(WORLD, False))
quad_center(quad_split_y(WORLD, True))
Out [7]:
(0.0, -45.0)
Out [7]:
(0.0, 45.0)

A simple encoding

blah blah blah

In [8]:
def decode_bits(bits):
    is_x = True
    quad = WORLD
    for bit in bits:
        bit = int(bit)
        if is_x:
            quad = quad_split_x(quad, bit)
            quad = quad_split_y(quad, bit)
        is_x = not is_x
    return quad_center(quad)

Out [8]:
(-112.5, 22.5)
Out [8]:
(-56.25, 39.375)

Blah blah blah

In [10]:
def encode_location(coord, N):
    bits = ""
    quad = WORLD
    x, y = coord
    is_x = True
    for i in range(N):
        mid = quad_center(quad)
        if is_x:
            if x > mid[0]:
                bit = 1
                bit = 0
            quad = quad_split_x(quad, bit)
            if y > mid[1]:
                bit = 1
                bit = 0
            quad = quad_split_y(quad, bit)
        bits += str(bit)
        is_x = not is_x
    return bits

Let’s make a plot of how accurate our positions gets with more bits added. Lower numbers are more accurate.

Map visualizations

In [11]:
HOUSE = (-8.577507, 52.664838)
In [12]:
def plot_path(path, zoom):
    print(f"Path length: {len(path)}")
    fig, ax = plt.subplots(figsize=(8, 8), dpi=100)

    xs = [0.5]
    ys = [0.5]

    bits = ""
    for bit in path:
        bits += bit
        x, y = decode_bits(bits)
        x, y = tilemapbase.project(x, y)
    extent = tilemapbase.Extent.from_centre_lonlat(*decode_bits(bits), xsize=zoom)
    extent = extent.to_aspect(1.0)
    plotter = tilemapbase.Plotter(extent, t, width=400)
    plotter.plot(ax, t)
    _ = ax.plot(xs, ys)
In [13]:
plot_path(encode_location(HOUSE, 8), 0.55)
Path length: 8
<Figure size 800x800 with 1 Axes>

We can see that 8 bits is enough to get us next to the correct country. Let’s do the same for getting to the city.

In [14]:
plot_path(encode_location(HOUSE, 16), 0.01)
Path length: 16
<Figure size 800x800 with 1 Axes>

16 bits (2 bytes) gets us in the county, really close to the actual city. Let’s keep going.

In [15]:
plot_path(encode_location(HOUSE, 24), 0.008)
plot_path(encode_location(HOUSE, 24), 0.0005)
Path length: 24