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.9.tar.gz (13.8 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.9-py3-none-any.whl (10.0 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: geopatch-1.2.9.tar.gz
  • Upload date:
  • Size: 13.8 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.9.tar.gz
Algorithm Hash digest
SHA256 eb51a6c9737388744e2216461e341ef02cd68f1421553b4ad8275f9a55d46e9f
MD5 5ff9c7cd8f0e829a0f6a889f05cfd155
BLAKE2b-256 c66d5eb449cba9f683a831952a7db66f3d60ba5778707ca8f9579dc5972094c4

See more details on using hashes here.

File details

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

File metadata

  • Download URL: geopatch-1.2.9-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.9-py3-none-any.whl
Algorithm Hash digest
SHA256 e7c220b49fb47ba44e6e34e1a23fd9d303aa9a2a89710bb4684353cc7fec931a
MD5 89baa9155905ed6720f78248dfcb47ed
BLAKE2b-256 27a43df0c3e9a3cec2eb6f038acc1d52257aa11a3617d019bb105f66963c4cc6

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