Skip to content

Documentation v1.0.0


Documentation / @openassistant/geoda / length

Variable: length

const length: OpenAssistantTool<LengthFunctionArgs, LengthLlmResult, LengthAdditionalData, SpatialToolContext>

Defined in: src/spatial_ops/length.ts:79

length Tool

This tool calculates the length of geometries in a GeoJSON dataset. It supports both direct GeoJSON input and dataset names, and can calculate lengths in either kilometers or miles.

Length Calculation

The tool calculates lengths for various geometry types:

  • LineStrings: Calculates the total length of line features
  • MultiLineStrings: Calculates the total length of multiline features
  • Polygons: Calculates the perimeter length of polygon boundaries
  • FeatureCollections: Calculates lengths for all line features in the collection

Parameters

  • datasetName: Name of the dataset with geometries to calculate length for (optional)
  • geojson: GeoJSON string of geometries to calculate length for (optional)
  • distanceUnit: Unit for length calculation - 'KM' for kilometers or 'Mile' for miles

Example user prompts:

  • "Calculate the length of these roads in kilometers"
  • "What is the total length of the river network in miles?"
  • "Measure the length of these boundaries"

Example

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

const lengthTool = {
  ...length,
  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: 'Calculate the length of these roads in kilometers',
  tools: { length: convertToVercelAiTool(lengthTool) },
});

Released under the MIT License.