itacart.geometry.polyfill
- itacart.geometry.polyfill(geometry, resolution, containment='center', compact=False, n_jobs=1)[source]
Rasterise a geometry into a compositional index.
Descends the hierarchy over the sinusoidal plane rather than testing cells one by one, so the cost tracks the boundary rather than the area.
centerkeeps cells whose centre falls inside,intersectskeeps every cell touching the geometry, andcontainskeeps only cells wholly inside. The three nest,containsinsidecenterinsideintersects, and they nest by construction rather than by coincidence: see_accept_leaf(), and The vertices drawn on the lattice below for the one stepcentertakes alone.The centre, not the anchor. A cell’s anchor is a vertex and lies on its own border, so an anchor test would award a cell by a point it shares with three neighbours, and
containswould stop being a subset ofcenterfor every cell whose anchor sits on the outline. The centre is interior to the cell and has neither problem.A centre on the outline. The centre is tested one micrometre east and ten nanometres north of where it lies, on the sinusoidal plane. A centre exactly on the outline then falls on one side of it, and of two geometries sharing an edge exactly one keeps the cell: west and south edges keep the centres on them, east and north edges do not. Without the shift such a centre was kept by neither, and whether a centre landed exactly on the line was decided by the last bits of the projection, which differ between platforms. It is the common case for a polygon drawn through cell centroids, whose every vertex is a centre.
The vertices drawn on the lattice. Under
center, and only there, a projected vertex within a micrometre of a corner or centre of an ordinary cell – a point of the five-millimetre plane grid – or of a prime-meridian triangle’s centroid is moved onto it before anything is tested, so that such a polygon is read with the same numbers on every platform. A polygon the move makes invalid is refused.intersectsandcontainsread the vertices as projected; the three still nest, because the move is at most 1.4 micrometres and no cell’s centre lies that close to its own border.What the rule leaves, sized in
count_internal_cells(), comes in two kinds. Alike on every platform, it departs from the exact count of centres where an edge runs 1:100 north of east on the plane, the shift’s own direction, and keeps its centres on it, and where a centre lies nearer an edge than the shift, at most a micrometre, and changes side, tie or not; on a polygon drawn through cell centroids the second takes an edge longer than about a hundred metres whose lattice direction has no short step, and on any other polygon it is all the shift changes, a few cells at resolution 13. Still decided by the platform’s last bits: near the points densification adds to an edge of 505.1 m or more at resolution 13, one kilometre at the others, and near a vertex the move does not reach, which includes the centroids of the border-absorbing cells and the polar caps.Densification is applied, not assumed. A straight edge on the plane is not a geodesic, so an undensified long edge fills the wrong cells in between and the result is a plausible, wrong cell set with no error raised. Areal input is therefore densified first, at the threshold
_auto_segment()derives from the target resolution. Pass an already densified geometry and the step is idempotent.The prime-meridian column. Its cells are triangles straddling the line, so the ordinary descent cannot test them and a separate walk does. They carry the eastern spelling, which is the only one the grammar admits, and their containment is decided against the geometry before the quadrant split – the one figure that sees both halves of a cell the split cuts in two.
Every family is filled. The last lattice column of a row, which absorbs the border strip, the polar row and the two caps are not the sheared square the descent tests, and each is walked by a descent of its own rather than refused.
A geometry without area fills nothing under an areal predicate.
centerandcontainskeep a cell by its relation to an area, so a point or a line keeps no cell under them, and the refusal says that rather than guessing the geometry was empty.intersectskeeps the cells such a geometry touches.An areal geometry must be valid. A polygon or multipolygon that is not a valid geometry is refused before anything reads it, with the reason the geometry engine gives, and is never repaired: validity belongs to the geometry as written, not to where it lies. Geometries without area are not asked.
Workers. The fill runs in the calling thread, whatever
n_jobssays. Release 1.1.0 spread it over threads, and two of them made it slower than one – 3.9 times, for a one-degree box at resolution 5, measured on a two-core Linux machine with Python 3.12, Shapely 2.2 and GEOS 3.14 – for the reasoncount_internal_cells()gives: every scalar Shapely call handed the interpreter lock to the other thread. The parameter stays, validated as before, so that calls which pass it keep working. A batched descent of its own, which could use processes as the count does, is future work.- Parameters:
geometry (
BaseGeometry) – A Shapely geometry in EPSG:4326.resolution (
int) – Target resolution level, 1 to 13.containment (
Literal['center','intersects','contains'], default:"center") – Predicate deciding whether a cell is kept.compact (
bool, default:False) – Fold every node whose children were all kept, giving the same index as compacting the uniform fill afterwards withitacart.hierarchy.compact_cells()under the same containment mode. Returns a mixed-resolution index instead of a uniform one. Refused where the fill walks the border-absorbing family – where the geometry meets the strip the last lattice column of a row absorbs, or a polar cap – because the fold reads a uniform tree and that family has none.n_jobs (
int, default:1) – Accepted for compatibility and validated, an int of at least 1; the fill is serial for every value (see Workers).
- Return type:
- Returns:
A compositional index string covering the geometry.
- Raises:
AntemeridianError – If the geometry crosses 180 degrees outside an extension zone.
UnsupportedGeometryTypeError – On unsupported geometry types.
DomainError – If a position lies outside the latitude domain.
GeometryError – If a polygon or multipolygon is not a valid geometry, if the fill exceeds
MAX_FILL_CELLS, ifcompact=Truemeets the border-absorbing family, or if it keeps no cell: the geometry is empty, it has no area undercenterorcontains, or it is narrower than one cell under the chosen mode. The message names which. And from resolution 11, if the geometry reaches the cell that holds a pole at resolution 10, whose children cannot be enumerated: a known limitation, stated initacart.hierarchy.