join_counts
Calculate global binary join counts for spatial data analysis. Input shapefile to measure spatial relationships within a specified distance threshold, supporting accurate geospatial insights.
Instructions
Global Binary Join Counts.
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| dependent_var | No | LAND_USE | |
| distance_threshold | No | ||
| shapefile_path | Yes | ||
| target_crs | No | EPSG:4326 |
Implementation Reference
- src/gis_mcp/pysal_functions.py:262-287 (handler)The primary handler function for the 'join_counts' MCP tool. It loads a shapefile, prepares spatial weights using pysal_load_data, computes global binary join counts using esda.Join_Counts, and returns statistics including z-score and p-value.@gis_mcp.tool() def join_counts(shapefile_path: str, dependent_var: str = "LAND_USE", target_crs: str = "EPSG:4326", distance_threshold: float = 100000) -> Dict[str, Any]: """Global Binary Join Counts.""" gdf, y, w, (threshold, unit), err = pysal_load_data(shapefile_path, dependent_var, target_crs, distance_threshold) if err: return {"status": "error", "message": err} # Join counts requires binary/categorical data - user must ensure y is binary (0/1 or True/False) import esda stat = esda.Join_Counts(y, w) preview = gdf[['geometry', dependent_var]].head(5).copy() preview['geometry'] = preview['geometry'].apply(lambda g: g.wkt) return { "status": "success", "message": f"Join Counts completed successfully (threshold: {threshold} {unit})", "result": { "join_counts": stat.jc, "expected": stat.expected, "variance": stat.variance, "z_score": stat.z_score, "p_value": stat.p_value, "data_preview": preview.to_dict(orient="records") } }
- Shared helper function used by 'join_counts' and other PySAL/ESDA tools to load GeoDataFrame, reproject, extract dependent variable, create row-standardized distance band weights, and handle isolated observations.def pysal_load_data(shapefile_path: str, dependent_var: str, target_crs: str, distance_threshold: float): """Common loader and weight creation for esda statistics.""" if not os.path.exists(shapefile_path): return None, None, None, None, f"Shapefile not found: {shapefile_path}" gdf = gpd.read_file(shapefile_path) if dependent_var not in gdf.columns: return None, None, None, None, f"Dependent variable '{dependent_var}' not found in shapefile columns" gdf = gdf.to_crs(target_crs) effective_threshold = distance_threshold unit = "meters" if target_crs.upper() == "EPSG:4326": effective_threshold = distance_threshold / 111000 unit = "degrees" y = gdf[dependent_var].values.astype(np.float64) import libpysal w = libpysal.weights.DistanceBand.from_dataframe(gdf, threshold=effective_threshold, binary=False) w.transform = 'r' for island in w.islands: w.weights[island] = [0] * len(w.weights[island]) w.cardinalities[island] = 0 return gdf, y, w, (effective_threshold, unit), None
- src/gis_mcp/pysal_functions.py:13-28 (registration)MCP resource that lists available ESDA operations including 'join_counts', serving as a discovery mechanism for the tool.@gis_mcp.resource("gis://operations/esda") def get_spatial_operations() -> Dict[str, List[str]]: """List available spatial analysis operations. This is for esda library. They are using pysal library.""" return { "operations": [ "getis_ord_g", "morans_i", "gearys_c", "gamma_statistic", "moran_local", "getis_ord_g_local", "join_counts", "join_counts_local", "adbscan" ] }