-
Notifications
You must be signed in to change notification settings - Fork 119
/
query.ts
204 lines (193 loc) · 5.54 KB
/
query.ts
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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
/* Copyright (c) 2017-2018 Environmental Systems Research Institute, Inc.
* Apache-2.0 */
import {
request,
cleanUrl,
appendCustomParams
} from "@esri/arcgis-rest-request";
import {
ISpatialReference,
IFeatureSet,
IFeature,
Units,
IExtent,
IStatisticDefinition
} from "@esri/arcgis-rest-types";
import { IGetLayerOptions, ISharedQueryOptions } from "./helpers";
/**
* Request options to fetch a feature by id.
*/
export interface IGetFeatureOptions extends IGetLayerOptions {
/**
* Unique identifier of the feature.
*/
id: number;
}
/**
* feature query request options. See [REST Documentation](https://developers.arcgis.com/rest/services-reference/query-feature-service-layer-.htm) for more information.
*/
export interface IQueryFeaturesOptions extends ISharedQueryOptions {
objectIds?: number[];
relationParam?: string;
// NOTE: either time=1199145600000 or time=1199145600000, 1230768000000
time?: number | number[];
distance?: number;
units?: Units;
/**
* Attribute fields to include in the response. Defaults to "*"
*/
outFields?: "*" | string[];
returnGeometry?: boolean;
maxAllowableOffset?: number;
geometryPrecision?: number;
// NOTE: either WKID or ISpatialReference
inSR?: string | ISpatialReference;
outSR?: string | ISpatialReference;
gdbVersion?: string;
returnDistinctValues?: boolean;
returnIdsOnly?: boolean;
returnCountOnly?: boolean;
returnExtentOnly?: boolean;
orderByFields?: string;
groupByFieldsForStatistics?: string;
outStatistics?: IStatisticDefinition[];
returnZ?: boolean;
returnM?: boolean;
multipatchOption?: "xyFootprint";
resultOffset?: number;
resultRecordCount?: number;
// TODO: IQuantizationParameters?
quantizationParameters?: any;
returnCentroid?: boolean;
resultType?: "none" | "standard" | "tile";
// to do: convert from Date() to epoch time internally
historicMoment?: number;
returnTrueCurves?: false;
sqlFormat?: "none" | "standard" | "native";
returnExceededLimitFeatures?: boolean;
/**
* Response format. Defaults to "json"
* NOTE: for "pbf" you must also supply `rawResponse: true`
* and parse the response yourself using `response.arrayBuffer()`
*/
f?: "json" | "geojson" | "pbf";
/**
* someday...
*
* If 'true' the query will be preceded by a metadata check to gather info about coded value domains and result values will be decoded. If a fieldset is provided it will be used to decode values and no internal metadata request will be issued.
*/
// decodeValues?: boolean | IField[];
}
export interface IQueryFeaturesResponse extends IFeatureSet {
exceededTransferLimit?: boolean;
}
export interface IQueryResponse {
count?: number;
extent?: IExtent;
objectIdFieldName?: string;
objectIds?: number[];
}
/**
* ```js
* import { getFeature } from '@esri/arcgis-rest-feature-layer';
* //
* const url = "https://services.arcgis.com/V6ZHFr6zdgNZuVG0/arcgis/rest/services/Landscape_Trees/FeatureServer/0";
* //
* getFeature({
* url,
* id: 42
* }).then(feature => {
* console.log(feature.attributes.FID); // 42
* });
* ```
* Get a feature by id.
*
* @param requestOptions - Options for the request
* @returns A Promise that will resolve with the feature or the [response](https://developer.mozilla.org/en-US/docs/Web/API/Response) itself if `rawResponse: true` was passed in.
*/
export function getFeature(
requestOptions: IGetFeatureOptions
): Promise<IFeature> {
const url = `${cleanUrl(requestOptions.url)}/${requestOptions.id}`;
// default to a GET request
const options: IGetFeatureOptions = {
...{ httpMethod: "GET" },
...requestOptions
};
return request(url, options).then((response: any) => {
if (options.rawResponse) {
return response;
}
return response.feature;
});
}
/**
* ```js
* import { queryFeatures } from '@esri/arcgis-rest-feature-layer';
* //
* queryFeatures({
* url: "http://sampleserver6.arcgisonline.com/arcgis/rest/services/Census/MapServer/3",
* where: "STATE_NAME = 'Alaska'"
* })
* .then(result)
* ```
* Query a feature service. See [REST Documentation](https://developers.arcgis.com/rest/services-reference/query-feature-service-layer-.htm) for more information.
*
* @param requestOptions - Options for the request
* @returns A Promise that will resolve with the query response.
*/
export function queryFeatures(
requestOptions: IQueryFeaturesOptions
): Promise<IQueryFeaturesResponse | IQueryResponse> {
const queryOptions = appendCustomParams<IQueryFeaturesOptions>(
requestOptions,
[
"where",
"objectIds",
"relationParam",
"time",
"distance",
"units",
"outFields",
"geometry",
"geometryType",
"spatialRel",
"returnGeometry",
"maxAllowableOffset",
"geometryPrecision",
"inSR",
"outSR",
"gdbVersion",
"returnDistinctValues",
"returnIdsOnly",
"returnCountOnly",
"returnExtentOnly",
"orderByFields",
"groupByFieldsForStatistics",
"outStatistics",
"returnZ",
"returnM",
"multipatchOption",
"resultOffset",
"resultRecordCount",
"quantizationParameters",
"returnCentroid",
"resultType",
"historicMoment",
"returnTrueCurves",
"sqlFormat",
"returnExceededLimitFeatures",
"f"
],
{
httpMethod: "GET",
params: {
// set default query parameters
where: "1=1",
outFields: "*",
...requestOptions.params
}
}
);
return request(`${cleanUrl(requestOptions.url)}/query`, queryOptions);
}