Skip to main content
Glama

concat_bands

Combine multiple single-band raster files into a multi-band raster, automatically aligning mismatched coordinate systems, resolutions, and dimensions for geospatial analysis.

Instructions

Concatenate multiple single-band raster files into one multi-band raster, handling alignment issues automatically.

Parameters:

  • folder_path: Path to folder containing input raster files (e.g. GeoTIFFs).

  • destination: Path to output multi-band raster file.

Notes:

  • Files are read in sorted order by filename.

  • If rasters have mismatched CRS, resolution, or dimensions, they are aligned automatically.

Input Schema

TableJSON Schema
NameRequiredDescriptionDefault
folder_pathYes
destinationYes

Implementation Reference

  • The concat_bands tool handler that concatenates multiple single-band TIFF files from a folder into one multi-band raster file, automatically aligning mismatched rasters using reprojection.
    def concat_bands(
        folder_path: str,
        destination: str
    ) -> Dict[str, Any]:
        """
        Concatenate multiple single-band raster files into one multi-band raster, 
        handling alignment issues automatically.
    
        Parameters:
        - folder_path:   Path to folder containing input raster files (e.g. GeoTIFFs).
        - destination:   Path to output multi-band raster file.
    
        Notes:
        - Files are read in sorted order by filename.
        - If rasters have mismatched CRS, resolution, or dimensions, they are aligned automatically.
        """
        try:
            import rasterio
            import numpy as np
            from rasterio.warp import reproject, calculate_default_transform, Resampling
            from glob import glob
    
            folder_path = os.path.expanduser(folder_path.replace("`", ""))
            dst_path = os.path.expanduser(destination.replace("`", ""))
    
            # Collect single-band TIFF files in folder
            files = sorted(glob(os.path.join(folder_path, "*.tif")))
    
            if len(files) == 0:
                raise ValueError("No .tif files found in folder.")
    
            # Read properties of the first file for reference
            with rasterio.open(files[0]) as ref:
                meta = ref.meta.copy()
                height, width = ref.height, ref.width
                crs = ref.crs
                transform = ref.transform
                dtype = ref.dtypes[0]
    
            meta.update(count=len(files), dtype=dtype)
    
            os.makedirs(os.path.dirname(dst_path) or ".", exist_ok=True)
    
            with rasterio.open(dst_path, "w", **meta) as dst:
                for idx, fp in enumerate(files, start=1):
                    with rasterio.open(fp) as src:
                        band = src.read(1)
    
                        # Auto-align raster if size or CRS mismatch occurs
                        if src.height != height or src.width != width or src.crs != crs or src.transform != transform:
                            new_transform, new_width, new_height = calculate_default_transform(
                                src.crs, crs, src.width, src.height, *src.bounds
                            )
                            aligned_band = np.zeros((new_height, new_width), dtype=dtype)
                            reproject(
                                source=band,
                                destination=aligned_band,
                                src_transform=src.transform,
                                src_crs=src.crs,
                                dst_transform=new_transform,
                                dst_crs=crs,
                                resampling=Resampling.bilinear
                            )
                            band = aligned_band
    
                        dst.write(band, idx)
    
            return {
                "status": "success",
                "destination": str(dst_path),
                "message": f"{len(files)} single-band rasters concatenated into '{dst_path}'."
            }
    
        except Exception as e:
            raise ValueError(f"Failed to concatenate rasters: {e}")
  • Resource listing available rasterio operations, including 'concat_bands', serving as tool discovery/registration.
    @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/mcp.py:1-6 (registration)
    Creation of the FastMCP instance 'gis_mcp' to which tools are registered via decorators.
    # MCP imports using the new SDK patterns
    from fastmcp import FastMCP
    
    
    gis_mcp = FastMCP("GIS MCP")

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