weights_from_shapefile
Generate spatial weights from a shapefile using queen or rook contiguity; optional ID field for observation identifiers. Supports geospatial analysis on GIS MCP Server.
Instructions
Create a spatial weights (W) from a shapefile using contiguity.
- contiguity: 'queen' or 'rook' (default 'queen')
- id_field: optional attribute name to use as observation IDs
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| contiguity | No | queen | |
| id_field | No | ||
| shapefile_path | Yes |
Implementation Reference
- src/gis_mcp/pysal_functions.py:340-393 (handler)The core handler function implementing the 'weights_from_shapefile' MCP tool. It loads a shapefile using libpysal.weights (Queen, Rook, or W.from_shapefile), computes spatial weights matrix statistics, previews neighbors and weights for the first 5 IDs, and returns structured results including neighbor stats and islands.@gis_mcp.tool() def weights_from_shapefile(shapefile_path: str, contiguity: str = "queen", id_field: Optional[str] = None) -> Dict[str, Any]: """Create a spatial weights (W) from a shapefile using contiguity. - contiguity: 'queen' or 'rook' (default 'queen') - id_field: optional attribute name to use as observation IDs """ try: if not os.path.exists(shapefile_path): return {"status": "error", "message": f"Shapefile not found: {shapefile_path}"} contiguity_lower = (contiguity or "").lower() import libpysal if contiguity_lower == "queen": w = libpysal.weights.Queen.from_shapefile(shapefile_path, idVariable=id_field) elif contiguity_lower == "rook": w = libpysal.weights.Rook.from_shapefile(shapefile_path, idVariable=id_field) else: # Fallback to generic W loader if an unrecognized contiguity is provided w = libpysal.weights.W.from_shapefile(shapefile_path, idVariable=id_field) ids = w.id_order neighbor_counts = [w.cardinalities[i] for i in ids] islands = list(w.islands) if hasattr(w, "islands") else [] preview_ids = ids[:5] neighbors_preview = {i: w.neighbors.get(i, []) for i in preview_ids} weights_preview = {i: w.weights.get(i, []) for i in preview_ids} result = { "n": int(w.n), "id_count": int(len(ids)), "id_field": id_field, "contiguity": contiguity_lower if contiguity_lower in {"queen", "rook"} else "generic", "neighbors_stats": { "min": int(min(neighbor_counts)) if neighbor_counts else 0, "max": int(max(neighbor_counts)) if neighbor_counts else 0, "mean": float(np.mean(neighbor_counts)) if neighbor_counts else 0.0, }, "islands": islands, "neighbors_preview": neighbors_preview, "weights_preview": weights_preview, } return { "status": "success", "message": "Spatial weights constructed successfully", "result": result, } except Exception as e: logger.error(f"Error creating spatial weights from shapefile: {str(e)}") return {"status": "error", "message": f"Failed to create spatial weights: {str(e)}"}
- src/gis_mcp/pysal_functions.py:340-340 (registration)The @gis_mcp.tool() decorator registers the weights_from_shapefile function as an MCP tool in the FastMCP instance defined in mcp.py.@gis_mcp.tool()