Skip to main content
Glama

raster_histogram

Analyze pixel value distributions in raster data bands to understand data patterns and quality for geospatial analysis.

Instructions

Compute histogram of pixel values for each band.

Parameters:

  • source: path to input raster.

  • bins: number of histogram bins.

Input Schema

TableJSON Schema
NameRequiredDescriptionDefault
sourceYes
binsNo

Implementation Reference

  • The core handler function for the 'raster_histogram' MCP tool. It reads the input raster, computes histograms for each band using numpy.histogram, and returns the results per band including bin edges.
    @gis_mcp.tool()
    def raster_histogram(
        source: str,
        bins: int = 256
    ) -> Dict[str, Any]:
        """
        Compute histogram of pixel values for each band.
    
        Parameters:
        - source: path to input raster.
        - bins:   number of histogram bins.
        """
        try:
            import rasterio
            import numpy as np
            import os
    
            src_path = os.path.expanduser(source.replace("`", ""))
            histograms = {}
    
            with rasterio.open(src_path) as src:
                for i in range(1, src.count + 1):
                    band = src.read(i, masked=True)
                    hist, bin_edges = np.histogram(band.compressed(), bins=bins)
                    histograms[f"Band {i}"] = {
                        "histogram": hist.tolist(),
                        "bin_edges": bin_edges.tolist()
                    }
    
            return {
                "status": "success",
                "histograms": histograms,
                "message": f"Histogram computed for all bands."
            }
    
        except Exception as e:
            raise ValueError(f"Failed to compute histogram: {e}")
  • MCP resource that lists all available rasterio operations, including 'raster_histogram', serving as a tool discovery/registration point.
    @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