Skip to main content
Glama
tharlestsa

OpenLandMap MCP Server

by tharlestsa

build_python_snippet

Generate Python code to access and process OpenLandMap raster data for operations like opening, plotting, clipping, or analyzing geospatial datasets.

Instructions

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

Supports multiple operations: open, info, plot, clip_bbox, stats, export_csv.

Args: collection_id: Collection identifier. item_id: Item identifier. asset_key: Asset key within the item. operation: Code operation to generate: 'open' — open the raster with rasterio 'info' — print raster metadata 'plot' — plot with matplotlib 'clip_bbox' — clip to a bounding box 'stats' — compute zonal statistics 'export_csv' — export values to CSV

Returns: Python code snippet as a string.

Example: build_python_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 'build_python_snippet' function, decorated with @mcp.tool(), implements the logic to generate Python code snippets for various raster operations.
    async def build_python_snippet(
        collection_id: str,
        item_id: str,
        asset_key: str,
        operation: str = "open",
    ) -> str:
        """Generate a ready-to-use Python code snippet for accessing a raster asset.
    
        Supports multiple operations: open, info, plot, clip_bbox, stats, export_csv.
    
        Args:
            collection_id: Collection identifier.
            item_id: Item identifier.
            asset_key: Asset key within the item.
            operation: Code operation to generate:
                'open' — open the raster with rasterio
                'info' — print raster metadata
                'plot' — plot with matplotlib
                'clip_bbox' — clip to a bounding box
                'stats' — compute zonal statistics
                'export_csv' — export values to CSV
    
        Returns:
            Python code snippet as a string.
    
        Example:
            build_python_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'import rasterio\n\n'
                f'url = "{url}"\n\n'
                f'with rasterio.open(url) as src:\n'
                f'    data = src.read(1)\n'
                f'    print(f"Shape: {{data.shape}}")\n'
                f'    print(f"CRS: {{src.crs}}")\n'
                f'    print(f"Bounds: {{src.bounds}}")\n'
                f'    print(f"Resolution: {{src.res}}")\n'
                f'    print(f"NoData: {{src.nodata}}")'
            ),
            "info": (
                f'import rasterio\n\n'
                f'url = "{url}"\n\n'
                f'with rasterio.open(url) as src:\n'
                f'    print(f"Driver: {{src.driver}}")\n'
                f'    print(f"Width x Height: {{src.width}} x {{src.height}}")\n'
                f'    print(f"Bands: {{src.count}}")\n'
                f'    print(f"CRS: {{src.crs}}")\n'
                f'    print(f"Transform: {{src.transform}}")\n'
                f'    print(f"Bounds: {{src.bounds}}")\n'
                f'    print(f"Resolution: {{src.res}}")\n'
                f'    print(f"Data type: {{src.dtypes}}")\n'
                f'    print(f"NoData: {{src.nodata}}")\n'
                f'    tags = src.tags()\n'
                f'    for k, v in tags.items():\n'
                f'        print(f"  {{k}}: {{v}}")'
            ),
            "plot": (
                f'import rasterio\n'
                f'import matplotlib.pyplot as plt\n'
                f'import numpy as np\n\n'
                f'url = "{url}"\n\n'
                f'with rasterio.open(url) as src:\n'
                f'    data = src.read(1)\n'
                f'    nodata = src.nodata\n'
                f'    if nodata is not None:\n'
                f'        data = np.where(data == nodata, np.nan, data)\n\n'
                f'    fig, ax = plt.subplots(1, 1, figsize=(12, 8))\n'
                f'    im = ax.imshow(data, cmap="viridis")\n'
                f'    plt.colorbar(im, ax=ax, label="{collection_id}")\n'
                f'    ax.set_title("{collection_id} — {item_id}")\n'
                f'    plt.tight_layout()\n'
                f'    plt.show()'
            ),
            "clip_bbox": (
                f'import rasterio\n'
                f'from rasterio.windows import from_bounds\n\n'
                f'url = "{url}"\n\n'
                f'# Define your bounding box [west, south, east, north]\n'
                f'bbox = [-54.0, -18.0, -45.0, -12.0]  # Example: part of Cerrado\n\n'
                f'with rasterio.open(url) as src:\n'
                f'    window = from_bounds(*bbox, transform=src.transform)\n'
                f'    data = src.read(1, window=window)\n'
                f'    print(f"Clipped shape: {{data.shape}}")\n'
  • Registration of 'build_python_snippet' as an MCP tool using the @mcp.tool() decorator.
    @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