Skip to main content
Glama

raster_band_statistics

Calculate min, max, mean, and standard deviation statistics for each band of a raster image to analyze geospatial data patterns.

Instructions

Calculate min, max, mean, and std for each band of a raster.

Parameters:

  • source: path to input raster (local or URL).

Input Schema

TableJSON Schema
NameRequiredDescriptionDefault
sourceYes

Implementation Reference

  • The handler function that implements the core logic of the raster_band_statistics tool. It reads the input raster, computes min, max, mean, and standard deviation for each band using rasterio and numpy, handling masked arrays for NoData values, and returns a dictionary with the statistics.
    def raster_band_statistics(
        source: str
    ) -> Dict[str, Any]:
        """
        Calculate min, max, mean, and std for each band of a raster.
    
        Parameters:
        - source: path to input raster (local or URL).
        """
        try:
            import numpy as np
            import rasterio
    
            src_path = os.path.expanduser(source.replace("`", ""))
            stats = {}
    
            with rasterio.open(src_path) as src:
                for i in range(1, src.count + 1):
                    band = src.read(i, masked=True)  # masked array handles NoData
                    stats[f"Band {i}"] = {
                        "min": float(band.min()),
                        "max": float(band.max()),
                        "mean": float(band.mean()),
                        "std": float(band.std())
                    }
    
            return {
                "status": "success",
                "statistics": stats,
                "message": f"Band-wise statistics computed successfully."
            }
    
        except Exception as e:
            raise ValueError(f"Failed to compute statistics: {e}")
  • The raster_band_statistics tool is registered/listed as one of the available rasterio operations in this resource endpoint.
    @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"
            ]
        }

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