reproject_raster
Transform a raster dataset to a new coordinate reference system using specified resampling. Input source, target CRS, and destination path for the reprojected raster.
Instructions
Reproject a raster dataset to a new CRS and save the result.
Parameters:
- source: local path or HTTPS URL of the source raster.
- target_crs: target CRS string (e.g., "EPSG:4326").
- destination: local filesystem path for the reprojected raster.
- resampling: resampling method: "nearest", "bilinear", "cubic", etc.
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| destination | Yes | ||
| resampling | No | nearest | |
| source | Yes | ||
| target_crs | Yes |
Implementation Reference
- The core handler function decorated with @gis_mcp.tool() that implements the reproject_raster tool logic using rasterio.warp.reproject to reproject the source raster to the target CRS and save it to the destination path.@gis_mcp.tool() def reproject_raster( source: str, target_crs: str, destination: str, resampling: str = "nearest" ) -> Dict[str, Any]: """ Reproject a raster dataset to a new CRS and save the result. Parameters: - source: local path or HTTPS URL of the source raster. - target_crs: target CRS string (e.g., "EPSG:4326"). - destination: local filesystem path for the reprojected raster. - resampling: resampling method: "nearest", "bilinear", "cubic", etc. """ try: import numpy as np import rasterio from rasterio.warp import calculate_default_transform, reproject, Resampling # Strip backticks if present src_clean = source.replace("`", "") dst_clean = destination.replace("`", "") # Open source (remote or local) if src_clean.lower().startswith("https://"): src = rasterio.open(src_clean) else: src_path = os.path.expanduser(src_clean) if not os.path.isfile(src_path): raise FileNotFoundError(f"Source raster not found at '{src_path}'.") src = rasterio.open(src_path) # Calculate transform and dimensions for the target CRS transform, width, height = calculate_default_transform( src.crs, target_crs, src.width, src.height, *src.bounds ) # Update profile for output profile = src.profile.copy() profile.update({ "crs": target_crs, "transform": transform, "width": width, "height": height }) src.close() # Map resampling method string to Resampling enum resampling_enum = getattr(Resampling, resampling.lower(), Resampling.nearest) # Ensure destination directory exists dst_path = os.path.expanduser(dst_clean) os.makedirs(os.path.dirname(dst_path) or ".", exist_ok=True) # Perform reprojection and write output with rasterio.open(dst_path, "w", **profile) as dst: for i in range(1, profile["count"] + 1): reproject( source=rasterio.open(src_clean).read(i), destination=rasterio.band(dst, i), src_transform=profile["transform"], # placeholder, will be overwritten src_crs=src.crs, dst_transform=transform, dst_crs=target_crs, resampling=resampling_enum ) return { "status": "success", "destination": str(dst_path), "message": f"Raster reprojected to '{target_crs}' and saved to '{dst_path}'." } except Exception as e: logger.error(f"Error reprojecting raster '{source}' to '{target_crs}': {e}") raise ValueError(f"Failed to reproject raster: {e}")
- src/gis_mcp/rasterio_functions.py:11-35 (registration)MCP resource that lists all available rasterio operations, including 'reproject_raster', effectively serving as a tool registry or discovery endpoint.@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" ] }
- Docstring defining the input parameters and purpose of the reproject_raster tool, which serves as the schema for input validation in MCP.""" Reproject a raster dataset to a new CRS and save the result. Parameters: - source: local path or HTTPS URL of the source raster. - target_crs: target CRS string (e.g., "EPSG:4326"). - destination: local filesystem path for the reprojected raster. - resampling: resampling method: "nearest", "bilinear", "cubic", etc. """