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.
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.
WORLD = Quadtile(-180, 180, 90, -90) WORLD
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.
def quad_center(quad): x = (quad.xFrom + quad.xTo) / 2 y = (quad.yFrom + quad.yTo) / 2 return (x, y)
quad_center(WORLD)
(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
def quad_split_x(quad, right): mid = (quad.xFrom + quad.xTo) / 2 if right: xf = mid xt = quad.xTo else: 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))
(-90.0, 0.0)
(90.0, 0.0)
Now let’s do it in the other axis.
def quad_split_y(quad, bottom): mid = (quad.yFrom + quad.yTo) / 2 if not bottom: yf = mid yt = quad.yTo else: 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))
(0.0, -45.0)
(0.0, 45.0)
A simple encoding
blah blah blah
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) else: quad = quad_split_y(quad, bit) is_x = not is_x return quad_center(quad) decode_bits("01001") decode_bits("01100111")
(-112.5, 22.5)
(-56.25, 39.375)
Blah blah blah
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 else: bit = 0 quad = quad_split_x(quad, bit) else: if y > mid[1]: bit = 1 else: 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
HOUSE = (-8.577507, 52.664838)
def plot_path(path, zoom): print(f"Path length: {len(path)}") fig, ax = plt.subplots(figsize=(8, 8), dpi=100) ax.xaxis.set_visible(False) ax.yaxis.set_visible(False) xs = [0.5] ys = [0.5] bits = "" for bit in path: bits += bit x, y = decode_bits(bits) x, y = tilemapbase.project(x, y) xs.append(x) ys.append(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) plt.show()
plot_path(encode_location(HOUSE, 8), 0.55)
Path length: 8
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.
plot_path(encode_location(HOUSE, 16), 0.01)
Path length: 16
16 bits (2 bytes) gets us in the county, really close to the actual city. Let’s keep going.
plot_path(encode_location(HOUSE, 24), 0.008) plot_path(encode_location(HOUSE, 24), 0.0005)
Path length: 24