Skip to main content
Glama

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
NameRequiredDescriptionDefault
output_pathNo
raster_pathYes
sizeNo
statisticYes

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)}
  • 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" ] }

Other Tools

Related Tools

Latest Blog Posts

MCP directory API

We provide all the information about MCP servers via our MCP API.

curl -X GET 'https://glama.ai/api/mcp/v1/servers/mahdin75/gis-mcp'

If you have feedback or need assistance with the MCP directory API, please join our Discord server