@esri/arcgis-rest-routing 3.4.2 → 4.0.0-beta.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (53) hide show
  1. package/README.md +4 -5
  2. package/dist/bundled/routing.esm.js +736 -0
  3. package/dist/bundled/routing.esm.js.map +1 -0
  4. package/dist/bundled/routing.esm.min.js +12 -0
  5. package/dist/bundled/routing.esm.min.js.map +1 -0
  6. package/dist/bundled/routing.umd.js +754 -0
  7. package/dist/bundled/routing.umd.js.map +1 -0
  8. package/dist/bundled/routing.umd.min.js +12 -0
  9. package/dist/bundled/routing.umd.min.js.map +1 -0
  10. package/dist/{node → cjs}/closestFacility.js +18 -18
  11. package/dist/cjs/closestFacility.js.map +1 -0
  12. package/dist/{node → cjs}/helpers.js +11 -11
  13. package/dist/cjs/helpers.js.map +1 -0
  14. package/dist/cjs/index.js +11 -0
  15. package/dist/cjs/index.js.map +1 -0
  16. package/dist/{node → cjs}/originDestinationMatrix.js +22 -23
  17. package/dist/cjs/originDestinationMatrix.js.map +1 -0
  18. package/dist/cjs/package.json +3 -0
  19. package/dist/{node → cjs}/serviceArea.js +15 -16
  20. package/dist/cjs/serviceArea.js.map +1 -0
  21. package/dist/{node → cjs}/solveRoute.js +16 -17
  22. package/dist/cjs/solveRoute.js.map +1 -0
  23. package/dist/esm/closestFacility.d.ts +3 -2
  24. package/dist/esm/closestFacility.js +9 -9
  25. package/dist/esm/closestFacility.js.map +1 -1
  26. package/dist/esm/helpers.d.ts +1 -2
  27. package/dist/esm/helpers.js +15 -15
  28. package/dist/esm/helpers.js.map +1 -1
  29. package/dist/esm/index.d.ts +6 -6
  30. package/dist/esm/index.js +5 -5
  31. package/dist/esm/index.js.map +1 -1
  32. package/dist/esm/originDestinationMatrix.d.ts +4 -4
  33. package/dist/esm/originDestinationMatrix.js +8 -9
  34. package/dist/esm/originDestinationMatrix.js.map +1 -1
  35. package/dist/esm/package.json +3 -0
  36. package/dist/esm/serviceArea.d.ts +2 -2
  37. package/dist/esm/serviceArea.js +8 -9
  38. package/dist/esm/serviceArea.js.map +1 -1
  39. package/dist/esm/solveRoute.d.ts +2 -2
  40. package/dist/esm/solveRoute.js +10 -11
  41. package/dist/esm/solveRoute.js.map +1 -1
  42. package/package.json +47 -41
  43. package/dist/node/closestFacility.js.map +0 -1
  44. package/dist/node/helpers.js.map +0 -1
  45. package/dist/node/index.js +0 -11
  46. package/dist/node/index.js.map +0 -1
  47. package/dist/node/originDestinationMatrix.js.map +0 -1
  48. package/dist/node/serviceArea.js.map +0 -1
  49. package/dist/node/solveRoute.js.map +0 -1
  50. package/dist/umd/routing.umd.js +0 -779
  51. package/dist/umd/routing.umd.js.map +0 -1
  52. package/dist/umd/routing.umd.min.js +0 -12
  53. package/dist/umd/routing.umd.min.js.map +0 -1
@@ -0,0 +1,736 @@
1
+ /* @preserve
2
+ * @esri/arcgis-rest-routing - v4.0.0-beta.2 - Apache-2.0
3
+ * Copyright (c) 2017-2022 Esri, Inc.
4
+ * Thu Mar 10 2022 23:21:15 GMT+0000 (Coordinated Universal Time)
5
+ */
6
+ import { request, cleanUrl, appendCustomParams } from '@esri/arcgis-rest-request';
7
+
8
+ /* Copyright (c) 2018 Environmental Systems Research Institute, Inc.
9
+ * Apache-2.0 */
10
+ // https always
11
+ const ARCGIS_ONLINE_ROUTING_URL = "https://route.arcgis.com/arcgis/rest/services/World/Route/NAServer/Route_World";
12
+ const ARCGIS_ONLINE_CLOSEST_FACILITY_URL = "https://route.arcgis.com/arcgis/rest/services/World/ClosestFacility/NAServer/ClosestFacility_World";
13
+ const ARCGIS_ONLINE_SERVICE_AREA_URL = "https://route.arcgis.com/arcgis/rest/services/World/ServiceAreas/NAServer/ServiceArea_World";
14
+ const ARCGIS_ONLINE_ORIGIN_DESTINATION_MATRIX_URL = "https://route.arcgis.com/arcgis/rest/services/World/OriginDestinationCostMatrix/NAServer/OriginDestinationCostMatrix_World";
15
+ function isLocationArray$1(coords) {
16
+ return (coords.length === 2 ||
17
+ coords.length === 3);
18
+ }
19
+ function isLocation$1(coords) {
20
+ return (coords.latitude !== undefined ||
21
+ coords.lat !== undefined);
22
+ }
23
+ function normalizeLocationsList(locations) {
24
+ return locations.map((coords) => {
25
+ if (isLocationArray$1(coords)) {
26
+ return coords.join();
27
+ }
28
+ else if (isLocation$1(coords)) {
29
+ if (coords.lat) {
30
+ return coords.long + "," + coords.lat;
31
+ }
32
+ else {
33
+ return coords.longitude + "," + coords.latitude;
34
+ }
35
+ }
36
+ else {
37
+ return coords.x + "," + coords.y;
38
+ }
39
+ });
40
+ }
41
+ function decompressGeometry(str) {
42
+ let xDiffPrev = 0;
43
+ let yDiffPrev = 0;
44
+ const points = [];
45
+ let x;
46
+ let y;
47
+ // Split the string into an array on the + and - characters
48
+ const strings = str.match(/((\+|-)[^+-]+)/g);
49
+ // The first value is the coefficient in base 32
50
+ const coefficient = parseInt(strings[0], 32);
51
+ for (let j = 1; j < strings.length; j += 2) {
52
+ // j is the offset for the x value
53
+ // Convert the value from base 32 and add the previous x value
54
+ x = parseInt(strings[j], 32) + xDiffPrev;
55
+ xDiffPrev = x;
56
+ // j+1 is the offset for the y value
57
+ // Convert the value from base 32 and add the previous y value
58
+ y = parseInt(strings[j + 1], 32) + yDiffPrev;
59
+ yDiffPrev = y;
60
+ points.push([x / coefficient, y / coefficient]);
61
+ }
62
+ return {
63
+ paths: [points]
64
+ };
65
+ }
66
+ /**
67
+ * User Defined Type Guard that verifies this is a featureSet
68
+ */
69
+ function isFeatureSet(arg) {
70
+ return Object.prototype.hasOwnProperty.call(arg, "features");
71
+ }
72
+
73
+ /* @preserve
74
+ * @terraformer/arcgis - v2.0.7 - MIT
75
+ * Copyright (c) 2012-2021 Environmental Systems Research Institute, Inc.
76
+ * Thu Jul 22 2021 13:58:30 GMT-0700 (Pacific Daylight Time)
77
+ */
78
+ /* Copyright (c) 2012-2019 Environmental Systems Research Institute, Inc.
79
+ * Apache-2.0 */
80
+
81
+ var edgeIntersectsEdge = function edgeIntersectsEdge(a1, a2, b1, b2) {
82
+ var uaT = (b2[0] - b1[0]) * (a1[1] - b1[1]) - (b2[1] - b1[1]) * (a1[0] - b1[0]);
83
+ var ubT = (a2[0] - a1[0]) * (a1[1] - b1[1]) - (a2[1] - a1[1]) * (a1[0] - b1[0]);
84
+ var uB = (b2[1] - b1[1]) * (a2[0] - a1[0]) - (b2[0] - b1[0]) * (a2[1] - a1[1]);
85
+
86
+ if (uB !== 0) {
87
+ var ua = uaT / uB;
88
+ var ub = ubT / uB;
89
+
90
+ if (ua >= 0 && ua <= 1 && ub >= 0 && ub <= 1) {
91
+ return true;
92
+ }
93
+ }
94
+
95
+ return false;
96
+ };
97
+ var coordinatesContainPoint = function coordinatesContainPoint(coordinates, point) {
98
+ var contains = false;
99
+
100
+ for (var i = -1, l = coordinates.length, j = l - 1; ++i < l; j = i) {
101
+ if ((coordinates[i][1] <= point[1] && point[1] < coordinates[j][1] || coordinates[j][1] <= point[1] && point[1] < coordinates[i][1]) && point[0] < (coordinates[j][0] - coordinates[i][0]) * (point[1] - coordinates[i][1]) / (coordinates[j][1] - coordinates[i][1]) + coordinates[i][0]) {
102
+ contains = !contains;
103
+ }
104
+ }
105
+
106
+ return contains;
107
+ };
108
+ var pointsEqual = function pointsEqual(a, b) {
109
+ for (var i = 0; i < a.length; i++) {
110
+ if (a[i] !== b[i]) {
111
+ return false;
112
+ }
113
+ }
114
+
115
+ return true;
116
+ };
117
+ var arrayIntersectsArray = function arrayIntersectsArray(a, b) {
118
+ for (var i = 0; i < a.length - 1; i++) {
119
+ for (var j = 0; j < b.length - 1; j++) {
120
+ if (edgeIntersectsEdge(a[i], a[i + 1], b[j], b[j + 1])) {
121
+ return true;
122
+ }
123
+ }
124
+ }
125
+
126
+ return false;
127
+ };
128
+
129
+ /* Copyright (c) 2012-2019 Environmental Systems Research Institute, Inc.
130
+ * Apache-2.0 */
131
+
132
+ var closeRing = function closeRing(coordinates) {
133
+ if (!pointsEqual(coordinates[0], coordinates[coordinates.length - 1])) {
134
+ coordinates.push(coordinates[0]);
135
+ }
136
+
137
+ return coordinates;
138
+ }; // determine if polygon ring coordinates are clockwise. clockwise signifies outer ring, counter-clockwise an inner ring
139
+ // or hole. this logic was found at http://stackoverflow.com/questions/1165647/how-to-determine-if-a-list-of-polygon-
140
+ // points-are-in-clockwise-order
141
+
142
+ var ringIsClockwise = function ringIsClockwise(ringToTest) {
143
+ var total = 0;
144
+ var i = 0;
145
+ var rLength = ringToTest.length;
146
+ var pt1 = ringToTest[i];
147
+ var pt2;
148
+
149
+ for (i; i < rLength - 1; i++) {
150
+ pt2 = ringToTest[i + 1];
151
+ total += (pt2[0] - pt1[0]) * (pt2[1] + pt1[1]);
152
+ pt1 = pt2;
153
+ }
154
+
155
+ return total >= 0;
156
+ }; // This function ensures that rings are oriented in the right directions
157
+ // from http://jsperf.com/cloning-an-object/2
158
+
159
+ var shallowClone = function shallowClone(obj) {
160
+ var target = {};
161
+
162
+ for (var i in obj) {
163
+ // both arcgis attributes and geojson props are just hardcoded keys
164
+ if (obj.hasOwnProperty(i)) {
165
+ // eslint-disable-line no-prototype-builtins
166
+ target[i] = obj[i];
167
+ }
168
+ }
169
+
170
+ return target;
171
+ };
172
+
173
+ /* Copyright (c) 2012-2019 Environmental Systems Research Institute, Inc.
174
+ * Apache-2.0 */
175
+
176
+ var coordinatesContainCoordinates = function coordinatesContainCoordinates(outer, inner) {
177
+ var intersects = arrayIntersectsArray(outer, inner);
178
+ var contains = coordinatesContainPoint(outer, inner[0]);
179
+
180
+ if (!intersects && contains) {
181
+ return true;
182
+ }
183
+
184
+ return false;
185
+ }; // do any polygons in this array contain any other polygons in this array?
186
+ // used for checking for holes in arcgis rings
187
+
188
+
189
+ var convertRingsToGeoJSON = function convertRingsToGeoJSON(rings) {
190
+ var outerRings = [];
191
+ var holes = [];
192
+ var x; // iterator
193
+
194
+ var outerRing; // current outer ring being evaluated
195
+
196
+ var hole; // current hole being evaluated
197
+ // for each ring
198
+
199
+ for (var r = 0; r < rings.length; r++) {
200
+ var ring = closeRing(rings[r].slice(0));
201
+
202
+ if (ring.length < 4) {
203
+ continue;
204
+ } // is this ring an outer ring? is it clockwise?
205
+
206
+
207
+ if (ringIsClockwise(ring)) {
208
+ var polygon = [ring.slice().reverse()]; // wind outer rings counterclockwise for RFC 7946 compliance
209
+
210
+ outerRings.push(polygon); // push to outer rings
211
+ } else {
212
+ holes.push(ring.slice().reverse()); // wind inner rings clockwise for RFC 7946 compliance
213
+ }
214
+ }
215
+
216
+ var uncontainedHoles = []; // while there are holes left...
217
+
218
+ while (holes.length) {
219
+ // pop a hole off out stack
220
+ hole = holes.pop(); // loop over all outer rings and see if they contain our hole.
221
+
222
+ var contained = false;
223
+
224
+ for (x = outerRings.length - 1; x >= 0; x--) {
225
+ outerRing = outerRings[x][0];
226
+
227
+ if (coordinatesContainCoordinates(outerRing, hole)) {
228
+ // the hole is contained push it into our polygon
229
+ outerRings[x].push(hole);
230
+ contained = true;
231
+ break;
232
+ }
233
+ } // ring is not contained in any outer ring
234
+ // sometimes this happens https://github.com/Esri/esri-leaflet/issues/320
235
+
236
+
237
+ if (!contained) {
238
+ uncontainedHoles.push(hole);
239
+ }
240
+ } // if we couldn't match any holes using contains we can try intersects...
241
+
242
+
243
+ while (uncontainedHoles.length) {
244
+ // pop a hole off out stack
245
+ hole = uncontainedHoles.pop(); // loop over all outer rings and see if any intersect our hole.
246
+
247
+ var intersects = false;
248
+
249
+ for (x = outerRings.length - 1; x >= 0; x--) {
250
+ outerRing = outerRings[x][0];
251
+
252
+ if (arrayIntersectsArray(outerRing, hole)) {
253
+ // the hole is contained push it into our polygon
254
+ outerRings[x].push(hole);
255
+ intersects = true;
256
+ break;
257
+ }
258
+ }
259
+
260
+ if (!intersects) {
261
+ outerRings.push([hole.reverse()]);
262
+ }
263
+ }
264
+
265
+ if (outerRings.length === 1) {
266
+ return {
267
+ type: 'Polygon',
268
+ coordinates: outerRings[0]
269
+ };
270
+ } else {
271
+ return {
272
+ type: 'MultiPolygon',
273
+ coordinates: outerRings
274
+ };
275
+ }
276
+ };
277
+
278
+ var getId = function getId(attributes, idAttribute) {
279
+ var keys = idAttribute ? [idAttribute, 'OBJECTID', 'FID'] : ['OBJECTID', 'FID'];
280
+
281
+ for (var i = 0; i < keys.length; i++) {
282
+ var key = keys[i];
283
+
284
+ if (key in attributes && (typeof attributes[key] === 'string' || typeof attributes[key] === 'number')) {
285
+ return attributes[key];
286
+ }
287
+ }
288
+
289
+ throw Error('No valid id attribute found');
290
+ };
291
+
292
+ var arcgisToGeoJSON = function arcgisToGeoJSON(arcgis, idAttribute) {
293
+ var geojson = {};
294
+
295
+ if (arcgis.features) {
296
+ geojson.type = 'FeatureCollection';
297
+ geojson.features = [];
298
+
299
+ for (var i = 0; i < arcgis.features.length; i++) {
300
+ geojson.features.push(arcgisToGeoJSON(arcgis.features[i], idAttribute));
301
+ }
302
+ }
303
+
304
+ if (typeof arcgis.x === 'number' && typeof arcgis.y === 'number') {
305
+ geojson.type = 'Point';
306
+ geojson.coordinates = [arcgis.x, arcgis.y];
307
+
308
+ if (typeof arcgis.z === 'number') {
309
+ geojson.coordinates.push(arcgis.z);
310
+ }
311
+ }
312
+
313
+ if (arcgis.points) {
314
+ geojson.type = 'MultiPoint';
315
+ geojson.coordinates = arcgis.points.slice(0);
316
+ }
317
+
318
+ if (arcgis.paths) {
319
+ if (arcgis.paths.length === 1) {
320
+ geojson.type = 'LineString';
321
+ geojson.coordinates = arcgis.paths[0].slice(0);
322
+ } else {
323
+ geojson.type = 'MultiLineString';
324
+ geojson.coordinates = arcgis.paths.slice(0);
325
+ }
326
+ }
327
+
328
+ if (arcgis.rings) {
329
+ geojson = convertRingsToGeoJSON(arcgis.rings.slice(0));
330
+ }
331
+
332
+ if (typeof arcgis.xmin === 'number' && typeof arcgis.ymin === 'number' && typeof arcgis.xmax === 'number' && typeof arcgis.ymax === 'number') {
333
+ geojson.type = 'Polygon';
334
+ geojson.coordinates = [[[arcgis.xmax, arcgis.ymax], [arcgis.xmin, arcgis.ymax], [arcgis.xmin, arcgis.ymin], [arcgis.xmax, arcgis.ymin], [arcgis.xmax, arcgis.ymax]]];
335
+ }
336
+
337
+ if (arcgis.geometry || arcgis.attributes) {
338
+ geojson.type = 'Feature';
339
+ geojson.geometry = arcgis.geometry ? arcgisToGeoJSON(arcgis.geometry) : null;
340
+ geojson.properties = arcgis.attributes ? shallowClone(arcgis.attributes) : null;
341
+
342
+ if (arcgis.attributes) {
343
+ try {
344
+ geojson.id = getId(arcgis.attributes, idAttribute);
345
+ } catch (err) {// don't set an id
346
+ }
347
+ }
348
+ } // if no valid geometry was encountered
349
+
350
+
351
+ if (JSON.stringify(geojson.geometry) === JSON.stringify({})) {
352
+ geojson.geometry = null;
353
+ }
354
+
355
+ if (arcgis.spatialReference && arcgis.spatialReference.wkid && arcgis.spatialReference.wkid !== 4326) {
356
+ console.warn('Object converted in non-standard crs - ' + JSON.stringify(arcgis.spatialReference));
357
+ }
358
+
359
+ return geojson;
360
+ };
361
+
362
+ /* Copyright (c) 2018 Environmental Systems Research Institute, Inc.
363
+ * Apache-2.0 */
364
+ function isLocationArray(coords) {
365
+ return (coords.length === 2 ||
366
+ coords.length === 3);
367
+ }
368
+ function isLocation(coords) {
369
+ return (coords.latitude !== undefined ||
370
+ coords.lat !== undefined);
371
+ }
372
+ /**
373
+ * ```js
374
+ * import { solveRoute } from '@esri/arcgis-rest-routing';
375
+ * //
376
+ * solveRoute({
377
+ * stops: [
378
+ * [-117.195677, 34.056383],
379
+ * [-117.918976, 33.812092],
380
+ * ],
381
+ * authentication
382
+ * })
383
+ * .then(response) // => {routes: {features: [{attributes: { ... }, geometry:{ ... }}]}
384
+ * ```
385
+ * Used to find the best way to get from one location to another or to visit several locations. See the [REST Documentation](https://developers.arcgis.com/rest/network/api-reference/route-synchronous-service.htm) for more information.
386
+ *
387
+ * @param requestOptions Options to pass through to the routing service.
388
+ * @returns A Promise that will resolve with routes and directions for the request.
389
+ * @restlink https://developers.arcgis.com/rest/network/api-reference/route-synchronous-service.htm
390
+ */
391
+ function solveRoute(requestOptions) {
392
+ const options = Object.assign({ endpoint: requestOptions.endpoint || ARCGIS_ONLINE_ROUTING_URL, params: {} }, requestOptions);
393
+ // the SAAS service does not support anonymous requests
394
+ if (!requestOptions.authentication &&
395
+ options.endpoint === ARCGIS_ONLINE_ROUTING_URL) {
396
+ return Promise.reject("Routing using the ArcGIS service requires authentication");
397
+ }
398
+ if (isFeatureSet(requestOptions.stops)) {
399
+ options.params.stops = requestOptions.stops;
400
+ }
401
+ else {
402
+ const stops = requestOptions.stops.map((coords) => {
403
+ if (isLocationArray(coords)) {
404
+ return coords.join();
405
+ }
406
+ else if (isLocation(coords)) {
407
+ if (coords.lat) {
408
+ return (coords.long + "," + coords.lat + (coords.z ? "," + coords.z : ""));
409
+ }
410
+ else {
411
+ return (coords.longitude +
412
+ "," +
413
+ coords.latitude +
414
+ (coords.z ? "," + coords.z : ""));
415
+ }
416
+ }
417
+ else {
418
+ return coords.x + "," + coords.y + (coords.z ? "," + coords.z : "");
419
+ }
420
+ });
421
+ options.params.stops = stops.join(";");
422
+ }
423
+ return request(`${cleanUrl(options.endpoint)}/solve`, options).then(cleanResponse$3);
424
+ }
425
+ function cleanResponse$3(res) {
426
+ if (res.directions && res.directions.length > 0) {
427
+ res.directions = res.directions.map((direction) => {
428
+ direction.features = direction.features.map((feature) => {
429
+ feature.geometry = decompressGeometry(feature.compressedGeometry);
430
+ return feature;
431
+ });
432
+ return direction;
433
+ });
434
+ }
435
+ // add "geoJson" property to "routes"
436
+ if (res.routes.spatialReference.wkid === 4326) {
437
+ const features = res.routes.features.map((feature) => {
438
+ return {
439
+ type: "Feature",
440
+ geometry: arcgisToGeoJSON(feature.geometry),
441
+ properties: Object.assign({}, feature.attributes)
442
+ };
443
+ });
444
+ res.routes.geoJson = {
445
+ type: "FeatureCollection",
446
+ features
447
+ };
448
+ }
449
+ return res;
450
+ }
451
+
452
+ /* Copyright (c) 2018 Environmental Systems Research Institute, Inc.
453
+ * Apache-2.0 */
454
+ function getTravelDirection$1(key) {
455
+ if (key === "incidentsToFacilities") {
456
+ return "esriNATravelDirectionFromFacility";
457
+ }
458
+ else {
459
+ return "esriNATravelDirectionToFacility";
460
+ }
461
+ }
462
+ /**
463
+ * ```js
464
+ * import { closestFacility } from '@esri/arcgis-rest-routing';
465
+ * //
466
+ * closestFacility({
467
+ * incidents: [
468
+ * [-90.404302, 38.600621],
469
+ * [-90.364293, 38.620427],
470
+ * ],
471
+ * facilities: [
472
+ * [-90.444716, 38.635501],
473
+ * [-90.311919, 38.633523],
474
+ * [-90.451147, 38.581107]
475
+ * ],
476
+ * authentication
477
+ * })
478
+ * .then(response) // => {routes: {features: [{attributes: { ... }, geometry:{ ... }}]}
479
+ * ```
480
+ * Used to find a route to the nearest of several possible destinations. See the [REST Documentation](https://developers.arcgis.com/rest/network/api-reference/closest-facility-synchronous-service.htm) for more information.
481
+ *
482
+ * @param requestOptions Options to pass through to the routing service.
483
+ * @returns A Promise that will resolve with routes and directions for the request.
484
+ * @restlink https://developers.arcgis.com/rest/network/api-reference/closest-facility-synchronous-service.htm
485
+ * @inline IClosestFacilityOptions
486
+ */
487
+ function closestFacility(requestOptions) {
488
+ const endpoint = requestOptions.endpoint || ARCGIS_ONLINE_CLOSEST_FACILITY_URL;
489
+ requestOptions.params = Object.assign({ returnFacilities: true, returnDirections: true, returnIncidents: true, returnBarriers: true, returnPolylineBarriers: true, returnPolygonBarriers: true, preserveObjectID: true }, requestOptions.params);
490
+ const options = appendCustomParams(requestOptions, [
491
+ "returnCFRoutes",
492
+ // "travelDirection",
493
+ "barriers",
494
+ "polylineBarriers",
495
+ "polygonBarriers",
496
+ "returnDirections",
497
+ "directionsOutputType",
498
+ "directionsLengthUnits",
499
+ "outputLines",
500
+ "returnFacilities",
501
+ "returnIncidents",
502
+ "returnBarriers",
503
+ "returnPolylineBarriers",
504
+ "returnPolygonBarriers",
505
+ "preserveObjectID"
506
+ ]);
507
+ // Set travelDirection
508
+ if (requestOptions.travelDirection) {
509
+ options.params.travelDirection = getTravelDirection$1(requestOptions.travelDirection);
510
+ }
511
+ // the SAAS service does not support anonymous requests
512
+ if (!requestOptions.authentication &&
513
+ endpoint === ARCGIS_ONLINE_CLOSEST_FACILITY_URL) {
514
+ return Promise.reject("Finding the closest facility using the ArcGIS service requires authentication");
515
+ }
516
+ if (isFeatureSet(requestOptions.incidents)) {
517
+ options.params.incidents = requestOptions.incidents;
518
+ }
519
+ else {
520
+ options.params.incidents = normalizeLocationsList(requestOptions.incidents).join(";");
521
+ }
522
+ if (isFeatureSet(requestOptions.facilities)) {
523
+ options.params.facilities = requestOptions.facilities;
524
+ }
525
+ else {
526
+ options.params.facilities = normalizeLocationsList(requestOptions.facilities).join(";");
527
+ }
528
+ // optional input param that may need point geometry normalizing
529
+ if (requestOptions.barriers) {
530
+ if (isFeatureSet(requestOptions.barriers)) {
531
+ options.params.barriers = requestOptions.barriers;
532
+ }
533
+ else {
534
+ // optional point geometry barriers must be normalized, too
535
+ // but not if provided as IFeatureSet type
536
+ // note that optional polylineBarriers and polygonBarriers do not need to be normalized
537
+ options.params.barriers = normalizeLocationsList(requestOptions.barriers).join(";");
538
+ }
539
+ }
540
+ return request(`${cleanUrl(endpoint)}/solveClosestFacility`, options).then(cleanResponse$2);
541
+ }
542
+ function cleanResponse$2(res) {
543
+ // add "geoJson" property to "routes"
544
+ if (res.routes.spatialReference.wkid === 4326) {
545
+ res.routes.geoJson = arcgisToGeoJSON(res.routes);
546
+ }
547
+ return res;
548
+ }
549
+
550
+ /* Copyright (c) 2018 Environmental Systems Research Institute, Inc.
551
+ * Apache-2.0 */
552
+ function getTravelDirection(key) {
553
+ if (key === "incidentsToFacilities") {
554
+ return "esriNATravelDirectionFromFacility";
555
+ }
556
+ else {
557
+ return "esriNATravelDirectionToFacility";
558
+ }
559
+ }
560
+ /**
561
+ * ```js
562
+ * import { serviceArea } from '@esri/arcgis-rest-routing';
563
+ * //
564
+ * serviceArea({
565
+ * facilities: [
566
+ * [-90.444716, 38.635501],
567
+ * [-90.311919, 38.633523],
568
+ * [-90.451147, 38.581107]
569
+ * ],
570
+ * authentication
571
+ * })
572
+ * .then(response) // => {routes: {features: [{attributes: { ... }, geometry:{ ... }}]}
573
+ * ```
574
+ * Used to find the area that can be reached from the input location within a given travel time or travel distance. See the [REST Documentation](https://developers.arcgis.com/rest/network/api-reference/service-area-synchronous-service.htm) for more information.
575
+ *
576
+ * @param requestOptions Options to pass through to the routing service.
577
+ * @returns A Promise that will resolve with service area polygons for the request.
578
+ * @restlink https://developers.arcgis.com/rest/network/api-reference/service-area-synchronous-service.htm
579
+ */
580
+ function serviceArea(requestOptions) {
581
+ const endpoint = requestOptions.endpoint || ARCGIS_ONLINE_SERVICE_AREA_URL;
582
+ requestOptions.params = Object.assign({ returnFacilities: true, returnBarriers: true, returnPolylineBarriers: true, returnPolygonBarriers: true, preserveObjectID: true }, requestOptions.params);
583
+ const options = appendCustomParams(requestOptions, [
584
+ "barriers",
585
+ "polylineBarriers",
586
+ "polygonBarriers",
587
+ "outputLines",
588
+ "returnFacilities",
589
+ "returnBarriers",
590
+ "returnPolylineBarriers",
591
+ "returnPolygonBarriers",
592
+ "preserveObjectID"
593
+ ]);
594
+ // Set travelDirection
595
+ if (requestOptions.travelDirection) {
596
+ options.params.travelDirection = getTravelDirection(requestOptions.travelDirection);
597
+ }
598
+ // the SAAS service does not support anonymous requests
599
+ if (!requestOptions.authentication &&
600
+ endpoint === ARCGIS_ONLINE_SERVICE_AREA_URL) {
601
+ return Promise.reject("Finding service areas using the ArcGIS service requires authentication");
602
+ }
603
+ if (isFeatureSet(requestOptions.facilities)) {
604
+ options.params.facilities = requestOptions.facilities;
605
+ }
606
+ else {
607
+ options.params.facilities = normalizeLocationsList(requestOptions.facilities).join(";");
608
+ }
609
+ // optional input param that may need point geometry normalizing
610
+ if (requestOptions.barriers) {
611
+ if (isFeatureSet(requestOptions.barriers)) {
612
+ options.params.barriers = requestOptions.barriers;
613
+ }
614
+ else {
615
+ // optional point geometry barriers must be normalized, too
616
+ // but not if provided as IFeatureSet type
617
+ // note that optional polylineBarriers and polygonBarriers do not need to be normalized
618
+ options.params.barriers = normalizeLocationsList(requestOptions.barriers).join(";");
619
+ }
620
+ }
621
+ return request(`${cleanUrl(endpoint)}/solveServiceArea`, options).then(cleanResponse$1);
622
+ }
623
+ function cleanResponse$1(res) {
624
+ // remove "fieldAliases" because it does not do anything.
625
+ delete res.saPolygons.fieldAliases;
626
+ // add "geoJson" property to "saPolygons"
627
+ if (res.saPolygons.spatialReference.wkid === 4326) {
628
+ res.saPolygons.geoJson = arcgisToGeoJSON(res.saPolygons);
629
+ }
630
+ return res;
631
+ }
632
+
633
+ /* Copyright (c) 2018 Environmental Systems Research Institute, Inc.
634
+ * Apache-2.0 */
635
+ /**
636
+ * ```js
637
+ * import { originDestinationMatrix } from '@esri/arcgis-rest-routing';
638
+ * //
639
+ * originDestinationMatrix({
640
+ * origins: [
641
+ * [-90.404302, 38.600621],
642
+ * [-90.364293, 38.620427],
643
+ * ],
644
+ * destinations: [
645
+ * [-90.444716, 38.635501],
646
+ * [-90.311919, 38.633523],
647
+ * [-90.451147, 38.581107]
648
+ * ],
649
+ * authentication
650
+ * })
651
+ * .then(response) // => { ... }
652
+ * ```
653
+ * Used to create an origin-destination (OD) cost matrix from multiple origins to multiple destinations. See the [REST Documentation](https://developers.arcgis.com/rest/network/api-reference/origin-destination-cost-matrix-synchronous-service.htm) for more information.
654
+ *
655
+ * @param requestOptions Options to pass through to the routing service.
656
+ * @returns A Promise that will resolve with travel time and/or distance for each origin-destination pair. It returns either odLines or odCostMatrix for this information depending on the outputType you specify.
657
+ * @restlink https://developers.arcgis.com/rest/network/api-reference/origin-destination-cost-matrix-synchronous-service.htm
658
+ */
659
+ function originDestinationMatrix(requestOptions) {
660
+ const endpoint = requestOptions.endpoint || ARCGIS_ONLINE_ORIGIN_DESTINATION_MATRIX_URL;
661
+ requestOptions.params = Object.assign({ outputType: "esriNAODOutputSparseMatrix", returnOrigins: true, returnDestinations: true, returnBarriers: true, returnPolylineBarriers: true, returnPolygonBarriers: true }, requestOptions.params);
662
+ const options = appendCustomParams(requestOptions, [
663
+ "outputType",
664
+ "barriers",
665
+ "polylineBarriers",
666
+ "polygonBarriers",
667
+ "returnOrigins",
668
+ "returnDestinations",
669
+ "returnBarriers",
670
+ "returnPolylineBarriers",
671
+ "returnPolygonBarriers"
672
+ ]);
673
+ // the SAAS service does not support anonymous requests
674
+ if (!requestOptions.authentication &&
675
+ endpoint === ARCGIS_ONLINE_ORIGIN_DESTINATION_MATRIX_URL) {
676
+ return Promise.reject("Calculating the origin-destination cost matrix using the ArcGIS service requires authentication");
677
+ }
678
+ // use a formatting helper for input params of this type: Array<IPoint | ILocation | [number, number]>
679
+ if (isFeatureSet(requestOptions.origins)) {
680
+ options.params.origins = requestOptions.origins;
681
+ }
682
+ else {
683
+ options.params.origins = normalizeLocationsList(requestOptions.origins).join(";");
684
+ }
685
+ if (isFeatureSet(requestOptions.destinations)) {
686
+ options.params.destinations = requestOptions.destinations;
687
+ }
688
+ else {
689
+ options.params.destinations = normalizeLocationsList(requestOptions.destinations).join(";");
690
+ }
691
+ // optional input param that may need point geometry normalizing
692
+ if (requestOptions.barriers) {
693
+ if (isFeatureSet(requestOptions.barriers)) {
694
+ options.params.barriers = requestOptions.barriers;
695
+ }
696
+ else {
697
+ // optional point geometry barriers must be normalized, too
698
+ // but not if provided as IFeatureSet type
699
+ // note that optional polylineBarriers and polygonBarriers do not need to be normalized
700
+ options.params.barriers = normalizeLocationsList(requestOptions.barriers).join(";");
701
+ }
702
+ }
703
+ return request(`${cleanUrl(endpoint)}/solveODCostMatrix`, options).then(function (res) {
704
+ return cleanResponse(res, options);
705
+ });
706
+ }
707
+ function cleanResponse(res, options) {
708
+ // add "geoJson" property to each response property that is an arcgis featureSet
709
+ // res.odLines only exists and only includes geometry in this condition (out of 3 possible options.params.outputType conditions)
710
+ if (options.params.outputType === "esriNAODOutputStraightLines" &&
711
+ res.odLines &&
712
+ res.odLines.spatialReference.wkid === 4326) {
713
+ res.odLines.geoJson = arcgisToGeoJSON(res.odLines);
714
+ }
715
+ if (res.origins && res.origins.spatialReference.wkid === 4326) {
716
+ res.origins.geoJson = arcgisToGeoJSON(res.origins);
717
+ }
718
+ if (res.destinations && res.destinations.spatialReference.wkid === 4326) {
719
+ res.destinations.geoJson = arcgisToGeoJSON(res.destinations);
720
+ }
721
+ if (res.barriers && res.barriers.spatialReference.wkid === 4326) {
722
+ res.barriers.geoJson = arcgisToGeoJSON(res.barriers);
723
+ }
724
+ if (res.polygonBarriers &&
725
+ res.polygonBarriers.spatialReference.wkid === 4326) {
726
+ res.polygonBarriers.geoJson = arcgisToGeoJSON(res.polygonBarriers);
727
+ }
728
+ if (res.polylineBarriers &&
729
+ res.polylineBarriers.spatialReference.wkid === 4326) {
730
+ res.polylineBarriers.geoJson = arcgisToGeoJSON(res.polylineBarriers);
731
+ }
732
+ return res;
733
+ }
734
+
735
+ export { ARCGIS_ONLINE_CLOSEST_FACILITY_URL, ARCGIS_ONLINE_ORIGIN_DESTINATION_MATRIX_URL, ARCGIS_ONLINE_ROUTING_URL, ARCGIS_ONLINE_SERVICE_AREA_URL, closestFacility, decompressGeometry, isFeatureSet, normalizeLocationsList, originDestinationMatrix, serviceArea, solveRoute };
736
+ //# sourceMappingURL=routing.esm.js.map