vectorize#
- vectorize(xds: str | CloudPath | Path | tuple[ndarray | MaskedArray, dict] | DataArray | Dataset | DatasetReader | DatasetWriter, values: None | int | list = None, keep_values: bool = True, dissolve: bool = False, default_nodata: int = 0) GeoDataFrame [source]#
Vectorize a
xarray
to get the class vectors.If dissolved is False, it returns a GeoDataFrame with a GeoSeries per cluster of pixel value, with the value as an attribute. Else it returns a GeoDataFrame with a unique polygon.
Warning
Your data is casted by force into np.uint8, so be sure that your data is classified.
This could take a while as the computing time directly depends on the number of polygons to vectorize.
Please be careful.
- Parameters:
xds (AnyRasterType) – Path to the raster or a rasterio dataset or a xarray
values (Union[None, int, list]) – Get only the polygons concerning this/these particular values
keep_values (bool) – Keep the passed values. If False, discard them and keep the others.
dissolve (bool) – Dissolve all the polygons into one unique. Only works if values are given.
default_nodata (int) – Default values for nodata in case of non existing in file
- Returns:
Classes Vector
- Return type:
gpd.GeoDataFrame
Example
>>> raster_path = "path/to/raster.tif" >>> vec1 = vectorize(raster_path) >>> >>> # or >>> with rasterio.open(raster_path) as ds: >>> vec2 = vectorize(ds) >>> >>> # Assert those two approaches give the same result >>> vec1 == vec2 True