focal_statistics
Calculate moving window statistics like mean, min, max, or std on raster data. Define window size and optionally save results for geospatial analysis using GIS MCP Server.
Instructions
Compute focal (moving window) statistics on a raster.
Args:
raster_path: Path to the input raster.
statistic: Statistic to compute ('mean', 'min', 'max', 'std').
size: Window size (odd integer).
output_path: Optional path to save the result.
Returns:
Dictionary with status, message, and output path if saved.
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| output_path | No | ||
| raster_path | Yes | ||
| size | No | ||
| statistic | Yes |
Implementation Reference
- The primary handler function for the 'focal_statistics' tool, decorated with @gis_mcp.tool(). It computes moving window statistics (mean, min, max, or std) on a raster band using scipy.ndimage.generic_filter and optionally saves the output.@gis_mcp.tool() def focal_statistics(raster_path: str, statistic: str, size: int = 3, output_path: str = None) -> Dict[str, Any]: """ Compute focal (moving window) statistics on a raster. Args: raster_path: Path to the input raster. statistic: Statistic to compute ('mean', 'min', 'max', 'std'). size: Window size (odd integer). output_path: Optional path to save the result. Returns: Dictionary with status, message, and output path if saved. """ try: import rasterio import numpy as np from scipy.ndimage import generic_filter with rasterio.open(raster_path) as src: data = src.read(1) profile = src.profile.copy() func = None if statistic == "mean": func = np.mean elif statistic == "min": func = np.min elif statistic == "max": func = np.max elif statistic == "std": func = np.std else: raise ValueError(f"Unsupported statistic: {statistic}") filtered = generic_filter(data, func, size=size, mode='nearest') if output_path: output_path_resolved = resolve_path(output_path, relative_to_storage=True) output_path_resolved.parent.mkdir(parents=True, exist_ok=True) with rasterio.open(str(output_path_resolved), "w", **profile) as dst: dst.write(filtered, 1) output_path = str(output_path_resolved) return { "status": "success", "message": f"Focal {statistic} computed successfully.", "output_path": output_path } except Exception as e: logger.error(f"Error in focal_statistics: {str(e)}") return {"status": "error", "message": str(e)}
- src/gis_mcp/rasterio_functions.py:11-35 (registration)MCP resource that lists 'focal_statistics' among the available rasterio operations for discovery.@gis_mcp.resource("gis://operation/rasterio") def get_rasterio_operations() -> Dict[str, List[str]]: """List available rasterio operations.""" return { "operations": [ "metadata_raster", "get_raster_crs", "clip_raster_with_shapefile", "resample_raster", "reproject_raster", "weighted_band_sum", "concat_bands", "raster_algebra", "compute_ndvi", "raster_histogram", "tile_raster", "raster_band_statistics", "extract_band", "zonal_statistics", "reclassify_raster", "focal_statistics", "hillshade", "write_raster" ] }