sjoin_nearest_gpd
Perform nearest neighbor spatial join between two geospatial datasets with optional max distance. Specify join type and save results directly for efficient geospatial analysis.
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
| Name | Required | Description | Default |
|---|---|---|---|
| how | No | left | |
| left_path | Yes | ||
| max_distance | No | ||
| output_path | No | ||
| right_path | Yes |
Implementation Reference
- The primary handler function decorated with @gis_mcp.tool() that performs nearest neighbor spatial join using geopandas.sjoin_nearest on two geospatial files.@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) preview = result.head(5).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)}
- src/gis_mcp/geopandas_functions.py:36-39 (registration)The tool is listed in the geopandas joins resource for discovery."sjoin_nearest_gpd", "point_in_polygon" ] }