Fill a Parcel
Cover a polygon with cells, under each definition of “inside”, and see what the choice costs.
import itacart
from itacart import geometry, index as ix
from shapely.geometry import Polygon
parcel = Polygon([(-46.6340, -23.5520), (-46.6330, -23.5520),
(-46.6330, -23.5500), (-46.6340, -23.5500)])
parcel.bounds
(-46.634, -23.552, -46.633, -23.55)
About a hundred metres by two hundred.
Densify first
A straight edge on the plane is not a geodesic, so a long edge that has not gained intermediate vertices fills the wrong cells in the middle — and does it without raising anything.
dense = geometry.densify_orthodromic(parcel, max_segment_m=5.0)
len(parcel.exterior.coords), len(dense.exterior.coords)
(5, 133)
polyfill does this itself at a threshold it derives from the resolution.
Doing it explicitly makes the threshold a decision instead of a default, and
the step is idempotent:
len(geometry.densify_orthodromic(dense, max_segment_m=5.0).exterior.coords)
133
Three covers, three answers
covers = {mode: geometry.polyfill(dense, resolution=7, containment=mode)
for mode in ("contains", "center", "intersects")}
{mode: ix.count_cells(c) for mode, c in covers.items()}
{'contains': 186, 'center': 226, 'intersects': 282}
They nest — contains inside center inside intersects — and the nesting is
a property of how acceptance is decided, not a coincidence of this parcel:
sets = {m: set(ix.decompose(c)) for m, c in covers.items()}
sets["contains"] <= sets["center"] <= sets["intersects"]
True
Between the tightest and the loosest is more than a quarter of the cells. A stored cover that does not record which mode produced it cannot be compared with another one. Containment Modes.
Counting without naming
If the answer you want is an area, naming the cells is wasted work:
n = geometry.count_internal_cells(dense, 7)
n, n * itacart.nominal_cell_area(7)
(226, 22600.0)
Square metres directly — no conversion factor, because the cells are equal-area and the areas are decimal.
Refining, and knowing when to stop
[(res,
geometry.count_internal_cells(dense, res),
geometry.count_internal_cells(dense, res) * itacart.nominal_cell_area(res))
for res in (5, 7, 9, 11)]
[(5, 3, 30000.0),
(7, 226, 22600.0),
(9, 22563, 22563.0),
(11, 2261552, 22615.52)]
The estimate settles. The cell count does not — it is multiplied by a hundred every two levels — so past the resolution your source mapping supports you are buying storage rather than accuracy. Choosing Resolution from Mapping Scale.
Compact before storing
cover = covers["center"]
compact = itacart.compact_cells(cover)
ix.count_cells(cover), ix.count_cells(compact)
(226, 154)
Identical ground, a fraction of the cells: the solid interior folds into coarse cells and the fine ones stay where the information is, along the boundary.
Compaction never changes the ground: a parent replaces its children only when all of them are present. “All” means the children the parent actually has, which at the domain border include one spelled under the next column:
border_children = list(itacart.get_children("NE(2003/0000)", flatten=True))
len(border_children), itacart.compact_cells(ix.compose(border_children))
(5, 'NE(2003/0000)')
Five children fold into one parent, and the fifth is spelled under
NE(2004/0000), a prefix that names no cell — so a parcel on the border
compacts exactly as an interior one does.
Compaction & Normalization
has the case where the fold must not happen.
When a parcel is smaller than a cell
from itacart.exceptions import ITACaRTError
tiny = Polygon([(-46.63400, -23.55200), (-46.63399, -23.55200),
(-46.63399, -23.55199), (-46.63400, -23.55199)])
try:
geometry.polyfill(tiny, resolution=5)
except ITACaRTError as exc:
print(type(exc).__name__, "--", exc)
GeometryError -- geometry covers no cell at this resolution: it is narrower than one cell under 'center'
The package raises rather than returning an empty cover. An empty cover read as “no overlap” is the kind of error that reaches a land registry.