Skip to main content

GeoPatch is a package for generating patches from remote sensing data

Project description

GeoPatch

PyPI version Downloads Github LinkedIn Twitter URL

GeoPatch Logo GeoPatch is a high-performance Python package designed for generating patches from remote sensing satellite imagery. It simplifies the preprocessing pipeline for deep learning tasks by automatically handling large rasters, clipping them into patches, and exporting them alongside corresponding labels for Semantic Segmentation and Object Detection (YOLO format).

GeoPatch is built on top of TerraTiff for lightweight, lightning-fast raster operations without the heavy GDAL dependencies.

🛠️ Installation

You can install GeoPatch directly from PyPI:

pip install GeoPatch

🚀 Tutorial & Usage

GeoPatch makes generating datasets for your computer vision models incredibly simple. It provides two main classes: TrainPatch (for generating training datasets) and PredictionPatch (for generating inference patches).

1. Generating Training Patches (TrainPatch)

You can generate datasets for either semantic segmentation or object detection.

from GeoPatch import TrainPatch

# Define your image and label paths
img_path = "path/to/your/satellite_image.tif"
lbl_path = "path/to/your/label_raster.tif"

# Initialize the patch generator
# patch_size: size of the square patches (e.g., 256x256)
# stride: sliding window step (overlap occurs if stride < patch_size)
patch_generator = TrainPatch(
    image=img_path, 
    label=lbl_path, 
    patch_size=256, 
    stride=128, 
    channel_first=True
)

Using Polygon Vector Data (Shapefiles / GeoJSON) as Labels

GeoPatch requires labels to be in a raster format (.tif or .npy). If your ground-truth labels are vector polygons (e.g., a .shp or .geojson file), you must rasterize them into a GeoTIFF mask before passing them to GeoPatch.

Here is how you can easily convert your polygons to a raster mask using geopandas and rasterio:

import geopandas as gpd
import rasterio
from rasterio.features import rasterize

# 1. Load the reference satellite image and the polygon shapefile
with rasterio.open("path/to/your/satellite_image.tif") as src:
    meta = src.meta.copy()
    transform = src.transform
    shape = (src.height, src.width)

gdf = gpd.read_file("path/to/your/polygons.shp")

# 2. Extract geometries and values (e.g., class IDs from a column like 'class_id')
# If you don't have a class column, you can use a default value (e.g., 1)
shapes = ((geom, value) for geom, value in zip(gdf.geometry, gdf['class_id']))

# 3. Rasterize the polygons into a mask array
mask = rasterize(
    shapes=shapes,
    out_shape=shape,
    transform=transform,
    fill=0,          # Background value
    all_touched=True,
    dtype=rasterio.uint8
)

# 4. Save the rasterized mask as a GeoTIFF
meta.update(count=1, dtype=rasterio.uint8)
with rasterio.open("path/to/your/label_raster.tif", 'w', **meta) as dst:
    dst.write(mask, 1)

# Now you can pass "path/to/your/label_raster.tif" to TrainPatch!

Semantic Segmentation Datasets

To generate image patches and their corresponding label masks:

# Generates semantic segmentation patches and saves them as GeoTIFFs
patch_generator.generate_segmentation(
    format="tif",                # Output format: "tif" or "npy"
    folder_name="seg_dataset",   # Output directory
    only_label=False             # Set to True to skip empty background patches
)

Object Detection Datasets (YOLO Format)

GeoPatch automatically converts your raster labels into YOLO-format bounding boxes .txt files.

# Generates image patches and YOLO bounding box text files
patch_generator.generate_detection(
    format="npy",                # Output format: "tif" or "npy"
    folder_name="det_dataset",   # Output directory
    only_label=True,             # Skips patches with no objects
    segmentation=True            # Set to True to also save segmentation masks alongside bboxes
)

2. Generating Inference Patches (PredictionPatch)

When you are ready to predict on a new satellite image, you can use PredictionPatch to slice the image into manageable pieces while preserving the geospatial metadata (GeoTransform, CRS) for seamless reconstruction later.

from GeoPatch import PredictionPatch

img_path = "path/to/new_area.tif"

# Initialize prediction patcher
pred_patch = PredictionPatch(
    image=img_path, 
    patch_size=512, 
    stride=512, 
    channel_first=True
)

# Export all patches as GeoTIFFs ready for model inference
pred_patch.save_Geotif(folder_name="inference_patches")

💡 Contact & Contributions

Any feedback from users is welcome! You can write to me at hejarshahabi@gmail.com in case of any contribution, bug reports, or suggestions.

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

geopatch-1.2.7.tar.gz (13.7 kB view details)

Uploaded Source

Built Distribution

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

geopatch-1.2.7-py3-none-any.whl (10.0 kB view details)

Uploaded Python 3

File details

Details for the file geopatch-1.2.7.tar.gz.

File metadata

  • Download URL: geopatch-1.2.7.tar.gz
  • Upload date:
  • Size: 13.7 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.13.5

File hashes

Hashes for geopatch-1.2.7.tar.gz
Algorithm Hash digest
SHA256 0e6d35c8d7803d55a792f75972e6595aa21f66d7dd3072cf882c5120a5ec498c
MD5 96bea949a2fddaa444e30883e7270788
BLAKE2b-256 c3f412abc57803e38c20d93fc6b1e0dad625e7d3d78ed6b4aeca5c78ad29f054

See more details on using hashes here.

File details

Details for the file geopatch-1.2.7-py3-none-any.whl.

File metadata

  • Download URL: geopatch-1.2.7-py3-none-any.whl
  • Upload date:
  • Size: 10.0 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.13.5

File hashes

Hashes for geopatch-1.2.7-py3-none-any.whl
Algorithm Hash digest
SHA256 467b30c914e74d76c6b31bceae61927fbe9b2101cd607c48cb9e26aefb2c8c8e
MD5 1b4b04a7b7706708ea70131533add0eb
BLAKE2b-256 aaf84bace1c104f3b37e30452dbcdeae8b2be316f9992800d65ab79871f21cee

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