-
Notifications
You must be signed in to change notification settings - Fork 799
/
Query.js
225 lines (193 loc) · 7.58 KB
/
Query.js
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
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
import { point, latLng } from 'leaflet';
import { Task } from './Task';
import {
warn,
responseToFeatureCollection,
isArcgisOnline,
extentToBounds,
_setGeometry
} from '../Util';
export var Query = Task.extend({
setters: {
offset: 'resultOffset',
limit: 'resultRecordCount',
fields: 'outFields',
precision: 'geometryPrecision',
featureIds: 'objectIds',
returnGeometry: 'returnGeometry',
returnM: 'returnM',
transform: 'datumTransformation',
token: 'token'
},
path: 'query',
params: {
returnGeometry: true,
where: '1=1',
outSR: 4326,
outFields: '*'
},
// Returns a feature if its shape is wholly contained within the search geometry. Valid for all shape type combinations.
within: function (geometry) {
this._setGeometryParams(geometry);
this.params.spatialRel = 'esriSpatialRelContains'; // to the REST api this reads geometry **contains** layer
return this;
},
// Returns a feature if any spatial relationship is found. Applies to all shape type combinations.
intersects: function (geometry) {
this._setGeometryParams(geometry);
this.params.spatialRel = 'esriSpatialRelIntersects';
return this;
},
// Returns a feature if its shape wholly contains the search geometry. Valid for all shape type combinations.
contains: function (geometry) {
this._setGeometryParams(geometry);
this.params.spatialRel = 'esriSpatialRelWithin'; // to the REST api this reads geometry **within** layer
return this;
},
// Returns a feature if the intersection of the interiors of the two shapes is not empty and has a lower dimension than the maximum dimension of the two shapes. Two lines that share an endpoint in common do not cross. Valid for Line/Line, Line/Area, Multi-point/Area, and Multi-point/Line shape type combinations.
crosses: function (geometry) {
this._setGeometryParams(geometry);
this.params.spatialRel = 'esriSpatialRelCrosses';
return this;
},
// Returns a feature if the two shapes share a common boundary. However, the intersection of the interiors of the two shapes must be empty. In the Point/Line case, the point may touch an endpoint only of the line. Applies to all combinations except Point/Point.
touches: function (geometry) {
this._setGeometryParams(geometry);
this.params.spatialRel = 'esriSpatialRelTouches';
return this;
},
// Returns a feature if the intersection of the two shapes results in an object of the same dimension, but different from both of the shapes. Applies to Area/Area, Line/Line, and Multi-point/Multi-point shape type combinations.
overlaps: function (geometry) {
this._setGeometryParams(geometry);
this.params.spatialRel = 'esriSpatialRelOverlaps';
return this;
},
// Returns a feature if the envelope of the two shapes intersects.
bboxIntersects: function (geometry) {
this._setGeometryParams(geometry);
this.params.spatialRel = 'esriSpatialRelEnvelopeIntersects';
return this;
},
// if someone can help decipher the ArcObjects explanation and translate to plain speak, we should mention this method in the doc
indexIntersects: function (geometry) {
this._setGeometryParams(geometry);
this.params.spatialRel = 'esriSpatialRelIndexIntersects'; // Returns a feature if the envelope of the query geometry intersects the index entry for the target geometry
return this;
},
// only valid for Feature Services running on ArcGIS Server 10.3+ or ArcGIS Online
nearby: function (latlng, radius) {
latlng = latLng(latlng);
this.params.geometry = [latlng.lng, latlng.lat];
this.params.geometryType = 'esriGeometryPoint';
this.params.spatialRel = 'esriSpatialRelIntersects';
this.params.units = 'esriSRUnit_Meter';
this.params.distance = radius;
this.params.inSR = 4326;
return this;
},
where: function (string) {
// instead of converting double-quotes to single quotes, pass as is, and provide a more informative message if a 400 is encountered
this.params.where = string;
return this;
},
between: function (start, end) {
this.params.time = [start.valueOf(), end.valueOf()];
return this;
},
simplify: function (map, factor) {
var mapWidth = Math.abs(map.getBounds().getWest() - map.getBounds().getEast());
this.params.maxAllowableOffset = (mapWidth / map.getSize().y) * factor;
return this;
},
orderBy: function (fieldName, order) {
order = order || 'ASC';
this.params.orderByFields = (this.params.orderByFields) ? this.params.orderByFields + ',' : '';
this.params.orderByFields += ([fieldName, order]).join(' ');
return this;
},
run: function (callback, context) {
this._cleanParams();
// services hosted on ArcGIS Online and ArcGIS Server 10.3.1+ support requesting geojson directly
if (this.options.isModern || (isArcgisOnline(this.options.url) && this.options.isModern === undefined)) {
this.params.f = 'geojson';
return this.request(function (error, response) {
this._trapSQLerrors(error);
callback.call(context, error, response, response);
}, this);
// otherwise convert it in the callback then pass it on
} else {
return this.request(function (error, response) {
this._trapSQLerrors(error);
callback.call(context, error, (response && responseToFeatureCollection(response)), response);
}, this);
}
},
count: function (callback, context) {
this._cleanParams();
this.params.returnCountOnly = true;
return this.request(function (error, response) {
callback.call(this, error, (response && response.count), response);
}, context);
},
ids: function (callback, context) {
this._cleanParams();
this.params.returnIdsOnly = true;
return this.request(function (error, response) {
callback.call(this, error, (response && response.objectIds), response);
}, context);
},
// only valid for Feature Services running on ArcGIS Server 10.3+ or ArcGIS Online
bounds: function (callback, context) {
this._cleanParams();
this.params.returnExtentOnly = true;
return this.request(function (error, response) {
if (response && response.extent && extentToBounds(response.extent)) {
callback.call(context, error, extentToBounds(response.extent), response);
} else {
error = {
message: 'Invalid Bounds'
};
callback.call(context, error, null, response);
}
}, context);
},
distinct: function () {
// geometry must be omitted for queries requesting distinct values
this.params.returnGeometry = false;
this.params.returnDistinctValues = true;
return this;
},
// only valid for image services
pixelSize: function (rawPoint) {
var castPoint = point(rawPoint);
this.params.pixelSize = [castPoint.x, castPoint.y];
return this;
},
// only valid for map services
layer: function (layer) {
this.path = layer + '/query';
return this;
},
_trapSQLerrors: function (error) {
if (error) {
if (error.code === '400') {
warn('one common syntax error in query requests is encasing string values in double quotes instead of single quotes');
}
}
},
_cleanParams: function () {
delete this.params.returnIdsOnly;
delete this.params.returnExtentOnly;
delete this.params.returnCountOnly;
},
_setGeometryParams: function (geometry) {
this.params.inSR = 4326;
var converted = _setGeometry(geometry);
this.params.geometry = converted.geometry;
this.params.geometryType = converted.geometryType;
}
});
export function query (options) {
return new Query(options);
}
export default query;