Skip to content

Documentation v1.0.0


Documentation / @openassistant/geoda / spatialWeights

Variable: spatialWeights ​

const spatialWeights: OpenAssistantTool<SpatialWeightsFunctionArgs, SpatialWeightsLlmResult, SpatialWeightsAdditionalData, SpatialWeightsFunctionContext>

Defined in: src/weights/tool.ts:110

spatialWeights Tool ​

This tool creates spatial weights matrices for spatial analysis. Spatial weights define the spatial relationships between observations and are essential for spatial autocorrelation analysis.

Spatial Weights Methods ​

The tool supports various spatial weights methods:

  • queen: Queen contiguity (shares edge or vertex)
  • rook: Rook contiguity (shares edge only)
  • knn: K-nearest neighbors
  • threshold: Distance-based weights

Parameters ​

  • datasetName: Name of the dataset containing the geometries
  • type: Spatial weights method to use (see above)
  • k: Number of neighbors for knn method (optional)
  • orderOfContiguity: Order of contiguity for queen/rook (optional)
  • includeLowerOrder: Include lower order contiguity (optional)
  • precisionThreshold: Precision threshold for geometry operations (optional)
  • distanceThreshold: Distance threshold for threshold method (optional)
  • isMile: Use miles instead of kilometers for distance (optional)
  • useCentroids: Use centroids instead of full geometries (optional)
  • mapBounds: Map bounds for visualization (optional)

Example user prompts:

  • "Create spatial weights using queen contiguity"
  • "Generate k-nearest neighbors weights with k=5"
  • "Make distance-based weights with 10km threshold"

Example ​

typescript
import { spatialWeights } from "@openassistant/geoda";
import { convertToVercelAiTool } from "@openassistant/utils";

const weightsTool = {
  ...spatialWeights,
  context: {
    getGeometries: async (datasetName: string) => {
      // Implementation to retrieve geometries from your data source
      return geometries;
    },
  },
};

const result = await generateText({
  model: openai('gpt-4.1', { apiKey: key }),
  prompt: 'Create spatial weights using queen contiguity',
  tools: { spatialWeights: convertToVercelAiTool(weightsTool) },
});

INFO

The spatialWeights tool should always be used with other spatial analysis tools like globalMoran, lisa, etc.

Released under the MIT License.