Skip to main content
Glama

sjoin_nearest_gpd

Perform spatial joins between geospatial datasets by matching features based on nearest proximity, with configurable distance thresholds and join types.

Instructions

Nearest neighbor spatial join using geopandas.sjoin_nearest. Args: left_path: Path to the left geospatial file. right_path: Path to the right geospatial file. how: Type of join ('left', 'right'). max_distance: Optional maximum search distance. output_path: Optional path to save the result. Returns: Dictionary with status, message, and output info.

Input Schema

TableJSON Schema
NameRequiredDescriptionDefault
left_pathYes
right_pathYes
howNoleft
max_distanceNo
output_pathNo

Output Schema

TableJSON Schema
NameRequiredDescriptionDefault

No arguments

Implementation Reference

  • Core handler function that loads two geospatial files, performs nearest neighbor spatial join with geopandas.sjoin_nearest, handles CRS alignment, optional distance limit and output saving, returns preview and metadata.
    @gis_mcp.tool()
    def sjoin_nearest_gpd(left_path: str, right_path: str, how: str = "left", max_distance: float = None, output_path: str = None) -> Dict[str, Any]:
        """
        Nearest neighbor spatial join using geopandas.sjoin_nearest.
        Args:
            left_path: Path to the left geospatial file.
            right_path: Path to the right geospatial file.
            how: Type of join ('left', 'right').
            max_distance: Optional maximum search distance.
            output_path: Optional path to save the result.
        Returns:
            Dictionary with status, message, and output info.
        """
        try:
            left = gpd.read_file(left_path)
            right = gpd.read_file(right_path)
            if left.crs != right.crs:
                right = right.to_crs(left.crs)
            kwargs = {"how": how}
            if max_distance is not None:
                kwargs["max_distance"] = max_distance
            result = gpd.sjoin_nearest(left, right, **kwargs)
            if output_path:
                output_path_resolved = resolve_path(output_path, relative_to_storage=True)
                output_path_resolved.parent.mkdir(parents=True, exist_ok=True)
                result.to_file(str(output_path_resolved))
                output_path = str(output_path_resolved)
            # Convert geometry to WKT for serialization
            preview_df = result.head(5).copy()
            if 'geometry' in preview_df.columns:
                preview_df['geometry'] = preview_df['geometry'].apply(lambda g: g.wkt if g is not None else None)
            preview = preview_df.to_dict(orient="records")
            return {
                "status": "success",
                "message": f"Nearest spatial join ({how}) completed successfully.",
                "num_features": len(result),
                "crs": str(result.crs),
                "columns": list(result.columns),
                "preview": preview,
                "output_path": output_path,
            }
        except Exception as e:
            logger.error(f"Error in sjoin_nearest_gpd: {str(e)}")
            return {"status": "error", "message": str(e)}
  • Imports the geopandas_functions module (among others), which executes the @gis_mcp.tool decorators to register the sjoin_nearest_gpd tool with the MCP server.
    from . import (
        geopandas_functions,
        shapely_functions,
        rasterio_functions,
        pyproj_functions,
        pysal_functions,
    )
  • MCP resource that lists 'sjoin_nearest_gpd' as one of the available GeoPandas join operations for tool discovery.
    @gis_mcp.resource("gis://geopandas/joins")
    def get_geopandas_joins() -> Dict[str, List[str]]:
        """List available GeoPandas join operations."""
        return {
            "operations": [
                "append_gpd",
                "merge_gpd",
                "sjoin_gpd",
                "sjoin_nearest_gpd",
                "point_in_polygon"
            ]
        }
  • src/gis_mcp/mcp.py:5-5 (registration)
    Creates the FastMCP instance 'gis_mcp' used for tool and resource registration via decorators.
    gis_mcp = FastMCP("GIS MCP")
Behavior2/5

Does the description disclose side effects, auth requirements, rate limits, or destructive behavior?

With no annotations provided, the description carries full burden but provides minimal behavioral context. It mentions the tool returns a dictionary with status/message/output info, but doesn't disclose important behaviors like whether files are modified in-place, what happens with invalid geometries, memory implications for large datasets, or error conditions.

Agents need to know what a tool does to the world before calling it. Descriptions should go beyond structured annotations to explain consequences.

Conciseness4/5

Is the description appropriately sized, front-loaded, and free of redundancy?

The description is efficiently structured with a clear purpose statement followed by Args/Returns sections. Every sentence serves a purpose, though the 'Returns' section could be more specific about what 'output info' contains given there's an output schema.

Shorter descriptions cost fewer tokens and are easier for agents to parse. Every sentence should earn its place.

Completeness3/5

Given the tool's complexity, does the description cover enough for an agent to succeed on first attempt?

For a spatial join tool with 5 parameters, 0% schema coverage, no annotations, but with an output schema, the description is minimally adequate. It covers basic purpose and parameters but lacks crucial context about spatial join behavior, performance characteristics, and when to use this versus other spatial operations available in the sibling tool list.

Complex tools with many parameters or behaviors need more documentation. Simple tools need less. This dimension scales expectations accordingly.

Parameters4/5

Does the description clarify parameter syntax, constraints, interactions, or defaults beyond what the schema provides?

With 0% schema description coverage, the description compensates well by listing all 5 parameters with brief explanations. It clarifies that max_distance and output_path are optional, distinguishes left_path/right_path roles, and mentions the 'how' parameter accepts 'left' or 'right' join types. However, it doesn't explain parameter formats (e.g., file path requirements) or units for max_distance.

Input schemas describe structure but not intent. Descriptions should explain non-obvious parameter relationships and valid value ranges.

Purpose4/5

Does the description clearly state what the tool does and how it differs from similar tools?

The description clearly states the tool performs a 'nearest neighbor spatial join using geopandas.sjoin_nearest', which is a specific verb (spatial join) with resource (geospatial files). It distinguishes from sibling tools like 'sjoin_gpd' by specifying 'nearest' functionality, though it doesn't explicitly contrast them.

Agents choose between tools based on descriptions. A clear purpose with a specific verb and resource helps agents select the right tool.

Usage Guidelines2/5

Does the description explain when to use this tool, when not to, or what alternatives exist?

The description provides no guidance on when to use this tool versus alternatives like 'sjoin_gpd' or other spatial operations. It mentions the 'how' parameter options but gives no context about when to choose left vs right joins or when nearest neighbor joins are appropriate.

Agents often have multiple tools that could apply. Explicit usage guidance like "use X instead of Y when Z" prevents misuse.

Install Server

Other Tools

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