-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathTilequeryTool.ts
More file actions
60 lines (47 loc) · 1.79 KB
/
Copy pathTilequeryTool.ts
File metadata and controls
60 lines (47 loc) · 1.79 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
import { MapboxApiBasedTool } from '../MapboxApiBasedTool.js';
import { TilequerySchema, TilequeryInput } from './TilequeryTool.schema.js';
export class TilequeryTool extends MapboxApiBasedTool<typeof TilequerySchema> {
name = 'tilequery_tool';
description =
'Query vector and raster data from Mapbox tilesets at geographic coordinates';
constructor() {
super({ inputSchema: TilequerySchema });
}
protected async execute(
input: TilequeryInput,
accessToken?: string
): Promise<any> {
const { tilesetId, longitude, latitude, ...queryParams } = input;
const url = new URL(
`${MapboxApiBasedTool.MAPBOX_API_ENDPOINT}v4/${tilesetId}/tilequery/${longitude},${latitude}.json`
);
if (queryParams.radius !== undefined) {
url.searchParams.set('radius', queryParams.radius.toString());
}
if (queryParams.limit !== undefined) {
url.searchParams.set('limit', queryParams.limit.toString());
}
if (queryParams.dedupe !== undefined) {
url.searchParams.set('dedupe', queryParams.dedupe.toString());
}
if (queryParams.geometry) {
url.searchParams.set('geometry', queryParams.geometry);
}
if (queryParams.layers && queryParams.layers.length > 0) {
url.searchParams.set('layers', queryParams.layers.join(','));
}
if (queryParams.bands && queryParams.bands.length > 0) {
url.searchParams.set('bands', queryParams.bands.join(','));
}
url.searchParams.set('access_token', accessToken || '');
const response = await fetch(url.toString());
if (!response.ok) {
const errorText = await response.text();
throw new Error(
`Tilequery request failed: ${response.status} ${response.statusText}. ${errorText}`
);
}
const data = await response.json();
return data;
}
}