Skip to main content
Glama

write_raster

Convert a numpy array into a raster file by copying metadata from a reference raster. Specify array, reference raster, output path, and optional data type to generate a new raster file efficiently.

Instructions

Write a numpy array to a raster file using metadata from a reference raster. Args: array: 2D or 3D list (or numpy array) of raster values. reference_raster: Path to a raster whose metadata will be copied. output_path: Path to save the new raster. dtype: Optional data type (e.g., 'float32', 'uint8'). Returns: Dictionary with status and message.

Input Schema

TableJSON Schema
NameRequiredDescriptionDefault
arrayYes
dtypeNo
output_pathYes
reference_rasterYes

Implementation Reference

  • The primary handler function for the 'write_raster' tool. It takes an array, a reference raster for metadata, an output path, and optional dtype, then writes the array as a GeoTIFF using rasterio. Registered via the @gis_mcp.tool() decorator.
    @gis_mcp.tool() def write_raster(array: list, reference_raster: str, output_path: str, dtype: str = None) -> Dict[str, Any]: """ Write a numpy array to a raster file using metadata from a reference raster. Args: array: 2D or 3D list (or numpy array) of raster values. reference_raster: Path to a raster whose metadata will be copied. output_path: Path to save the new raster. dtype: Optional data type (e.g., 'float32', 'uint8'). Returns: Dictionary with status and message. """ try: import rasterio import numpy as np arr = np.array(array) with rasterio.open(reference_raster) as src: profile = src.profile.copy() if dtype: profile.update(dtype=dtype) if arr.ndim == 2: profile.update(count=1) elif arr.ndim == 3: profile.update(count=arr.shape[0]) else: raise ValueError("Array must be 2D or 3D.") output_path_resolved = resolve_path(output_path, relative_to_storage=True) output_path_resolved.parent.mkdir(parents=True, exist_ok=True) with rasterio.open(str(output_path_resolved), "w", **profile) as dst: dst.write(arr) return { "status": "success", "message": f"Raster written to '{output_path_resolved}' successfully.", "output_path": str(output_path_resolved) } except Exception as e: logger.error(f"Error in write_raster: {str(e)}") return {"status": "error", "message": str(e)}
  • MCP resource endpoint listing available rasterio operations, including 'write_raster', for 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" ] }

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