Skip to main content
Glama

raster_algebra

Perform addition or subtraction on raster band data, automatically aligning mismatched rasters, and save the output as a new geospatial file for efficient geospatial analysis.

Instructions

Perform algebraic operations (addition or subtraction) on two raster bands, handling alignment issues automatically. Parameters: - raster1: Path to the first raster (.tif). - raster2: Path to the second raster (.tif). - band_index: Index of the band to process (1-based index). - operation: Either "add" or "subtract" to specify the calculation. - destination: Path to save the result as a new raster. The function aligns rasters if needed, applies the selected operation, and saves the result.

Input Schema

TableJSON Schema
NameRequiredDescriptionDefault
band_indexYes
destinationYes
operationYes
raster1Yes
raster2Yes

Implementation Reference

  • Core implementation of the 'raster_algebra' tool. Decorated with @gis_mcp.tool(), it performs addition or subtraction between bands of two input rasters (aligning them if necessary using reprojection), and writes the result to a specified output file. Includes input schema via type annotations and detailed docstring describing parameters and behavior.
    @gis_mcp.tool() def raster_algebra( raster1: str, raster2: str, band_index: int, operation: str, # User selects "add" or "subtract" destination: str ) -> Dict[str, Any]: """ Perform algebraic operations (addition or subtraction) on two raster bands, handling alignment issues automatically. Parameters: - raster1: Path to the first raster (.tif). - raster2: Path to the second raster (.tif). - band_index: Index of the band to process (1-based index). - operation: Either "add" or "subtract" to specify the calculation. - destination: Path to save the result as a new raster. The function aligns rasters if needed, applies the selected operation, and saves the result. """ try: import rasterio import numpy as np from rasterio.warp import reproject, calculate_default_transform, Resampling # Expand file paths r1 = os.path.expanduser(raster1.replace("`", "")) r2 = os.path.expanduser(raster2.replace("`", "")) dst = os.path.expanduser(destination.replace("`", "")) # Open the raster files with rasterio.open(r1) as src1, rasterio.open(r2) as src2: # Ensure alignment of rasters if src1.crs != src2.crs or src1.transform != src2.transform or src1.shape != src2.shape: transform, width, height = calculate_default_transform( src2.crs, src1.crs, src2.width, src2.height, *src2.bounds ) aligned_data = np.zeros((height, width), dtype="float32") reproject( source=src2.read(band_index), destination=aligned_data, src_transform=src2.transform, src_crs=src2.crs, dst_transform=transform, dst_crs=src1.crs, resampling=Resampling.bilinear ) band2 = aligned_data else: band2 = src2.read(band_index).astype("float32") band1 = src1.read(band_index).astype("float32") # Perform the selected operation if operation.lower() == "add": result = band1 + band2 elif operation.lower() == "subtract": result = band1 - band2 else: raise ValueError("Invalid operation. Use 'add' or 'subtract'.") # Prepare output raster metadata profile = src1.profile.copy() profile.update(dtype="float32", count=1) # Ensure the output directory exists os.makedirs(os.path.dirname(dst) or ".", exist_ok=True) # Save the result to a new raster file with rasterio.open(dst, "w", **profile) as dstfile: dstfile.write(result, 1) return { "status": "success", "destination": dst, "message": f"Raster operation '{operation}' completed and saved." } except Exception as e: raise ValueError(f"Failed to perform raster operation: {e}")
  • 'raster_algebra' is listed among the exported tools in the module, likely part of __all__ or a tools registry list for module-level registration.
    "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"

Other Tools

Related Tools

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