API Referenceο
- class raster2poly.RasterClassifier(raster_path)[source]ο
Bases:
objectClassify a raster and vectorise the result to polygons.
- Parameters:
raster_path (path-like) β Multi-band GeoTIFF (or any GDAL-readable raster).
Examples
>>> clf = RasterClassifier("image.tif") >>> gdf = clf.unsupervised(n_clusters=6) >>> gdf.to_file("classes.shp")
- unsupervised(n_clusters=5, *, algorithm='kmeans', dissolve=True, min_area=0.0)[source]ο
K-Means or MiniBatchKMeans clustering.
- Parameters:
- Return type:
GeoDataFrame with
class_idandgeometrycolumns.
- supervised(roi_path, *, class_col='class_id', n_estimators=100, dissolve=True, min_area=0.0)[source]ο
Random Forest classification trained from an ROI shapefile.
The ROI file may 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.
- from_dn_ranges(rules, *, dissolve=True, min_area=0.0)[source]ο
Rule-based classification from digital-number thresholds.
- Parameters:
- Return type:
Example
>>> rules = { ... 1: [(4, 0.15, 1.0), (5, 0.0, 0.10)], # high Red, low NIR ... 2: [(5, 0.25, 1.0)], # high NIR ... } >>> gdf = clf.from_dn_ranges(rules)
- static available_algorithms()[source]ο
Print all supported classification algorithms.
Example
>>> RasterClassifier.available_algorithms()
- Return type:
None
- band_stats()[source]ο
Print min / max / mean / std for every band directly from the raster.
Example
>>> clf.band_stats()
- Return type:
None
- encode_roi(roi_path, label_col, *, output_path=None, id_col='class_id')[source]ο
Encode a string / categorical label column to consecutive integer IDs and save the result β no extra scripts required.
- Parameters:
roi_path (path-like) β Input shapefile / GeoPackage with a text label column.
label_col (str) β Column that holds the string class names (e.g.
"Age").output_path (path-like, optional) β Destination file. Defaults to
<stem>_encoded<suffix>next to the input file.id_col (str) β Name of the new integer-ID column added to the output (default
"class_id").
- Returns:
output_path (Path) β Path of the saved encoded file.
mapping (dict) β
{label_name: integer_id}β labels are sorted alphabetically and numbered from 1 (0 is reserved for nodata).
- Return type:
Example
>>> out, mapping = clf.encode_roi("ages2.shp", label_col="Age") >>> print(mapping) # {'Holocene': 1, 'Jurassic': 2, ...} >>> gdf_rf = clf.supervised(roi_path=out, class_col="class_id")