apbase.grid
Regular target-grid generation, shared by IDW and kriging interpolation.
This package exports the Grid class and a functional convenience wrapper
for generating regular point grids used as interpolation targets.
- class apbase.grid.Grid(*, boundary=None, resolution=20.0, chunk_size=1000000, hull_ratio=0.12, coordinate_transform=None, geographic_mode=None)
Bases:
objectGenerate regular target points inside a boundary.
The boundary either comes from the caller (
boundary=, or thefrom_wkt()/from_wkb()constructors) or is inferred as a concave hull ofx/ysource points (from_data_xy(), or plainfit(x, y)) when none is given.- Parameters:
boundary (Boundary | None) – Optional Shapely geometry used as the interpolation domain. When omitted, a concave hull is inferred from the finite input points.
resolution (float) – Grid spacing in the same units as the input coordinates.
chunk_size (int) – Maximum number of candidate points tested per chunk.
hull_ratio (float) – Ratio passed to
shapely.concave_hullwhen the boundary is inferred. Has no effect whenboundaryis supplied explicitly.coordinate_transform (CoordinateTransform | None) – Optional
CoordinateTransformapplied toboundarybefore building the grid, so it lands in the same frame asx/yalready converted withprepare_metric_xy. Takes priority overgeographic_mode.geographic_mode (str | None) –
None(default),"local", or"utm". Whenboundarylooks geographic andx/ydoes not, reprojectsboundaryto metric before building the grid.
- boundary
- resolution
- chunk_size
- hull_ratio
- coordinate_transform
- geographic_mode
- classmethod from_data_xy(x, y, **kwargs)
Create a grid and infer its boundary from x/y source points (concave hull).
Equivalent to
Grid(**kwargs).fit(x, y)with no explicitboundary. Usefrom_wkt(),from_wkb(), or passboundary=directly instead when the domain is already known – source points are not needed in that case.
- classmethod from_wkt(wkt, **kwargs)
Create a grid whose boundary is parsed from a WKT string.
No source points are needed: the grid is generated directly from the parsed geometry.
- classmethod from_wkb(wkb, **kwargs)
Create a grid whose boundary is parsed from WKB bytes.
No source points are needed: the grid is generated directly from the parsed geometry.
- property is_fitted: bool
Whether the grid points have already been generated.
- Returns:
Trueafterfit()succeeds.- Return type:
bool
- property points: ndarray
Generated target coordinates.
- Returns:
(n_points, 2)target coordinates inside the fitted boundary.- Return type:
numpy.ndarray
- property fitted_boundary: Boundary
Prepared boundary used to keep target points inside the domain.
- Returns:
Buffered and prepared boundary used by point-in-boundary tests.
- Return type:
shapely.Geometry
- property n_threads: int
Thread count used to parallelize boundary containment tests.
Resolved live from
apbase.config["n_threads"](see the class docstring) on every access – not cached at construction time, so it always reflects the current process-wide setting.- Returns:
Current thread count.
- Return type:
int
- fit(x=None, y=None)
Generate grid points.
When
boundarywas supplied to the constructor, the grid is built directly from it;x/yare optional in that case, used only to reconcileboundaryagainstcoordinate_transform/geographic_mode(see the class docstring) –x/yare never used to build the grid points themselves whenboundaryis given. Otherwisex/yare required: non-finite pairs are removed and a concave hull inferred from the rest becomes the boundary. Input coordinates are assumed to already be in the target projected coordinate system, unless converted viacoordinate_transform/geographic_mode.- Parameters:
x (array_like or None, default None) – Source coordinates used only when inferring a boundary or reconciling a provided boundary against a coordinate transform.
y (array_like or None, default None) – Source coordinates used only when inferring a boundary or reconciling a provided boundary against a coordinate transform.
- Returns:
This grid with generated
pointsand preparedfitted_boundary.- Return type:
- Raises:
ValueError – If no boundary can be inferred, the generated candidate grid is empty, or the candidate grid exceeds
apbase.config["grid_max_points_ceiling"].
- generate(x=None, y=None)
Fit the grid and return generated target coordinates.
- apbase.grid.create_point_grid(*, x=None, y=None, resolution, boundary=None, chunk_size=1000000, hull_ratio=0.12)
Create regular target coordinates, from a boundary or from
x/y.- Parameters:
x (array_like or None, default None) – One-dimensional source coordinates. Required only when
boundaryis omitted, to infer one as a concave hull of the finite pairs.y (array_like or None, default None) – One-dimensional source coordinates. Required only when
boundaryis omitted, to infer one as a concave hull of the finite pairs.resolution (float) – Grid spacing in the same coordinate unit as
xandy.boundary (object or None, default None) – Optional Shapely geometry used as interpolation boundary. When given,
x/yare not needed.chunk_size (int, default DEFAULT_CHUNK_SIZE) – Maximum number of candidate points tested per chunk.
hull_ratio (float, default DEFAULT_HULL_RATIO) – Ratio used by Shapely concave hull when
boundaryis omitted.
- Returns:
(n_points, 2)generated target coordinates inside the boundary.- Return type:
numpy.ndarray
- Raises:
ValueError – If
boundaryis omitted andx/ycannot infer a valid boundary, or if the candidate grid is empty or too large.