Index a Point

Turn a geodetic coordinate into a cell index, then get the geography back.

import itacart
itacart.__version__
'1.2.1'

One coordinate, one cell

A position in São Paulo, addressed at one-centimetre resolution:

LON, LAT = -46.6328862, -23.5508962
cell = itacart.geo_to_cell(LON, LAT, resolution=13)
cell
'SW(0476/0260(4(A2(4(A4(1(E5(4(D4(1(B2(4(B4)))))))))))))'

Longitude first, then latitude — the order GeoJSON uses, and the opposite of the order most people say out loud.

The string is not opaque. SW is the quadrant, 0476/0260 is the ten-kilometre cell in that quadrant’s Cartesian grid, and each parenthesis after it is one step finer. Index Structure reads it properly.

The same point at every resolution

for res in (1, 3, 5, 7, 9, 11, 13):
    print(res, "\t", itacart.geo_to_cell(LON, LAT, resolution=res))
1 	 SW(0476/0260)
3 	 SW(0476/0260(4(A2)))
5 	 SW(0476/0260(4(A2(4(A4)))))
7 	 SW(0476/0260(4(A2(4(A4(1(E5)))))))
9 	 SW(0476/0260(4(A2(4(A4(1(E5(4(D4)))))))))
11 	 SW(0476/0260(4(A2(4(A4(1(E5(4(D4(1(B2)))))))))))
13 	 SW(0476/0260(4(A2(4(A4(1(E5(4(D4(1(B2(4(B4)))))))))))))

Each line is a prefix of the next. That is the hierarchy — a coarser address is literally the start of the finer one, so no lookup is needed to go up a level.

Back to geography

lon, lat = itacart.cell_to_centroid(cell)
lon, lat
(-46.632886245130635, -23.550896188502815)

The centroid is the cell’s representative position — the one point every single-point route answers with. The round trip closes:

itacart.geo_to_cell(lon, lat, resolution=13) == cell
True

The anchor is a different point

itacart.cell_to_anchor(cell)
(-46.63288622920516, -23.550896143356816)

Close to the centroid at this resolution, and not the same thing. The anchor is the lattice corner the index literally encodes; it sits on the cell’s boundary, which is why it is not used as the representative position. Cells, Vertices, and Spatial Identity.

The whole cell

ring = itacart.cell_to_boundary(cell, close=True)
ring
[(-46.63288622920516, -23.550896143356816),
 (-46.632886327146664, -23.550896143356816),
 (-46.63288626105611, -23.550896233648814),
 (-46.63288616311461, -23.550896233648814),
 (-46.63288622920516, -23.550896143356816)]

Four vertices and a repeat of the first. As a Shapely polygon:

poly = itacart.cell_to_polygon(cell)
poly.geom_type, poly.is_valid
('Polygon', True)

How big is it, really

itacart.nominal_cell_area(13), itacart.is_equal_area_cell(cell)
(0.0001, True)

One square centimetre, and the equal-area guarantee holds for this cell. That second call is not ceremony — near the antemeridian and the poles it returns False, and then the nominal figure is the wrong number. Equal Area.

What the grid refuses

from itacart.exceptions import ITACaRTError
for lon_, lat_, res in [(181.0, 0.0, 9), (LON, LAT, 14)]:
    try:
        itacart.geo_to_cell(lon_, lat_, res)
    except ITACaRTError as exc:
        print(type(exc).__name__, "--", exc)
DomainError -- lon 181.0 outside [-180, 180], and not the eastern spelling of a position inside an extension zone
ResolutionError -- resolution 14 outside 1..13

Both refusals are deliberate. A position the grid cannot address is almost always a fault upstream, and answering it with the nearest plausible cell buries that fault in stored data.