Classify rasters (KMeans, Random Forest, DN rules) and vectorise to polygons.
Project description
raster2poly
Classify rasters and vectorise the result to clean polygons — in three lines of code.
Supports unsupervised clustering (KMeans), supervised classification (Random Forest from ROI shapefiles), and rule-based DN thresholds. Outputs are dissolved, filtered GeoDataFrames ready for GIS.
Installation
pip install raster2poly
Quick start
Unsupervised (KMeans)
from raster2poly import RasterClassifier
clf = RasterClassifier("satellite_image.tif")
gdf = clf.unsupervised(n_clusters=6, algorithm="mini_batch_kmeans")
clf.save(gdf, "classes.gpkg")
Supervised (ROI shapefile)
gdf = clf.supervised("training_rois.shp", class_col="class_id")
clf.save(gdf, "supervised.shp")
The ROI file can contain Points or Polygons (or both). For polygons, every pixel inside the geometry is used as a training sample — far more robust than a single zonal mean.
Rule-based (DN ranges)
rules = {
1: [(4, 0.15, 1.0), (5, 0.0, 0.10)], # high Red, low NIR → built-up
2: [(5, 0.25, 1.0)], # high NIR → vegetation
}
gdf = clf.from_dn_ranges(rules)
Band numbers are 1-based. A pixel must satisfy all conditions in the list to be assigned that class.
Key improvements over the original script
| Issue in original | Fix |
|---|---|
point_query returns wrong shape for multi-band |
Replaced with per-pixel rasterised extraction |
| Only zonal mean used for polygon ROIs | Every pixel inside the polygon is a training sample |
Hardcoded 'class' column name |
Configurable class_col parameter |
| No polygon dissolve — millions of tiny fragments | dissolve=True by default, plus min_area filter |
rasterstats dependency for simple ops |
Replaced with rasterio.features.geometry_mask |
| No CRS check on ROI shapefile | Auto-reprojects vector → raster CRS |
| Output always Shapefile | Auto-detects .shp / .gpkg / .geojson |
| No nodata → NaN conversion | Nodata replaced with NaN on load, masked throughout |
Output format
The returned GeoDataFrame has two columns:
class_id(int) — the class labelgeometry— dissolved polygons
Save to any format: .shp, .gpkg, .geojson.
License
MIT
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file raster2poly-0.1.0.tar.gz.
File metadata
- Download URL: raster2poly-0.1.0.tar.gz
- Upload date:
- Size: 23.2 kB
- Tags: Source
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.12
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
380d4186de92bc57fed582e76901b56b53f03ed24a4c47233485d12c42fbe06e
|
|
| MD5 |
8b5fbaee846317c4772b870efd3db626
|
|
| BLAKE2b-256 |
2f1dd2f829813c9c26df312e84c1aad23cad1fd7eff2ebac5368b7b72220ca62
|
Provenance
The following attestation bundles were made for raster2poly-0.1.0.tar.gz:
Publisher:
release.yml on geoharkat/raster2poly
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
raster2poly-0.1.0.tar.gz -
Subject digest:
380d4186de92bc57fed582e76901b56b53f03ed24a4c47233485d12c42fbe06e - Sigstore transparency entry: 1273872659
- Sigstore integration time:
-
Permalink:
geoharkat/raster2poly@8dd6f0870d26ec2af9b3390bb658214f0c916695 -
Branch / Tag:
refs/tags/0.1.0 - Owner: https://github.com/geoharkat
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
release.yml@8dd6f0870d26ec2af9b3390bb658214f0c916695 -
Trigger Event:
push
-
Statement type:
File details
Details for the file raster2poly-0.1.0-py3-none-any.whl.
File metadata
- Download URL: raster2poly-0.1.0-py3-none-any.whl
- Upload date:
- Size: 9.2 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.12
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
3988bee97829d774c19a0160c3c518376eb6121d6ff4c3860be09dabe35ef247
|
|
| MD5 |
48410e402453c11073f4844796d452f3
|
|
| BLAKE2b-256 |
3f9c26f95c4ffa920d53afe80f3a21c0310e497cb0f0d06124f652374cf8853f
|
Provenance
The following attestation bundles were made for raster2poly-0.1.0-py3-none-any.whl:
Publisher:
release.yml on geoharkat/raster2poly
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
raster2poly-0.1.0-py3-none-any.whl -
Subject digest:
3988bee97829d774c19a0160c3c518376eb6121d6ff4c3860be09dabe35ef247 - Sigstore transparency entry: 1273872963
- Sigstore integration time:
-
Permalink:
geoharkat/raster2poly@8dd6f0870d26ec2af9b3390bb658214f0c916695 -
Branch / Tag:
refs/tags/0.1.0 - Owner: https://github.com/geoharkat
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
release.yml@8dd6f0870d26ec2af9b3390bb658214f0c916695 -
Trigger Event:
push
-
Statement type: