{"id":8582,"library":"rasterstats","title":"rasterstats","description":"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.","status":"active","version":"0.20.0","language":"en","source_language":"en","source_url":"https://github.com/perrygeo/python-rasterstats","tags":["gis","geospatial","geographic","raster","vector","zonal statistics"],"install":[{"cmd":"pip install rasterstats","lang":"bash","label":"Install with pip"}],"dependencies":[{"reason":"Handles affine transformations for georeferencing.","package":"affine"},{"reason":"Command-line interface (CLI) toolkit.","package":"click"},{"reason":"Utilities for Click and GeoJSON.","package":"cligj"},{"reason":"Reads and writes vector geospatial data.","package":"fiona"},{"reason":"Core library for numerical operations on raster data.","package":"numpy"},{"reason":"Reads and writes raster geospatial data.","package":"rasterio"},{"reason":"Manipulates geometric objects.","package":"shapely"},{"reason":"Fast JSON encoder/decoder.","package":"simplejson"}],"imports":[{"symbol":"zonal_stats","correct":"from rasterstats import zonal_stats"},{"symbol":"point_query","correct":"from rasterstats import point_query"}],"quickstart":{"code":"import os\nfrom rasterstats import zonal_stats, point_query\n\n# This is a simplified example. In a real scenario, you'd load actual geospatial files.\n# For demonstration, we'll simulate inputs or refer to a common pattern.\n# Assuming 'polygons.shp' and 'elevation.tif' exist in a 'data' directory for this example.\n# You would replace these paths with your actual data file paths.\n\n# Example for zonal_stats\n# stats = zonal_stats(os.environ.get('VECTOR_PATH', 'data/polygons.shp'), \n#                     os.environ.get('RASTER_PATH', 'data/elevation.tif'), \n#                     stats=['min', 'max', 'mean', 'median'])\n# print('Zonal Stats for first polygon:', stats[0])\n\n# Example for point_query\n# point_geom = {'type': 'Point', 'coordinates': (245309.0, 1000064.0)} # Example coordinate\n# value = point_query(point_geom, os.environ.get('RASTER_PATH', 'data/elevation.tif'))\n# print('Point Query value:', value)\n\nprint(\"To run quickstart, ensure you have 'polygons.shp' and 'elevation.tif' files.\")\nprint(\"Uncomment the examples in the code to execute them.\")\nprint(\"For full quickstart, refer to the official documentation.\")","lang":"python","description":"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."},"warnings":[{"fix":"Upgrade your Python environment to 3.9 or newer before upgrading rasterstats to versions 0.19.0+.","message":"Python 2 support was dropped in version 0.19.0. Additionally, Python 3.8 support was dropped in version 0.20.0. The minimum required Python version is now 3.9.","severity":"breaking","affected_versions":"0.19.0, 0.20.0"},{"fix":"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.","message":"The `all_touched` parameter (default `False`) in `zonal_stats` can significantly impact results. With `all_touched=False`, only pixels whose centroids intersect a vector geometry are included. This may result in `None` statistics for very small polygons. Setting `all_touched=True` includes all pixels touched by the geometry, which can potentially bias results by including edge pixels not fully representative of the geometry.","severity":"gotcha","affected_versions":"All versions"},{"fix":"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.","message":"Encountering 'Width and Height must be > 0' errors or `NoneType` attribute errors often indicates issues with the spatial intersection between your vector geometries and the raster data. This can be caused by misaligned Coordinate Reference Systems (CRS), empty or invalid geometries in the vector layer, or geometries being too small to intersect any raster cells under the default `all_touched=False` setting.","severity":"gotcha","affected_versions":"All versions"},{"fix":"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.","message":"Processing very large rasters with complex MultiPolygon geometries (especially if spread out over vast areas) can lead to excessive memory consumption, potentially crashing the process. This is because `rasterstats` may attempt to read large sections of the raster into memory for processing.","severity":"gotcha","affected_versions":"All versions"},{"fix":"Upgrade `rasterstats` to version 0.17.0 or newer to ensure compatibility with recent versions of its core dependencies and suppress these warnings.","message":"Older versions of `rasterstats` (e.g., 0.14.0-0.16.0) issued deprecation warnings when used with newer versions of `shapely` (1.8+), `Affine`, and `numpy` (v1.16+).","severity":"deprecated","affected_versions":"<0.17.0"}],"env_vars":null,"last_verified":"2026-04-16T00:00:00.000Z","next_check":"2026-07-15T00:00:00.000Z","problems":[{"fix":"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.","cause":"The vector geometry does not sufficiently overlap with the raster, or the rasterization process results in a zero-sized internal raster representation for that geometry. This is often due to very small polygons, CRS mismatch, or invalid geometries.","error":"Error - Rasterize Geom - Width and Height must be > 0"},{"fix":"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())]`.","cause":"The input vector data contains null, empty, or otherwise invalid geometries that `rasterstats` attempts to process, leading to an unexpected `None` value.","error":"AttributeError: 'NoneType' object has no attribute 'crs' (or similar errors related to 'NoneType' geometries)"},{"fix":"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.","cause":"This appears to be a recent, open issue related to `numpy.distutils` being deprecated in Python 3.12 and causing import errors with `rasterstats` in some environments.","error":"Depreciated dependency on python 3.12 (numpy.distutils) causes error on import"}]}