Skip to main content
Glama

get_raster_crs

Retrieve the Coordinate Reference System (CRS) of a raster dataset to understand its geospatial reference for accurate GIS analysis and transformations.

Instructions

Retrieve the Coordinate Reference System (CRS) of a raster dataset.

Opens the raster (local path or HTTPS URL), reads its DatasetReader.crs attribute as a PROJ.4-style dict, and also returns the WKT representation.

Input Schema

TableJSON Schema
NameRequiredDescriptionDefault
path_or_urlYes

Implementation Reference

  • The core handler function for the 'get_raster_crs' tool. It opens a raster file (local or URL), extracts its CRS using rasterio, and returns it in PROJ.4 dictionary and WKT formats.
    def get_raster_crs(path_or_url: str) -> Dict[str, Any]:
        """
        Retrieve the Coordinate Reference System (CRS) of a raster dataset.
        
        Opens the raster (local path or HTTPS URL), reads its DatasetReader.crs
        attribute as a PROJ.4-style dict, and also returns the WKT representation.
        """
        try:
            import numpy as np
            import rasterio
    
            # Strip backticks if the client wrapped the input in them
            cleaned = path_or_url.replace("`", "")
    
            # Open remote or local dataset
            if cleaned.lower().startswith("https://"):
                src = rasterio.open(cleaned)
            else:
                local_path = os.path.expanduser(cleaned)
                if not os.path.isfile(local_path):
                    raise FileNotFoundError(f"Raster file not found at '{local_path}'.")
                src = rasterio.open(local_path)
    
            # Access the CRS object on the opened dataset
            crs_obj = src.crs
            src.close()
    
            if crs_obj is None:
                raise ValueError("No CRS defined for this dataset.")
    
            # Convert CRS to PROJ.4‐style dict and WKT string
            proj4_dict = crs_obj.to_dict()    # e.g., {'init': 'epsg:32618'}
            wkt_str    = crs_obj.to_wkt()     # full WKT representation
    
            return {
                "status":      "success",
                "proj4":       proj4_dict,
                "wkt":         wkt_str,
                "message":     "CRS retrieved successfully"
            }
    
        except Exception as e:
            # Log and re-raise as ValueError for MCP error propagation
            logger.error(f"Error retrieving CRS for '{path_or_url}': {e}")
            raise ValueError(f"Failed to retrieve CRS: {e}")
  • Helper resource that lists 'get_raster_crs' among available rasterio operations, aiding in 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"
            ]
        }
  • src/gis_mcp/mcp.py:1-6 (registration)
    Definition of the FastMCP instance 'gis_mcp' used to register all tools via decorators.
    # MCP imports using the new SDK patterns
    from fastmcp import FastMCP
    
    
    gis_mcp = FastMCP("GIS MCP")
  • Import of rasterio_functions module in main.py, which triggers registration of the get_raster_crs tool via its @gis_mcp.tool() decorator.
    from . import (
        geopandas_functions,
        shapely_functions,
        rasterio_functions,
        pyproj_functions,
        pysal_functions,
    )

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