rasterstats

0.20.0 · active · verified Thu Apr 16

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.

Common errors

Warnings

Install

Imports

Quickstart

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.

import os
from rasterstats import zonal_stats, point_query

# This is a simplified example. In a real scenario, you'd load actual geospatial files.
# For demonstration, we'll simulate inputs or refer to a common pattern.
# Assuming 'polygons.shp' and 'elevation.tif' exist in a 'data' directory for this example.
# You would replace these paths with your actual data file paths.

# Example for zonal_stats
# stats = zonal_stats(os.environ.get('VECTOR_PATH', 'data/polygons.shp'), 
#                     os.environ.get('RASTER_PATH', 'data/elevation.tif'), 
#                     stats=['min', 'max', 'mean', 'median'])
# print('Zonal Stats for first polygon:', stats[0])

# Example for point_query
# point_geom = {'type': 'Point', 'coordinates': (245309.0, 1000064.0)} # Example coordinate
# value = point_query(point_geom, os.environ.get('RASTER_PATH', 'data/elevation.tif'))
# print('Point Query value:', value)

print("To run quickstart, ensure you have 'polygons.shp' and 'elevation.tif' files.")
print("Uncomment the examples in the code to execute them.")
print("For full quickstart, refer to the official documentation.")

view raw JSON →