polygonize function on cpu for numpy-backed xarray DataArrays #585
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
This PR adds a cpu-based
polygonize
function for numpy-backedDataArray
s.The algorithm is slightly different to GDAL's in that it is simpler and has fewer memory reallocations, at the cost of requiring more RAM. It is placed in a new
xrspatial/experimental
directory as the API has not been finalised and in particular there are some optional dependencies (e.g,geopandas
) that we don't want to be required dependencies.The API follows that of
rasterio.features.shapes
but there are 4 possible return types that are specified using thereturn_type
kwarg. Possibilities are:numpy
(the default) - return polygons as numpy arrays, which is the fastest option.spatialpandas
- return aspatialpandas.GeoDataFrame
.geopandas
- return ageopandas.GeoDataFrame
. This option is only available ifgeopandas
andshapely
optional dependencies have been installed.awkward
- return polygons as anawkward
array (https://awkward-array.readthedocs.io/en/latest/index.html). This option is only available ifawkward
is installed.Here is some example code to run the same
raster
andmask
arrays through this algorithm and that of rasterio/GDAL, and display the results side-by-side using Matplotlib.Example output:

Closes #471