Skip to main content
Glama
tharlestsa

OpenLandMap MCP Server

by tharlestsa

build_r_snippet

Generate R code snippets to access and process raster datasets from OpenLandMap's environmental data catalog for geospatial analysis.

Instructions

Generate a ready-to-use R code snippet for accessing a raster asset.

Uses terra, sf, and rstac packages.

Args: collection_id: Collection identifier. item_id: Item identifier. asset_key: Asset key within the item. operation: Code operation: 'open' — open with terra::rast() 'info' — print metadata 'plot' — plot the raster 'clip_bbox' — crop to bounding box 'stats' — compute statistics 'export_csv' — export to CSV

Returns: R code snippet as a string.

Example: build_r_snippet("organic.carbon_usda.6a1c", "organic.carbon_usda.6a1c_20180101_20181231", "organic.carbon_usda.6a1c_m_1km_b30cm_s", "plot")

Input Schema

TableJSON Schema
NameRequiredDescriptionDefault
collection_idYes
item_idYes
asset_keyYes
operationNoopen

Implementation Reference

  • The function `build_r_snippet` implements the tool logic to generate R code snippets based on collection, item, asset keys, and an operation type. It is annotated with `@mcp.tool()` which registers it as an MCP tool.
    async def build_r_snippet(
        collection_id: str,
        item_id: str,
        asset_key: str,
        operation: str = "open",
    ) -> str:
        """Generate a ready-to-use R code snippet for accessing a raster asset.
    
        Uses terra, sf, and rstac packages.
    
        Args:
            collection_id: Collection identifier.
            item_id: Item identifier.
            asset_key: Asset key within the item.
            operation: Code operation:
                'open' — open with terra::rast()
                'info' — print metadata
                'plot' — plot the raster
                'clip_bbox' — crop to bounding box
                'stats' — compute statistics
                'export_csv' — export to CSV
    
        Returns:
            R code snippet as a string.
    
        Example:
            build_r_snippet("organic.carbon_usda.6a1c",
                            "organic.carbon_usda.6a1c_20180101_20181231",
                            "organic.carbon_usda.6a1c_m_1km_b30cm_s",
                            "plot")
        """
        data = await client.get_item_raw(collection_id, item_id)
        assets = data.get("assets", {})
    
        if asset_key not in assets:
            return f"# Error: asset '{asset_key}' not found. Available: {list(assets.keys())}"
    
        url = assets[asset_key].get("href", "")
    
        snippets = {
            "open": (
                f'library(terra)\n\n'
                f'url <- "{url}"\n'
                f'r <- rast(url)\n'
                f'print(r)\n'
                f'cat("CRS:", crs(r, describe=TRUE)$code, "\\n")\n'
                f'cat("Resolution:", res(r), "\\n")\n'
                f'cat("Extent:", as.vector(ext(r)), "\\n")'
            ),
            "info": (
                f'library(terra)\n\n'
                f'url <- "{url}"\n'
                f'r <- rast(url)\n'
                f'cat("Dimensions:", nrow(r), "x", ncol(r), "\\n")\n'
                f'cat("Layers:", nlyr(r), "\\n")\n'
                f'cat("CRS:", crs(r, describe=TRUE)$code, "\\n")\n'
                f'cat("Resolution:", res(r), "\\n")\n'
                f'cat("Extent:", as.vector(ext(r)), "\\n")\n'
                f'cat("Min:", minmax(r)[1], "\\n")\n'
                f'cat("Max:", minmax(r)[2], "\\n")\n'
                f'cat("Data type:", datatype(r), "\\n")'
            ),
            "plot": (
                f'library(terra)\n\n'
                f'url <- "{url}"\n'
                f'r <- rast(url)\n'
                f'plot(r, main="{collection_id} — {item_id}",\n'
                f'     col=hcl.colors(50, "viridis"))'
            ),
            "clip_bbox": (
  • The `@mcp.tool()` decorator on line 410 registers the `build_r_snippet` function as an MCP tool. The module `openlandmap_mcp.tools.analysis` is imported in `src/openlandmap_mcp/server.py` to ensure registration.
    @mcp.tool()

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/tharlestsa/openlandmap_mcp'

If you have feedback or need assistance with the MCP directory API, please join our Discord server