point_in_polygon
Determine if points lie within specified polygons by performing a spatial join using the 'within' predicate. Input point and polygon geospatial files, optionally save the result, and receive a detailed output dictionary.
Instructions
Check if points are inside polygons using spatial join (predicate='within').
Args:
points_path: Path to the point geospatial file.
polygons_path: Path to the polygon geospatial file.
output_path: Optional path to save the result.
Returns:
Dictionary with status, message, and output info.
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| output_path | No | ||
| points_path | Yes | ||
| polygons_path | Yes |
Implementation Reference
- The main handler function for the 'point_in_polygon' tool. It uses geopandas.sjoin with predicate='within' to check which points are inside polygons, optionally saves the result to a file, and returns metadata including a preview.@gis_mcp.tool() def point_in_polygon(points_path: str, polygons_path: str, output_path: str = None) -> Dict[str, Any]: """ Check if points are inside polygons using spatial join (predicate='within'). Args: points_path: Path to the point geospatial file. polygons_path: Path to the polygon geospatial file. output_path: Optional path to save the result. Returns: Dictionary with status, message, and output info. """ try: points = gpd.read_file(points_path) polygons = gpd.read_file(polygons_path) if points.crs != polygons.crs: polygons = polygons.to_crs(points.crs) result = gpd.sjoin(points, polygons, how="left", predicate="within") 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": "Point-in-polygon test 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 point_in_polygon: {str(e)}") return {"status": "error", "message": str(e)}
- src/gis_mcp/geopandas_functions.py:28-39 (registration)Resource function listing 'point_in_polygon' as one of the available GeoPandas join operations under the 'gis://geopandas/joins' namespace.@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" ] }