Skip to main content

Create rectangular pixel cutouts from GeoTIFFs centred on GeoJSON features, with COCO annotation output.

Project description

geotiff-cutout

Create rectangular pixel cutouts from a GeoTIFF centred on GeoJSON Polygons or Points, with an optional random shift. All intersecting annotations are returned in COCO format — boxes that extend beyond the cutout edge are clipped and flagged with is_truncated, Polygons will also be returned as segmentation. Classes must be indicated in the properties of the features.


Installation

pip install geotiff-cutout

Note: rasterio and fiona depend on GDAL. On most platforms the wheels ship GDAL bundled, so a plain pip install is sufficient. If you hit build errors, install via conda:

conda install -c conda-forge rasterio fiona
pip install geotiff-cutout

Quick start

Python API

from geotiff_cutout import make_cutout

# Rectangular cutouts with moderate random shift saved to disk
make_cutouts(
    "image.tif",
    "annotation.geojson",
    "data",
    coco_path="annotations.json",
    cutout_width_px=400,
    cutout_height_px=200,
    random_shift=0.5,
    method="fallback"
)

Command line

# Square cutouts, default settings
geotiff-cutout ortho.tif annotations.geojson

# Rectangular cutouts with custom shift and output directory
geotiff-cutout ortho.tif annotations.geojson \
    --width 640 --height 480 \
    --shift 0.8 \
    --output my_cutouts/

# Full help
geotiff-cutout --help

The random_shift parameter

Value Effect
0.0 Cutout centred exactly on the feature centroid.
0.5 Centroid can wander up to half the available slack.
1.0 Maximum shift: the target bbox may touch (but never cross) the cutout edge.

Shift is applied independently on each axis, so rectangular cutouts behave correctly — a wide cutout allows more absolute horizontal shift than a tall one.


COCO output format

Each call returns a standard COCO dict:

{
  "images": [{
    "id": 0
    "width": 512,
    "height": 512,
    "file_name": "cutout_<uuid>.png",
    "geotiff_source": "ortho.tif",
    "geojson_source": "annotations.geojson",
    "cutout_col_offset": 340,
    "cutout_row_offset": 120
  }],
  "categories": [{"id": 1, "name": "car"}],
  "annotations": [{
    "id": 0,
    "image_id": 0,
    "category_id": 1,
    "bbox": [12.0, 34.5, 80.0, 60.0],
    "bbox_mode": "xywh",
    "area": 4800.0,
    "iscrowd": 0,
    "is_truncated": false,
    "original_bbox": [12.0, 34.5, 80.0, 60.0],
    "segmentations":[[12.0, 34.5, ...,  80.0, 60.0]],
    "origina_segmentations":[[12.0, 34.5, ...,  80.0, 60.0]]
  }]
}

all coordinates in coco annotation are local image pixel coordinates bbox is the annotation clipped to the cutout boundary ([x, y, w, h] in local cutout pixels).
original_bbox is the full, unclipped box (may have negative coordinates or values exceeding the cutout dimensions for truncated boxes).
segmentation similar to bbox but Polygon segemnt if input was not Point. original_segmentation similar to original_bbox but Polygon segemnt if input was not Point. is_truncated is true when the box was clipped.

The category name is read from the first matching GeoJSON property key: categoryclasslabel"object" (fallback).


License

AGPL 3

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

geotiff_cutout-0.1.8.tar.gz (20.7 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

geotiff_cutout-0.1.8-py3-none-any.whl (22.0 kB view details)

Uploaded Python 3

File details

Details for the file geotiff_cutout-0.1.8.tar.gz.

File metadata

  • Download URL: geotiff_cutout-0.1.8.tar.gz
  • Upload date:
  • Size: 20.7 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.14.3

File hashes

Hashes for geotiff_cutout-0.1.8.tar.gz
Algorithm Hash digest
SHA256 0b304b3c1287e22bec185b7756cfda223b8327e45b5e0830f6d4646d63ad8c78
MD5 03dd24cd0c3439560b1919e7a31ff6a6
BLAKE2b-256 fcb63e31f9cc9e07814e09261eac2c9c10030559d46f47ddb33e114b013697b6

See more details on using hashes here.

File details

Details for the file geotiff_cutout-0.1.8-py3-none-any.whl.

File metadata

  • Download URL: geotiff_cutout-0.1.8-py3-none-any.whl
  • Upload date:
  • Size: 22.0 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.14.3

File hashes

Hashes for geotiff_cutout-0.1.8-py3-none-any.whl
Algorithm Hash digest
SHA256 f85296fa394bb7c31744b491e26a9d84cd969040cfa763f1b1cd8859039fa25e
MD5 20aaf7731db1304274f1d1fee035cddf
BLAKE2b-256 033b530a0fc9d757ed4c1773d145e067045b19d6abe0276022b9addcd653e24f

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page