Essentials
Eight things you meet in the first hour, one screen each. Every output below is produced when this page is built.
This is a map, not an argument. Each section links to the page where the reasoning lives.
import itacart
from itacart import geometry, interop, topology, index as ix
from shapely.geometry import Polygon
1. A point becomes a cell
cell = itacart.geo_to_cell(-46.6328862, -23.5508962, resolution=9)
cell
'SW(0476/0260(4(A2(4(A4(1(E5(4(D4)))))))))'
Longitude, latitude, resolution. Resolution 9 is one metre; the ladder runs 1 to 13, and 0 addresses the quadrant. → Point-to-Cell Indexing
2. A cell becomes geometry
(itacart.cell_to_centroid(cell),
len(itacart.cell_to_boundary(cell, close=True)),
itacart.cell_to_polygon(cell).geom_type)
((-46.632887116254246, -23.550899213284907), 5, 'Polygon')
The centroid is the cell’s representative position and the point every
single-point route answers with. cell_to_anchor gives the lattice corner the
index encodes — a different point, on the boundary, not a substitute.
→ Cell Geometry
3. A polygon becomes a cover
parcel = Polygon([(-46.6340, -23.5520), (-46.6330, -23.5520),
(-46.6330, -23.5500), (-46.6340, -23.5500)])
{mode: ix.count_cells(geometry.polyfill(parcel, 7, containment=mode))
for mode in ("contains", "center", "intersects")}
{'contains': 186, 'center': 226, 'intersects': 282}
Three definitions of inside, three covers, nested in that order. center is
the default and the unbiased one for area. Store the mode beside the cover
— it is not recoverable from the cells.
→ Containment Modes
4. A cover becomes GeoJSON
cover = geometry.polyfill(parcel, 7)
collection = interop.cells_to_geojson(cover)
(len(collection["features"]),
interop.recover_from_geojson(collection) == ix.decompose(cover))
(226, True)
The index travels in each Feature’s id, which is what makes recovery exact.
Rebuilding cells from the exported coordinates is a different operation and
is not promised to return them.
→ Geographic vs Grid Geometry
5. Moving up and down
parent = itacart.get_parent(cell)
(parent,
len(list(itacart.get_children(parent, flatten=True))),
ix.count_cells(itacart.compact_cells(cover)))
('SW(0476/0260(4(A2(4(A4(1(E5(4))))))))', 25, 154)
Ascent is truncation of the string. Descent is enumeration, not multiplication — at the domain border a cell can hold fewer children than the ratio predicts. Compaction folds a solid interior into coarse cells without touching geometry. → Parent, Children & Descendants
6. What comes back: one cell or many
An index names one cell or a whole set, and the return shape follows. This is the single largest source of surprise in the library.
The table below is built when this page is compiled, by calling each function twice — once with a single cell, once with a composite index of three — and reporting what came back.
one cell one composite index of three
get_parent str list[3] of str
get_ancestors list[9] of str list[3] of list
get_children list[1] of list list[3] of list
get_children(flatten=True) list[4] of str list[12] of str
grid_disk(k_distance=1) list[9] of str list[3] of list
cell_to_centroid tuple[2] list[3] of tuple
cell_to_boundary list[4] of tuple list[3] of list
is_equal_area_cell bool list[3] of bool
count_cells int int
compact_cells str str
Two rules and one exception.
A function answering one value per cell gives the bare value for a single
cell and a list aligned with decompose() order for a composite index. A
function answering many values per cell gives the flat list for a single
cell and a list of lists for a composite one.
get_children is the exception: it groups always, so
list(get_children(cell)) has length one and the children are inside it. Pass
flatten=True to read them directly.
→ Composition & Decomposition
7. Coordinate order
(itacart.cell_to_centroid(cell), # (longitude, latitude)
itacart.cell_to_sinusoidal(cell)) # (x, y) in metres on the plane
((-46.632887116254246, -23.550899213284907), (-4761300.0, -2605548.0))
Longitude first, then latitude, everywhere geodetic — the GeoJSON order,
and the opposite of how most people say it aloud. Shapely geometries you hand
in follow the same convention. cell_to_sinusoidal is the other space
entirely: metres on the projection plane, where the lattice is regular.
→ The Sinusoidal Construction
8. Limits, and where the area is safe
from itacart.exceptions import ITACaRTError
for lon, lat, res in [(181.0, 0.0, 9), (-46.6, -23.5, 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
Resolutions run 1 to 13. Longitudes past 180 are refused unless they are the eastern spelling of an extension zone. The package raises rather than returning a plausible value, because a plausible wrong answer is the one that reaches stored data.
Area is a count times a constant — except at the antemeridian and the poles, where cells do not carry the nominal area. The guard is never the shape name:
[(c, itacart.is_equal_area_cell(c))
for c in (cell, "NE(0000/0500)", "SE(1930/0196)", "NE(0000/1000)")]
[('SW(0476/0260(4(A2(4(A4(1(E5(4(D4)))))))))', True),
('NE(0000/0500)', True),
('SE(1930/0196)', False),
('NE(0000/1000)', False)]
An interior cell and a meridian triangle carry it; a border trapezoid and a
polar cap do not — and the polar cap is called a triangle. Ask
is_equal_area_cell, and use effective_cell_area when it answers False.
→ Known Boundary Exceptions
Where to go next
Example Notebooks work each of these through at length. Concepts & Guides argues them.