Skip to main content
Glama

extract_band

Extract a specific band from a multi-band raster and save it as a single-band GeoTIFF for focused geospatial analysis.

Instructions

Extract a specific band from a multi-band raster and save it as a single-band GeoTIFF.

Parameters:

  • source: path or URL of the input raster.

  • band_index: index of the band to extract (1-based).

  • destination: path to save the extracted band raster.

Input Schema

TableJSON Schema
NameRequiredDescriptionDefault
sourceYes
band_indexYes
destinationYes

Implementation Reference

  • The core handler function for the 'extract_band' tool. It reads a specific band from the source raster using rasterio, copies the profile, updates the band count to 1, and writes the extracted band to the destination path.
    def extract_band(
        source: str,
        band_index: int,
        destination: str
    ) -> Dict[str, Any]:
        """
        Extract a specific band from a multi-band raster and save it as a single-band GeoTIFF.
    
        Parameters:
        - source:      path or URL of the input raster.
        - band_index:  index of the band to extract (1-based).
        - destination: path to save the extracted band raster.
        """
        try:
            import rasterio
    
            src_path = os.path.expanduser(source.replace("`", ""))
            dst_path = os.path.expanduser(destination.replace("`", ""))
    
            with rasterio.open(src_path) as src:
                if band_index < 1 or band_index > src.count:
                    raise ValueError(f"Band index {band_index} is out of range. This raster has {src.count} bands.")
    
                band = src.read(band_index)
                profile = src.profile.copy()
                profile.update({
                    "count": 1
                })
    
            os.makedirs(os.path.dirname(dst_path) or ".", exist_ok=True)
    
            with rasterio.open(dst_path, "w", **profile) as dst:
                dst.write(band, 1)
    
            return {
                "status": "success",
                "destination": str(dst_path),
                "message": f"Band {band_index} extracted and saved to '{dst_path}'."
            }
    
        except Exception as e:
            raise ValueError(f"Failed to extract band: {e}")
  • The 'extract_band' tool is registered/listed in the rasterio operations resource, which provides a list of available rasterio tools.
    @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"
            ]
        }

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