Skip to main content

Variable: centroid

const centroid: ExtendedTool<CentroidFunctionArgs, CentroidLlmResult, CentroidAdditionalData, SpatialToolContext>

Defined in: packages/tools/geoda/src/spatial_ops/centroid.ts:71

centroid Tool

This tool is used to calculate the centroids (geometric centers) of geometries.

Centroid Calculation

The tool supports:

  • Calculating centroids from GeoJSON input
  • Calculating centroids from geometries in a dataset
  • Returns centroids as points that can be used for mapping

When user prompts e.g. can you find the center points of these counties?

  1. The LLM will execute the callback function of centroidFunctionDefinition, and calculate the centroids using the geometries retrieved from getGeometries function.
  2. The result will include the centroid points and a new dataset name for mapping.
  3. The LLM will respond with the centroid calculation results and the new dataset name.

For example

User: can you find the center points of these counties?
LLM: I've calculated the centroids of the counties. The centroid points are saved in dataset "centroid_123"...

Code example

import { centroid, CentroidTool } from '@openassistant/geoda';
import { convertToVercelAiTool } from '@openassistant/utils';
import { generateText } from 'ai';

const centroidTool: CentroidTool = {
...centroid,
context: {
getGeometries: (datasetName) => {
return SAMPLE_DATASETS[datasetName].map((item) => item.geometry);
},
},
};

generateText({
model: openai('gpt-4o-mini', { apiKey: key }),
prompt: 'Can you find the center points of these counties?',
tools: {centroid: convertToVercelAiTool(centroidTool)},
});