Skip to main content
Glama

moran_local

Calculate Local Moran's I spatial autocorrelation to identify clusters and outliers in geospatial data, supporting spatial pattern analysis for geographic datasets.

Instructions

Local Moran's I.

Input Schema

TableJSON Schema
NameRequiredDescriptionDefault
shapefile_pathYes
dependent_varNoLAND_USE
target_crsNoEPSG:4326
distance_thresholdNo

Output Schema

TableJSON Schema
NameRequiredDescriptionDefault

No arguments

Implementation Reference

  • The primary handler function for the 'moran_local' MCP tool. It loads geospatial data, constructs spatial weights, handles isolated observations, computes Local Moran's I using esda.Moran_Local, and returns local I values, p-values, z-scores, and a data preview.
    @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}
    
        # Handle islands - if all points are islands, fall back to KNN weights for connectivity
        import libpysal
        if w.islands:
            if len(w.islands) == len(gdf):
                # All points are islands - fall back to KNN weights
                try:
                    # Use k=4 for a 5x5 grid to ensure connectivity
                    w = libpysal.weights.KNN.from_dataframe(gdf, k=4)
                    w.transform = 'r'
                except Exception as e:
                    return {"status": "error", "message": f"All units are islands and KNN fallback failed: {str(e)}"}
            else:
                # Some islands - filter them out
                keep_idx = [i for i in range(len(gdf)) if i not in set(w.islands)]
                if len(keep_idx) == 0:
                    return {"status": "error", "message": "All units are islands (no neighbors). Try increasing distance_threshold."}
                # Filter data
                gdf_filtered = gdf.iloc[keep_idx].reset_index(drop=True)
                y_filtered = y[keep_idx]
                # Rebuild weights without islands using the same threshold
                w_filtered = libpysal.weights.DistanceBand.from_dataframe(
                    gdf_filtered, 
                    threshold=threshold,  # Use the effective threshold already calculated in pysal_load_data
                    binary=False
                )
                w_filtered.transform = 'r'
                gdf, y, w = gdf_filtered, y_filtered, w_filtered
    
        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() if hasattr(stat.Is, 'tolist') else list(stat.Is),
                "p_values": stat.p_sim.tolist() if hasattr(stat.p_sim, 'tolist') else list(stat.p_sim),
                "z_scores": stat.z_sim.tolist() if hasattr(stat.z_sim, 'tolist') else list(stat.z_sim),
                "data_preview": preview.to_dict(orient="records")
            }
        }
  • Shared helper function used by 'moran_local' and other PySAL tools to load GeoDataFrame, validate inputs, reproject, create row-standardized distance band spatial weights, and handle basic island cases.
    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
Behavior1/5

Does the description disclose side effects, auth requirements, rate limits, or destructive behavior?

No annotations are provided, so the description carries the full burden of behavioral disclosure. 'Local Moran's I' implies a statistical computation but reveals nothing about required permissions, data formats, computational intensity, output structure, or error handling. For a tool with 4 parameters and spatial analysis complexity, this is a critical gap.

Agents need to know what a tool does to the world before calling it. Descriptions should go beyond structured annotations to explain consequences.

Conciseness3/5

Is the description appropriately sized, front-loaded, and free of redundancy?

The description is extremely concise ('Local Moran's I.'), which could be efficient if it were informative. However, it's under-specified rather than appropriately concise—it lacks essential details. The single sentence structure is clear but fails to convey necessary information.

Shorter descriptions cost fewer tokens and are easier for agents to parse. Every sentence should earn its place.

Completeness2/5

Given the tool's complexity, does the description cover enough for an agent to succeed on first attempt?

Given the complexity of spatial autocorrelation analysis, 4 parameters with 0% schema coverage, no annotations, and an output schema (which helps but isn't described), the description is incomplete. It doesn't explain the tool's purpose, usage, or parameters, leaving significant gaps for an AI agent to understand and invoke it correctly.

Complex tools with many parameters or behaviors need more documentation. Simple tools need less. This dimension scales expectations accordingly.

Parameters1/5

Does the description clarify parameter syntax, constraints, interactions, or defaults beyond what the schema provides?

Schema description coverage is 0%, meaning none of the 4 parameters are documented in the schema. The description adds no information about parameters like 'shapefile_path', 'dependent_var', 'target_crs', or 'distance_threshold'. It doesn't explain what these inputs mean, their formats, or how they affect the computation.

Input schemas describe structure but not intent. Descriptions should explain non-obvious parameter relationships and valid value ranges.

Purpose2/5

Does the description clearly state what the tool does and how it differs from similar tools?

The description 'Local Moran's I' is a tautology that restates the tool name without explaining what it does. It doesn't specify the action (e.g., 'calculate' or 'compute') or the resource involved. While 'Moran's I' is a known spatial autocorrelation statistic, the description fails to articulate the tool's function beyond the name.

Agents choose between tools based on descriptions. A clear purpose with a specific verb and resource helps agents select the right tool.

Usage Guidelines1/5

Does the description explain when to use this tool, when not to, or what alternatives exist?

The description provides no guidance on when to use this tool versus alternatives. With sibling tools like 'morans_i' (global Moran's I), 'getis_ord_g_local', 'dynamic_lisa', and 'join_counts_local' available, there's no indication of how this tool differs or when it's appropriate. No context or exclusions are mentioned.

Agents often have multiple tools that could apply. Explicit usage guidance like "use X instead of Y when Z" prevents misuse.

Install Server

Other 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