Skip to main content
Glama

reclassify_raster

Reclassify raster values by mapping old values to new values using a dictionary, then save the reclassified raster to a specified output path.

Instructions

Reclassify raster values using a mapping dictionary. Args: raster_path: Path to the input raster. reclass_map: Dictionary mapping old values to new values (e.g., {1: 10, 2: 20}). output_path: Path to save the reclassified raster. Returns: Dictionary with status and message.

Input Schema

TableJSON Schema
NameRequiredDescriptionDefault
raster_pathYes
reclass_mapYes
output_pathYes

Implementation Reference

  • The core handler function for the 'reclassify_raster' tool. It reads the input raster, applies the reclassification mapping using numpy, and writes the output raster using rasterio.
    def reclassify_raster(raster_path: str, reclass_map: dict, output_path: str) -> Dict[str, Any]:
        """
        Reclassify raster values using a mapping dictionary.
        Args:
            raster_path: Path to the input raster.
            reclass_map: Dictionary mapping old values to new values (e.g., {1: 10, 2: 20}).
            output_path: Path to save the reclassified raster.
        Returns:
            Dictionary with status and message.
        """
        try:
            import rasterio
            import numpy as np
            with rasterio.open(raster_path) as src:
                data = src.read(1)
                profile = src.profile.copy()
                reclass_data = np.copy(data)
                for old, new in reclass_map.items():
                    reclass_data[data == old] = new
            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(reclass_data, 1)
            return {
                "status": "success",
                "message": f"Raster reclassified and saved to '{output_path_resolved}'.", 
                "output_path": str(output_path_resolved)
            }
        except Exception as e:
            logger.error(f"Error in reclassify_raster: {str(e)}")
            return {"status": "error", "message": str(e)}
  • Imports the rasterio_functions module (line 69), which triggers the @gis_mcp.tool() decorators to register the reclassify_raster tool with the MCP server.
    from . import (
        geopandas_functions,
        shapely_functions,
        rasterio_functions,
        pyproj_functions,
        pysal_functions,
    )
  • Resource function listing 'reclassify_raster' among available rasterio operations, aiding tool 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"
            ]
        }
  • Wildcard import of rasterio_functions.py in __init__.py, ensuring tools are registered when the package is imported.
    from .rasterio_functions import *
  • Function signature and docstring define the input schema (raster_path: str, reclass_map: dict, output_path: str) and output format.
    def reclassify_raster(raster_path: str, reclass_map: dict, output_path: str) -> Dict[str, Any]:
        """
        Reclassify raster values using a mapping dictionary.
        Args:
            raster_path: Path to the input raster.
            reclass_map: Dictionary mapping old values to new values (e.g., {1: 10, 2: 20}).
            output_path: Path to save the reclassified raster.
        Returns:
            Dictionary with status and message.
        """

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