GeoPatch
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.
Metadata
Release files for GeoPatch 1.2.9
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | Actions |
|---|---|---|---|---|
| geopatch-1.2.9.tar.gz | 13.8 kB | Without provenance | Details for geopatch-1.2.9.tar.gz |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Actions Reset |
|---|---|---|---|---|
| geopatch-1.2.9-py3-none-any.whl | Python 3 | none | any | Details for geopatch-1.2.9-py3-none-any.whl |
Total release size: 23.8 kB
Release files / geopatch-1.2.9.tar.gz
| Download URL | geopatch-1.2.9.tar.gz |
|---|---|
| Size | 13.8 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
eb51a6c9737388744e2216461e341ef02cd68f1421553b4ad8275f9a55d46e9f
|
|
BLAKE2b-256 checksum How to use checksums |
c66d5eb449cba9f683a831952a7db66f3d60ba5778707ca8f9579dc5972094c4
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/6.2.0 CPython/3.13.5
|
Release files / geopatch-1.2.9-py3-none-any.whl
| Download URL | geopatch-1.2.9-py3-none-any.whl |
|---|---|
| Size | 10.0 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
e7c220b49fb47ba44e6e34e1a23fd9d303aa9a2a89710bb4684353cc7fec931a
|
|
BLAKE2b-256 checksum How to use checksums |
27a43df0c3e9a3cec2eb6f038acc1d52257aa11a3617d019bb105f66963c4cc6
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/6.2.0 CPython/3.13.5
|
Security for GeoPatch 1.2.9
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
No provenance found
None of the files in this release were uploaded with provenance metadata.
A note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.
To learn more about protecting yourself from malicious actors, visit our package security documentation.