reclassify_raster
Reclassifies raster values using a mapping dictionary, transforming specific input values into desired output values. Saves the modified raster to a specified path for enhanced geospatial analysis.
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
| Name | Required | Description | Default |
|---|---|---|---|
| output_path | Yes | ||
| raster_path | Yes | ||
| reclass_map | Yes |
Implementation Reference
- src/gis_mcp/rasterio_functions.py:86-117 (handler)The core handler function for the 'reclassify_raster' tool. It uses rasterio to read the input raster, applies the reclassification mapping to pixel values using numpy, resolves the output path, and writes the reclassified raster.@gis_mcp.tool() 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)}
- src/gis_mcp/rasterio_functions.py:11-35 (registration)Resource function listing available rasterio operations, including 'reclassify_raster', for 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" ] }
- Type hints and docstring defining the input schema (raster_path: str, reclass_map: dict, output_path: str) and output (Dict[str, Any]).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. """