metadata_raster
Retrieve metadata from raster datasets using a local file path or HTTPS URL. Ideal for GIS analysis and spatial data management within the GIS MCP Server.
Instructions
Open a raster dataset in read-only mode and return metadata.
This tool supports two modes based on the provided string:
1. A local filesystem path (e.g., "D:\Data\my_raster.tif").
2. An HTTPS URL (e.g., "https://example.com/my_raster.tif").
The input must be a single string that is either a valid file path
on the local machine or a valid HTTPS URL pointing to a raster.
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| path_or_url | Yes |
Implementation Reference
- The handler function for the 'metadata_raster' tool. It opens a raster dataset from a local path or HTTPS URL using rasterio and returns comprehensive metadata including dimensions, CRS, bounds, driver, nodata values, band dtypes, and affine transform.@gis_mcp.tool() def metadata_raster(path_or_url: str) -> Dict[str, Any]: """ Open a raster dataset in read-only mode and return metadata. This tool supports two modes based on the provided string: 1. A local filesystem path (e.g., "D:\\Data\\my_raster.tif"). 2. An HTTPS URL (e.g., "https://example.com/my_raster.tif"). The input must be a single string that is either a valid file path on the local machine or a valid HTTPS URL pointing to a raster. """ try: # Import numpy first to ensure NumPy's C-API is initialized import numpy as np import rasterio # Remove any backticks (`) if the client wrapped the path_or_url in them cleaned = path_or_url.replace("`", "") # Determine if the string is an HTTPS URL or a local file path if cleaned.lower().startswith("https://"): # For HTTPS URLs, let Rasterio/GDAL handle remote access directly dataset = rasterio.open(cleaned) else: # Treat as local filesystem path local_path = os.path.expanduser(cleaned) # Verify that the file exists on disk if not os.path.isfile(local_path): raise FileNotFoundError(f"Raster file not found at '{local_path}'.") # Open the local file in read-only mode dataset = rasterio.open(local_path) # Build a mapping from band index to its data type (dtype) band_dtypes = {i: dtype for i, dtype in zip(dataset.indexes, dataset.dtypes)} # Collect core metadata fields in simple Python types meta: Dict[str, Any] = { "name": dataset.name, # Full URI or filesystem path "mode": dataset.mode, # Mode should be 'r' for read "driver": dataset.driver, # GDAL driver, e.g. "GTiff" "width": dataset.width, # Number of columns "height": dataset.height, # Number of rows "count": dataset.count, # Number of bands "bounds": dataset.bounds, # Show bounding box "band_dtypes": band_dtypes, # { band_index: dtype_string } "no_data": dataset.nodatavals, # Number of NoData values in each band "crs": dataset.crs.to_string() if dataset.crs else None, # CRS as EPSG string or None "transform": list(dataset.transform), # Affine transform coefficients (6 floats) } # Return a success status along with metadata return { "status": "success", "metadata": meta, "message": f"Raster dataset opened successfully from '{cleaned}'." } except Exception as e: # Log the error for debugging purposes, then raise ValueError so MCP can relay it logger.error(f"Error opening raster '{path_or_url}': {str(e)}") raise ValueError(f"Failed to open raster '{path_or_url}': {str(e)}")
- src/gis_mcp/rasterio_functions.py:11-35 (registration)The resource function that lists all available rasterio operations, including 'metadata_raster', effectively registering it as part of the GIS MCP 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" ] }