Skip to main content
Glama

distance_band_weights

Generate spatial weights for point data based on distance thresholds using GIS MCP Server. Define neighbors within a specified range and apply binary or inverse distance weights for geospatial analysis.

Instructions

Create a distance-based spatial weights (W) object from point data. - data_path: path to point shapefile or GeoPackage - threshold: distance threshold for neighbors (in CRS units, e.g., meters) - binary: True for binary weights, False for inverse distance weights - id_field: optional attribute name to use as observation IDs

Input Schema

TableJSON Schema
NameRequiredDescriptionDefault
binaryNo
data_pathYes
id_fieldNo
thresholdYes

Implementation Reference

  • The handler function for the 'distance_band_weights' tool. It loads point data from a shapefile or GeoPackage, extracts coordinates, constructs a libpysal.weights.DistanceBand spatial weights matrix based on the given threshold (binary or inverse distance), computes summary statistics, and returns a preview of neighbors and weights.
    def distance_band_weights( data_path: str, threshold: float, binary: bool = True, id_field: Optional[str] = None ) -> Dict[str, Any]: """ Create a distance-based spatial weights (W) object from point data. - data_path: path to point shapefile or GeoPackage - threshold: distance threshold for neighbors (in CRS units, e.g., meters) - binary: True for binary weights, False for inverse distance weights - id_field: optional attribute name to use as observation IDs """ try: if not os.path.exists(data_path): return {"status": "error", "message": f"Data file not found: {data_path}"} gdf = gpd.read_file(data_path) if gdf.empty: return {"status": "error", "message": "Input file contains no features"} # Extract coordinates coords = [(geom.x, geom.y) for geom in gdf.geometry] # Create DistanceBand weights from libpysal.weights import weights if id_field and id_field in gdf.columns: ids = gdf[id_field].tolist() w = weights.DistanceBand(coords, threshold=threshold, binary=binary, ids=ids) else: w = weights.DistanceBand(coords, threshold=threshold, binary=binary) ids = w.id_order neighbor_counts = [w.cardinalities[i] for i in ids] islands = list(w.islands) if hasattr(w, "islands") else [] # Previews 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": len(ids), "threshold": threshold, "binary": binary, "id_field": id_field, "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": "DistanceBand spatial weights constructed successfully", "result": result, } except Exception as e: logger.error(f"Error creating DistanceBand weights: {str(e)}") return {"status": "error", "message": f"Failed to create DistanceBand weights: {str(e)}"}

Other Tools

Related 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