import { OpenAssistantTool } from '@openassistant/utils'; import { SpatialGeometry } from '@geoda/core'; import { z } from 'zod'; import { GetValues, GetGeometries } from '../types'; export type SpatialJoinFunctionArgs = z.ZodObject<{ rightDatasetName: z.ZodString; leftDatasetName: z.ZodString; joinVariables: z.ZodOptional; }>>>; }>; export type SpatialJoinLlmResult = { success: boolean; firstTwoRows?: { [x: string]: (number | string)[]; }[]; datasetName?: string; result?: string; joinStats?: { totalCount: number; minCount: number; maxCount: number; averageCount: number; }; error?: string; }; export type SpatialJoinAdditionalData = { rightDatasetName: string; leftDatasetName: string; joinVariableNames?: string[]; joinOperators?: string[]; datasetName: string; [datasetName: string]: unknown; }; export type SpatialJoinFunctionContext = { getGeometries: GetGeometries; getValues?: GetValues; saveAsDataset?: (datasetName: string, data: Record) => void; }; /** * ## spatialJoin Tool * * This tool performs spatial join operations between two geometric datasets, combining attributes based on spatial relationships. * It's useful for overlaying different types of spatial data and aggregating values across spatial boundaries. * * ### Spatial Join Operations * * The tool supports various join operations for aggregating values: * - **sum**: Sum of values in overlapping geometries * - **mean**: Average of values in overlapping geometries * - **min**: Minimum value in overlapping geometries * - **max**: Maximum value in overlapping geometries * - **median**: Median value in overlapping geometries * - **count**: Count of overlapping geometries * - **unique**: Count of unique values in overlapping geometries * * ### Parameters * - `rightDatasetName`: Name of the dataset providing the geometries to join (source) * - `leftDatasetName`: Name of the dataset receiving the joined data (target) * - `joinVariables`: Array of variables to join with their aggregation operators (optional) * * **Example user prompts:** * - "Can you join the population data with county boundaries?" * - "Join crime incidents to police districts using sum aggregation" * - "Overlay school locations with census tracts and count schools per tract" * * ### Example * ```typescript * import { spatialJoin } from "@openassistant/geoda"; * import { convertToVercelAiTool } from "@openassistant/utils"; * * const joinTool = { * ...spatialJoin, * context: { * getGeometries: async (datasetName: string) => { * // Implementation to retrieve geometries from your data source * return geometries; * }, * getValues: async (datasetName: string, variableName: string) => { * // Implementation to retrieve values from your data source * return [100, 200, 150, 300, 250]; * }, * }, * }; * * const result = await generateText({ * model: openai('gpt-4.1', { apiKey: key }), * prompt: 'Can you join the population data with county boundaries?', * tools: { spatialJoin: convertToVercelAiTool(joinTool) }, * }); * ``` * * :::note * The left dataset and right dataset should be different. joinVariables should come from the right dataset. * ::: */ export declare const spatialJoin: OpenAssistantTool; export type SpatialJoinTool = typeof spatialJoin; export declare function runSpatialJoin({ rightDatasetName, leftDatasetName, joinVariables, getGeometries, getValues, }: { rightDatasetName: string; leftDatasetName: string; joinVariables?: { variableName: string; operator: string; }[]; getGeometries: GetGeometries; getValues: GetValues; }): Promise<{ llmResult: { success: boolean; firstTwoRows: { [x: string]: (string | number)[]; }[]; datasetName: string; result: string; joinStats: { totalCount: number; minCount: number; maxCount: number; averageCount: number; }; error?: undefined; }; additionalData: { [x: string]: string | number[][] | { variableName: string; operator: string; }[] | Record | { type: string; content: { type: string; features: any[]; }; } | { type: string; content: any[][]; } | undefined; joinResult: number[][]; joinValues: Record; rightDatasetName: string; leftDatasetName: string; joinVariables: { variableName: string; operator: string; }[] | undefined; datasetName: string; }; } | { llmResult: { success: boolean; error: string; firstTwoRows?: undefined; datasetName?: undefined; result?: undefined; joinStats?: undefined; }; additionalData?: undefined; }>; /** * Get basic statistics of the result * @param result - the result of the spatial join * @returns - the basic statistics of the result */ export declare function getBasicStatistics(result: number[][]): { totalCount: number; minCount: number; maxCount: number; averageCount: number; }; export declare function appendJoinValuesToGeometries(geometries: SpatialGeometry, joinValues: Record): { type: string; content: { type: string; features: any[]; }; } | { type: string; content: any[][]; };