compute_ndvi
Calculate vegetation health by computing NDVI from red and near-infrared bands in raster data, then save results as a GeoTIFF file.
Instructions
Compute NDVI (Normalized Difference Vegetation Index) and save to GeoTIFF.
Parameters:
source: input raster path.
red_band_index: index of red band (1-based).
nir_band_index: index of near-infrared band (1-based).
destination: output NDVI raster path.
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| source | Yes | ||
| red_band_index | Yes | ||
| nir_band_index | Yes | ||
| destination | Yes |
Implementation Reference
- The core handler function for the compute_ndvi MCP tool. It reads specified red and NIR bands from the input raster, computes NDVI using the formula (NIR - Red) / (NIR + Red), handles division by zero, copies metadata, and writes the result to a new GeoTIFF file.@gis_mcp.tool() def compute_ndvi( source: str, red_band_index: int, nir_band_index: int, destination: str ) -> Dict[str, Any]: """ Compute NDVI (Normalized Difference Vegetation Index) and save to GeoTIFF. Parameters: - source: input raster path. - red_band_index: index of red band (1-based). - nir_band_index: index of near-infrared band (1-based). - destination: output NDVI raster path. """ try: import rasterio import numpy as np src_path = os.path.expanduser(source.replace("`", "")) dst_path = os.path.expanduser(destination.replace("`", "")) with rasterio.open(src_path) as src: red = src.read(red_band_index).astype("float32") nir = src.read(nir_band_index).astype("float32") ndvi = (nir - red) / (nir + red + 1e-6) # avoid division by zero profile = src.profile.copy() profile.update(dtype="float32", count=1) os.makedirs(os.path.dirname(dst_path) or ".", exist_ok=True) with rasterio.open(dst_path, "w", **profile) as dst: dst.write(ndvi, 1) return { "status": "success", "destination": str(dst_path), "message": f"NDVI calculated and saved to '{dst_path}'." } except Exception as e: raise ValueError(f"Failed to compute NDVI: {e}")
- src/gis_mcp/rasterio_functions.py:11-35 (registration)MCP resource that lists 'compute_ndvi' among available rasterio operations, effectively advertising the tool's availability.@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" ] }
- src/gis_mcp/main.py:66-72 (registration)Import statements in the main entry point that trigger the execution of decorators in rasterio_functions.py, registering the compute_ndvi tool with the FastMCP instance.from . import ( geopandas_functions, shapely_functions, rasterio_functions, pyproj_functions, pysal_functions, )
- src/gis_mcp/mcp.py:1-6 (helper)Definition of the gis_mcp FastMCP instance used by the @gis_mcp.tool() decorator to register tools including compute_ndvi.# MCP imports using the new SDK patterns from fastmcp import FastMCP gis_mcp = FastMCP("GIS MCP")