vectorize

Contents

vectorize#

vectorize(ds: str | tuple | DatasetReader, values: None | int | list = None, keep_values: bool = True, dissolve: bool = False, default_nodata: int = 0) GeoDataFrame[source]#

Vectorize a raster 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

  • Please only use this function on a classified raster.

  • This could take a while as the computing time directly depends on the number of polygons to vectorize.

    Please be careful.

Parameters:
  • ds (PATH_ARR_DS) – Path to the raster, its dataset, its xarray or a tuple containing its array and metadata

  • 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"  # Classified raster, with no data set to 255
>>> 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