get_raster_crs
Retrieve the Coordinate Reference System (CRS) of a raster dataset, returning both PROJ.4-style dict and WKT formats. Use with local paths or HTTPS URLs to identify spatial reference details.
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
| Name | Required | Description | Default |
|---|---|---|---|
| path_or_url | Yes |
Implementation Reference
- The handler function decorated with @gis_mcp.tool() that opens a raster file (local or URL), retrieves its CRS using rasterio, and returns it in PROJ4 dict and WKT formats.@gis_mcp.tool() 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}")
- src/gis_mcp/rasterio_functions.py:11-35 (registration)Registers the get_raster_crs tool as part of the available rasterio operations list via the @gis_mcp.resource decorator for 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" ] }