Geometry¶
geotoolz.geom
¶
Geometry, projection, tiling, mosaicking, rasterization, and vectorization.
CropTo
¶
Bases: Operator
Crop a GeoTensor to a target spatial shape.
Anchors either at the centre (default) or upper-left corner of the
input. The affine transform is updated through
:meth:georeader.geotensor.GeoTensor.read_from_window so the
geographic extent of the surviving pixels is preserved.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
shape
|
tuple[int, int]
|
Target spatial shape |
required |
anchor
|
str
|
|
'center'
|
Examples:
>>> import geotoolz as gz
>>> # Centre-crop a 1024×1024 scene to a 512×512 chip.
>>> chip = gz.geom.CropTo(shape=(512, 512))(scene)
Source code in src/geotoolz/geom/_src/operators.py
CropToBounds
¶
Bases: Operator
Crop a GeoTensor to a geographic bounding box.
Computes the pixel window covering bounds via
:func:rasterio.windows.from_bounds (after reprojecting bounds
into the carrier's CRS if crs differs from it) and reads the
intersection with the carrier. Non-intersecting bounds raise
:class:rasterio.windows.WindowError via
:meth:georeader.geotensor.GeoTensor.read_from_window.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
bounds
|
tuple[float, float, float, float]
|
|
required |
crs
|
str | None
|
CRS of |
None
|
Examples:
>>> import geotoolz as gz
>>> # Crop a global mosaic to a small AOI in geographic coords.
>>> aoi = gz.geom.CropToBounds(
... bounds=(-10.0, 35.0, -8.0, 36.5), crs="EPSG:4326"
... )
>>> patch = aoi(global_scene)
Source code in src/geotoolz/geom/_src/operators.py
Georeference
¶
Bases: Operator
Georeference swath data using a georeader GLT GeoTensor.
Thin wrapper around :func:georeader.griddata.georreference (note
the upstream spelling). The GLT (Geolocation Lookup Table) maps
output-grid pixels to source-sensor pixels for fast, exact
orthorectification with no resampling artifacts.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
glt
|
GeoTensor
|
GLT |
required |
valid_glt
|
ndarray | None
|
Optional boolean mask of valid GLT pixels. |
None
|
Note
Carries a concrete GeoTensor and (optionally) a numpy array
reference, so flagged forbid_in_yaml = True.
Examples:
>>> import geotoolz as gz
>>> # Orthorectify a hyperspectral scene predicted in sensor space.
>>> ortho = gz.geom.Georeference(glt=glt)(prediction_swath)
Source code in src/geotoolz/geom/_src/operators.py
Mosaic
¶
Bases: Operator
Mosaic multiple GeoTensors onto a single grid.
For method="first" this is a thin wrapper around
:func:georeader.mosaic.spatial_mosaic: rasters are processed in
order and the first valid pixel wins. For all other methods the
function is still used to compute the frame (union of extents
+ first raster's CRS / dtype), then every input is reprojected onto
that frame and the per-pixel reduction is applied:
.. math::
\hat{x}_{ij} \;=\; \mathrm{agg}\!\left(
\{ x^{(k)}_{ij} \;|\; x^{(k)}_{ij} \neq \text{fill} \}
\right)
with agg chosen from mean, median, max, min.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
method
|
str
|
|
'first'
|
resampling
|
str
|
Resampling mode used for reprojection onto the mosaic frame. |
'bilinear'
|
Examples:
>>> import geotoolz as gz
>>> # Median-composite a stack of overlapping S2 scenes.
>>> composite = gz.geom.Mosaic(method="median")(s2_scene_list)
Source code in src/geotoolz/geom/_src/operators.py
PadTo
¶
Bases: Operator
Pad a GeoTensor up to a target spatial shape.
Pads the spatial axes ("y", "x") symmetrically so the output
has at least shape pixels along each spatial axis. If the
input already exceeds the target on an axis, that axis is left
alone (no truncation — pair with :class:CropTo for that).
The affine transform is updated by GeoTensor.pad so the
geographic origin of the existing data does not move; new rows
appear to the top/bottom and new columns to the left/right.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
shape
|
tuple[int, int]
|
Target minimum spatial shape |
required |
mode
|
str
|
Numpy pad mode. |
'constant'
|
fill
|
float | None
|
Constant value when |
None
|
Examples:
>>> import geotoolz as gz
>>> # Pad to a multiple of 256 before tiling.
>>> snug = gz.geom.PadTo(shape=(2048, 2048), fill=0)(scene)
Source code in src/geotoolz/geom/_src/operators.py
Rasterize
¶
Bases: Operator
Rasterize geometries onto the input GeoTensor's grid.
Burns geometries into a fresh array aligned with the input's
CRS and transform. Accepts either:
- a
listof shapely geometries (each gets value1); or - a :class:
geopandas.GeoDataFrame(thecolumnargument selects which attribute supplies the burn-in value, default1.0).
Delegates to :func:georeader.rasterize.rasterize_geopandas_like
or :func:georeader.rasterize.rasterize_geometry_like.
Note
Carries Python-level geometry objects (shapely / geopandas),
so flagged forbid_in_yaml = True.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
geometries
|
list[BaseGeometry] | GeoDataFrame
|
Shapely geometries or a |
required |
column
|
str | None
|
For |
None
|
all_touched
|
bool
|
Whether to mark every pixel the geometry touches (vs. only those whose centre is inside). |
False
|
fill
|
float
|
Background value. Default |
0.0
|
Examples:
>>> import geotoolz as gz
>>> from shapely.geometry import box
>>> aoi_mask = gz.geom.Rasterize(geometries=[box(*aoi_bounds)])(scene)
Source code in src/geotoolz/geom/_src/operators.py
RasterizeLike
¶
Bases: Operator
Rasterize geometries onto a stored reference grid.
Same as :class:Rasterize but the reference grid is pinned at
construction (like) rather than supplied per call. The
operator takes no positional input (it is a producer), or accepts
a GeoTensor that is ignored — useful as the head of a Sequential
pipeline that consumes the burned mask.
Note
Flagged forbid_in_yaml = True.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
like
|
GeoTensor
|
Reference |
required |
geometries
|
GeoDataFrame
|
Shapely geometries or a |
required |
column
|
str | None
|
Attribute column for the burn-in value, or |
None
|
all_touched
|
bool
|
As in :class: |
False
|
fill
|
float
|
Background value. |
0.0
|
Examples:
>>> import geotoolz as gz
>>> burner = gz.geom.RasterizeLike(
... like=ref_scene, geometries=polygons_gdf, column="class_id"
... )
>>> labels = burner()
Source code in src/geotoolz/geom/_src/operators.py
Reproject
¶
Bases: Operator
Reproject a GeoTensor to a destination CRS (and optional resolution).
Thin Operator wrapping :func:georeader.read.read_to_crs. The
destination grid is anchored to the input's bounding box reprojected
into dst_crs; the resolution defaults to the input's pixel size
expressed in the destination CRS.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
dst_crs
|
str
|
Target CRS as a string (EPSG code, WKT, PROJ string, or
anything :class: |
required |
resolution
|
tuple[float, float] | None
|
Optional |
None
|
resampling
|
str
|
One of |
'bilinear'
|
Examples:
>>> import geotoolz as gz
>>> # Reproject a UTM scene into geographic coords for plotting.
>>> reproj = gz.geom.Reproject(
... dst_crs="EPSG:4326", resampling="bilinear"
... )
>>> geographic = reproj(utm_scene)
Source code in src/geotoolz/geom/_src/operators.py
ReprojectLike
¶
Bases: Operator
Reproject a GeoTensor onto another GeoTensor's CRS + grid.
Wraps :func:georeader.read.read_reproject_like: the output has the
same CRS, transform, and spatial shape as like. Useful for
aligning auxiliary data (DEMs, masks, predictions from another
sensor) onto a reference scene grid before downstream pipelines.
Note
like is a concrete GeoTensor, so this Operator is
flagged forbid_in_yaml = True.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
like
|
GeoTensor
|
The reference |
required |
resampling
|
str
|
See :class: |
'bilinear'
|
Examples:
>>> import geotoolz as gz
>>> # Align a coarse cloud mask onto the scene grid.
>>> aligned_mask = gz.geom.ReprojectLike(
... like=scene, resampling="nearest"
... )(coarse_mask)
Source code in src/geotoolz/geom/_src/operators.py
Resample
¶
Bases: Operator
Resample a GeoTensor to a target spatial resolution.
Delegates to :meth:georeader.geotensor.GeoTensor.resize via the
resolution_dst argument. The output shape is implied by the
ratio of the input pixel size to the requested resolution.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
resolution
|
tuple[float, float]
|
Target |
required |
resampling
|
str
|
Interpolation mode. Default |
'bilinear'
|
anti_aliasing
|
bool
|
Whether to anti-alias before downscaling.
Default |
True
|
Examples:
>>> import geotoolz as gz
>>> # Downsample a 10 m raster to 30 m.
>>> coarse = gz.geom.Resample(resolution=(30.0, 30.0))(fine_10m)
Source code in src/geotoolz/geom/_src/operators.py
ResampleLike
¶
Bases: ReprojectLike
Resample a GeoTensor onto another GeoTensor's spatial grid.
Alias of :class:ReprojectLike. The intent of the rename is purely
pipeline-readability: in workflows where both inputs already share a
CRS, ResampleLike reads as "match this grid" rather than
"warp into a new projection".
Examples:
>>> import geotoolz as gz
>>> # Downsample a 10 m S2 band onto a 20 m reference grid.
>>> rs = gz.geom.ResampleLike(like=s2_20m, resampling="average")
>>> b04_20m = rs(b04_10m)
Source code in src/geotoolz/geom/_src/operators.py
Resize
¶
Bases: Operator
Resize a GeoTensor to a target spatial shape.
Delegates to :meth:georeader.geotensor.GeoTensor.resize, which
re-bases the affine transform so the geographic extent is preserved
while the pixel grid is densified or coarsened. anti_aliasing
applies a Gaussian pre-filter when downscaling (recommended).
Note
The interpolation name is mapped onto
:func:skimage.transform.resize's conventions
("bilinear", "bicubic", "nearest").
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
shape
|
tuple[int, int]
|
Target spatial shape |
required |
anti_aliasing
|
bool
|
Whether to anti-alias before downscaling.
Default |
True
|
resampling
|
str
|
Interpolation mode. Default |
'bilinear'
|
Examples:
Source code in src/geotoolz/geom/_src/operators.py
SlidingWindow
¶
Bases: Tile
Split a GeoTensor into overlapping sliding-window tiles.
Convenience subclass of :class:Tile that takes a scalar
overlap (in pixels) and derives the stride. Equivalent to
Tile(size=size, stride=(size[0] - overlap, size[1] - overlap)).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
size
|
tuple[int, int]
|
Tile spatial shape |
required |
overlap
|
int
|
Overlap in pixels along both axes. Default |
0
|
Examples:
>>> import geotoolz as gz
>>> # 256x256 tiles, 32-pixel overlap on every side.
>>> tiles = gz.geom.SlidingWindow(size=(256, 256), overlap=32)(scene)
Source code in src/geotoolz/geom/_src/operators.py
Stitch
¶
Bases: Operator
Stitch georeferenced tiles back into one GeoTensor.
Blends a list[GeoTensor] (typically the output of :class:Tile)
onto a unioning north-up grid, masking out the
fill_value_default sentinels that :class:Tile introduces
along the bottom / right edges with boundless=True.
Supported blend modes:
"average"— unweighted mean of overlapping pixels."feather"— overlap-add with cosine-edge weights, kernel from :func:geotoolz.geom._src.array.feather_weights."first"— first tile wins on overlap."max"— element-wise maximum across overlapping tiles.
The mathematical model for "average" / "feather" is the
standard overlap-add reconstruction:
.. math::
\hat{x}_{ij} \;=\; \frac{\sum_k w_k(i, j)\, m_k(i, j)\, x_k(i, j)}
{\sum_k w_k(i, j)\, m_k(i, j)}
where m_k is the validity mask of tile k and w_k is the
weight kernel (1 everywhere for "average", the feather kernel
otherwise). Pixels with no valid contributor are filled with
fill (default: the first tile's fill_value_default).
Note
Set forbid_in_yaml = True when target_transform is
supplied — :class:affine.Affine is not JSON-safe. The auto-
derived (bbox-union) path round-trips cleanly through hydra-zen.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
blend
|
str
|
Blend mode (see above). Default |
'average'
|
feather_width
|
int
|
Pixel ramp width for |
16
|
target_shape
|
tuple[int, int] | None
|
Optional |
None
|
target_transform
|
Affine | None
|
Optional :class: |
None
|
target_crs
|
str | None
|
Optional CRS override for the output. Defaults to the first tile's CRS. |
None
|
fill
|
float | int | None
|
Output fill value. |
None
|
Examples:
>>> import geotoolz as gz
>>> # Round-trip a tiled inference pipeline.
>>> tiles = gz.geom.Tile(size=(256, 256))(scene)
>>> predictions = [model(tile) for tile in tiles]
>>> stitched = gz.geom.Stitch(blend="feather", feather_width=16)(
... predictions
... )
Source code in src/geotoolz/geom/_src/operators.py
489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 | |
Tile
¶
Bases: Operator
Split a GeoTensor into spatial tiles.
Wraps :func:georeader.slices.create_windows and then reads each
window with boundless=True so the trailing-edge tiles match
size exactly. Out-of-bounds areas are padded with the carrier's
fill_value_default; use :func:Stitch (which masks the
sentinel) to recover the original extent.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
size
|
tuple[int, int]
|
Tile spatial shape |
required |
stride
|
tuple[int, int] | None
|
Step between consecutive tile origins |
None
|
include_incomplete
|
bool
|
Whether to keep edge tiles that don't fully
fit (still emitted at |
True
|
Examples:
>>> import geotoolz as gz
>>> # 512x512 tiles with 64-pixel overlap.
>>> tiles = gz.geom.Tile(size=(512, 512), stride=(448, 448))(scene)
Source code in src/geotoolz/geom/_src/operators.py
Vectorize
¶
Bases: Operator
Vectorize non-zero regions of a mask GeoTensor into polygons.
Wraps :func:georeader.vectorize.get_polygons. The carrier's
transform is used to express polygons in geographic coordinates.
Polygons with area below min_area (in square pixels of the
mask) are dropped.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
min_area
|
float
|
Minimum polygon area in square pixels. Default
|
25.5
|
simplify_tolerance
|
float | None
|
Optional post-hoc :meth: |
None
|
Examples:
Source code in src/geotoolz/geom/_src/operators.py
Projection operator choices¶
Reprojectchanges CRS and optionally resolution.ReprojectLikematches anotherGeoTensor's CRS, transform, and shape.ResampleLikeis the same grid-matching surface for resolution/alignment work where the CRS is already compatible.