rasterstats is a Python module for summarizing geospatial raster datasets based on vector geometries, including functions for zonal statistics and interpolated point queries. It is currently at version 0.20.0, requires Python >=3.7, and maintains an active development cycle with several releases per year to keep up with dependencies and introduce new features.
pip install rasterstatsVerified import paths — ran on the pinned version, not inferred.
Demonstrates how to use `zonal_stats` to calculate summary statistics for raster data based on vector geometries (e.g., polygons) and `point_query` to retrieve raster values at specific point locations. The example placeholders for file paths need to be replaced with actual geospatial data.
Upgrade your Python environment to 3.9 or newer before upgrading rasterstats to versions 0.19.0+.
Understand the implications of `all_touched`. For small polygons, consider `all_touched=True` or pre-processing to ensure intersection. Always validate results, especially if polygon sizes vary greatly relative to raster pixel size.
Ensure vector and raster data share the same CRS. Filter out any null or empty geometries from your vector dataset. If polygons are very small, try `all_touched=True` in `zonal_stats` or increase the polygon size slightly for testing. Verify affine transformation for manually created rasters.
Consider splitting large MultiPolygon features into individual polygons or smaller subsets. For extremely large datasets, external tools or custom C++ solutions might be more memory efficient. Alternatively, ensure sufficient RAM is available or process data in chunks.
Upgrade `rasterstats` to version 0.17.0 or newer to ensure compatibility with recent versions of its core dependencies and suppress these warnings.
Verify CRS alignment between vector and raster. Remove or repair invalid/empty geometries. For small polygons, try `zonal_stats(..., all_touched=True)` or ensure polygons are large enough to cover at least one raster cell centroid.
Filter out invalid or empty geometries from your vector dataset before passing it to `rasterstats`. For example, when using GeoPandas: `gdf = gdf[~(gdf['geometry'].is_empty | gdf['geometry'].isna())]`.
As of current knowledge, this is an active issue in some setups with Python 3.12. Consider using Python 3.9, 3.10, or 3.11, or check the `rasterstats` GitHub issues for a resolution or workaround from the maintainers.