From 6b2a77ce90923bdd80fabd3d47c4be07b0cd8137 Mon Sep 17 00:00:00 2001 From: Daramora Kaminari Date: Wed, 13 Mar 2019 10:17:14 +0300 Subject: [PATCH] feat(routing): Support elevation in routing Add support for elevation z in routing by allow a third number or z --- .../arcgis-rest-routing/src/solveRoute.ts | 17 +- .../test/solveRoute.test.ts | 242 ++++++++++++++++++ 2 files changed, 255 insertions(+), 4 deletions(-) diff --git a/packages/arcgis-rest-routing/src/solveRoute.ts b/packages/arcgis-rest-routing/src/solveRoute.ts index 232282183f..60682cb8c7 100644 --- a/packages/arcgis-rest-routing/src/solveRoute.ts +++ b/packages/arcgis-rest-routing/src/solveRoute.ts @@ -18,7 +18,9 @@ export interface ISolveRouteRequestOptions extends IEndpointRequestOptions { /** * Specify two or more locations between which the route is to be found. */ - stops: Array; + stops: Array< + IPoint | ILocation | [number, number] | [number, number, number] + >; } export interface ISolveRouteResponse { @@ -80,12 +82,19 @@ export function solveRoute( return coords.join(); } else if (isLocation(coords)) { if (coords.lat) { - return coords.long + "," + coords.lat; + return ( + coords.long + "," + coords.lat + (coords.z ? "," + coords.z : "") + ); } else { - return coords.longitude + "," + coords.latitude; + return ( + coords.longitude + + "," + + coords.latitude + + (coords.z ? "," + coords.z : "") + ); } } else { - return coords.x + "," + coords.y; + return coords.x + "," + coords.y + (coords.z ? "," + coords.z : ""); } }); options.params.stops = stops.join(";"); diff --git a/packages/arcgis-rest-routing/test/solveRoute.test.ts b/packages/arcgis-rest-routing/test/solveRoute.test.ts index cb5177ba47..3e1d551955 100644 --- a/packages/arcgis-rest-routing/test/solveRoute.test.ts +++ b/packages/arcgis-rest-routing/test/solveRoute.test.ts @@ -14,6 +14,12 @@ const stops: Array<[number, number]> = [ [-117.918976, 33.812092] ]; +// -117.195677,34.056383,10.11;-117.918976,33.812092,8.43 +const stops3: Array<[number, number, number]> = [ + [-117.195677, 34.056383, 10.11], + [-117.918976, 33.812092, 8.43] +]; + const stopsObjectsLatLong: ILocation[] = [ { lat: 34.056383, @@ -25,6 +31,19 @@ const stopsObjectsLatLong: ILocation[] = [ } ]; +const stopsObjectsLatLong3: ILocation[] = [ + { + lat: 34.056383, + long: -117.195677, + z: 10.11 + }, + { + lat: 33.812092, + long: -117.918976, + z: 8.43 + } +]; + const stopsObjectsLatitudeLongitude: ILocation[] = [ { latitude: 34.056383, @@ -36,6 +55,19 @@ const stopsObjectsLatitudeLongitude: ILocation[] = [ } ]; +const stopsObjectsLatitudeLongitude3: ILocation[] = [ + { + latitude: 34.056383, + longitude: -117.195677, + z: 10.11 + }, + { + latitude: 33.812092, + longitude: -117.918976, + z: 8.43 + } +]; + const stopsObjectsPoint: IPoint[] = [ { x: -117.195677, @@ -47,6 +79,19 @@ const stopsObjectsPoint: IPoint[] = [ } ]; +const stopsObjectsPoint3: IPoint[] = [ + { + x: -117.195677, + y: 34.056383, + z: 10.11 + }, + { + x: -117.918976, + y: 33.812092, + z: 8.43 + } +]; + // const customRoutingUrl = // "https://foo.com/ArcGIS/rest/services/Network/USA/NAServer/"; @@ -105,6 +150,43 @@ describe("solveRoute", () => { }); }); + it("should make a simple solveRoute request (array of 3d stops)", done => { + fetchMock.once("*", Solve); + + const MOCK_AUTH = { + getToken() { + return Promise.resolve("token"); + }, + portal: "https://mapsdev.arcgis.com" + }; + + solveRoute({ stops: stops3, authentication: MOCK_AUTH }) + .then(response => { + expect(fetchMock.called()).toEqual(true); + const [url, options]: [string, RequestInit] = fetchMock.lastCall("*"); + expect(url).toEqual( + "https://route.arcgis.com/arcgis/rest/services/World/Route/NAServer/Route_World/solve" + ); + expect(options.method).toBe("POST"); + expect(options.body).toContain("f=json"); + // "stops=-117.195677,34.056383,10.11;-117.918976,33.812092,8.43" + expect(options.body).toContain( + `stops=${encodeURIComponent( + "-117.195677,34.056383,10.11;-117.918976,33.812092,8.43" + )}` + ); + expect(options.body).toContain("token=token"); + expect(response.routes.spatialReference.latestWkid).toEqual(4326); + expect(response.routes.features[0].attributes.Name).toEqual( + "Location 1 - Location 2" + ); + done(); + }) + .catch(e => { + fail(e); + }); + }); + it("should make a simple solveRoute request (array of objects - lat/lon)", done => { fetchMock.once("*", Solve); @@ -145,6 +227,46 @@ describe("solveRoute", () => { }); }); + it("should make a simple solveRoute request (array of objects - 3d lat/lon)", done => { + fetchMock.once("*", Solve); + + const MOCK_AUTH = { + getToken() { + return Promise.resolve("token"); + }, + portal: "https://mapsdev.arcgis.com" + }; + + solveRoute({ + stops: stopsObjectsLatLong3, + authentication: MOCK_AUTH + }) + .then(response => { + expect(fetchMock.called()).toEqual(true); + const [url, options]: [string, RequestInit] = fetchMock.lastCall("*"); + expect(url).toEqual( + "https://route.arcgis.com/arcgis/rest/services/World/Route/NAServer/Route_World/solve" + ); + expect(options.method).toBe("POST"); + expect(options.body).toContain("f=json"); + // "stops=-117.195677,34.056383,10.11;-117.918976,33.812092,8.43" + expect(options.body).toContain( + `stops=${encodeURIComponent( + "-117.195677,34.056383,10.11;-117.918976,33.812092,8.43" + )}` + ); + expect(options.body).toContain("token=token"); + expect(response.routes.spatialReference.latestWkid).toEqual(4326); + expect(response.routes.features[0].attributes.Name).toEqual( + "Location 1 - Location 2" + ); + done(); + }) + .catch(e => { + fail(e); + }); + }); + it("should make a simple solveRoute request (array of objects - latitude/longitude)", done => { fetchMock.once("*", Solve); @@ -185,6 +307,46 @@ describe("solveRoute", () => { }); }); + it("should make a simple solveRoute request (array of objects - 3d latitude/longitude)", done => { + fetchMock.once("*", Solve); + + const MOCK_AUTH = { + getToken() { + return Promise.resolve("token"); + }, + portal: "https://mapsdev.arcgis.com" + }; + + solveRoute({ + stops: stopsObjectsLatitudeLongitude3, + authentication: MOCK_AUTH + }) + .then(response => { + expect(fetchMock.called()).toEqual(true); + const [url, options]: [string, RequestInit] = fetchMock.lastCall("*"); + expect(url).toEqual( + "https://route.arcgis.com/arcgis/rest/services/World/Route/NAServer/Route_World/solve" + ); + expect(options.method).toBe("POST"); + expect(options.body).toContain("f=json"); + // "stops=-117.195677,34.056383,10.11;-117.918976,33.812092,8.43" + expect(options.body).toContain( + `stops=${encodeURIComponent( + "-117.195677,34.056383,10.11;-117.918976,33.812092,8.43" + )}` + ); + expect(options.body).toContain("token=token"); + expect(response.routes.spatialReference.latestWkid).toEqual(4326); + expect(response.routes.features[0].attributes.Name).toEqual( + "Location 1 - Location 2" + ); + done(); + }) + .catch(e => { + fail(e); + }); + }); + it("should make a simple solveRoute request (array of objects - latitude/longitude)", done => { fetchMock.once("*", Solve); @@ -225,6 +387,46 @@ describe("solveRoute", () => { }); }); + it("should make a simple solveRoute request (array of objects - 3d latitude/longitude)", done => { + fetchMock.once("*", Solve); + + const MOCK_AUTH = { + getToken() { + return Promise.resolve("token"); + }, + portal: "https://mapsdev.arcgis.com" + }; + + solveRoute({ + stops: stopsObjectsLatitudeLongitude3, + authentication: MOCK_AUTH + }) + .then(response => { + expect(fetchMock.called()).toEqual(true); + const [url, options]: [string, RequestInit] = fetchMock.lastCall("*"); + expect(url).toEqual( + "https://route.arcgis.com/arcgis/rest/services/World/Route/NAServer/Route_World/solve" + ); + expect(options.method).toBe("POST"); + expect(options.body).toContain("f=json"); + // "stops=-117.195677,34.056383,10.11;-117.918976,33.812092,8.43" + expect(options.body).toContain( + `stops=${encodeURIComponent( + "-117.195677,34.056383,10.11;-117.918976,33.812092,8.43" + )}` + ); + expect(options.body).toContain("token=token"); + expect(response.routes.spatialReference.latestWkid).toEqual(4326); + expect(response.routes.features[0].attributes.Name).toEqual( + "Location 1 - Location 2" + ); + done(); + }) + .catch(e => { + fail(e); + }); + }); + it("should make a simple solveRoute request (array of IPoint)", done => { fetchMock.once("*", Solve); @@ -264,4 +466,44 @@ describe("solveRoute", () => { fail(e); }); }); + + it("should make a simple solveRoute request (array of 3d IPoint)", done => { + fetchMock.once("*", Solve); + + const MOCK_AUTH = { + getToken() { + return Promise.resolve("token"); + }, + portal: "https://mapsdev.arcgis.com" + }; + + solveRoute({ + stops: stopsObjectsPoint3, + authentication: MOCK_AUTH + }) + .then(response => { + expect(fetchMock.called()).toEqual(true); + const [url, options]: [string, RequestInit] = fetchMock.lastCall("*"); + expect(url).toEqual( + "https://route.arcgis.com/arcgis/rest/services/World/Route/NAServer/Route_World/solve" + ); + expect(options.method).toBe("POST"); + expect(options.body).toContain("f=json"); + // "stops=-117.195677,34.056383,10.11;-117.918976,33.812092,8.43" + expect(options.body).toContain( + `stops=${encodeURIComponent( + "-117.195677,34.056383,10.11;-117.918976,33.812092,8.43" + )}` + ); + expect(options.body).toContain("token=token"); + expect(response.routes.spatialReference.latestWkid).toEqual(4326); + expect(response.routes.features[0].attributes.Name).toEqual( + "Location 1 - Location 2" + ); + done(); + }) + .catch(e => { + fail(e); + }); + }); });