Skip to main content
Glama

moran_local

Analyze spatial autocorrelation in geospatial data using Local Moran's I to identify clusters and outliers in spatial patterns. Input a shapefile, dependent variable, and optional parameters to assess spatial dependencies.

Instructions

Local Moran's I.

Input Schema

TableJSON Schema
NameRequiredDescriptionDefault
dependent_varNoLAND_USE
distance_thresholdNo
shapefile_pathYes
target_crsNoEPSG:4326

Implementation Reference

  • The core handler function for the 'moran_local' tool. Computes Local Moran's I statistic using esda.Moran_Local on a GeoDataFrame's dependent variable with row-standardized distance-based spatial weights.
    @gis_mcp.tool() def moran_local(shapefile_path: str, dependent_var: str = "LAND_USE", target_crs: str = "EPSG:4326", distance_threshold: float = 100000) -> Dict[str, Any]: """Local Moran's I.""" gdf, y, w, (threshold, unit), err = pysal_load_data(shapefile_path, dependent_var, target_crs, distance_threshold) if err: return {"status": "error", "message": err} import esda stat = esda.Moran_Local(y, w) preview = gdf[['geometry', dependent_var]].head(5).copy() preview['geometry'] = preview['geometry'].apply(lambda g: g.wkt) # Return local statistics array summary return { "status": "success", "message": f"Local Moran's I completed successfully (threshold: {threshold} {unit})", "result": { "Is": stat.Is.tolist(), "p_values": stat.p_sim.tolist(), "z_scores": stat.z_sim.tolist(), "data_preview": preview.to_dict(orient="records") } }
  • Supporting helper function that loads the shapefile into a GeoDataFrame, extracts the dependent variable as a numpy array, builds row-standardized distance-band spatial weights, handles isolated observations, and adjusts threshold for geographic CRS.
    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
  • MCP resource that lists available ESDA/PySAL operations, explicitly including 'moran_local' to inform clients of the tool's availability.
    @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" ] }

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