Skip to content

Commit

Permalink
feat(routing): Support elevation in routing
Browse files Browse the repository at this point in the history
Add support for elevation z in routing by allow a third number or z
  • Loading branch information
DKaminari committed Mar 13, 2019
1 parent cc22727 commit 6b2a77c
Show file tree
Hide file tree
Showing 2 changed files with 255 additions and 4 deletions.
17 changes: 13 additions & 4 deletions packages/arcgis-rest-routing/src/solveRoute.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,9 @@ export interface ISolveRouteRequestOptions extends IEndpointRequestOptions {
/**
* Specify two or more locations between which the route is to be found.
*/
stops: Array<IPoint | ILocation | [number, number]>;
stops: Array<
IPoint | ILocation | [number, number] | [number, number, number]
>;
}

export interface ISolveRouteResponse {
Expand Down Expand Up @@ -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(";");
Expand Down
242 changes: 242 additions & 0 deletions packages/arcgis-rest-routing/test/solveRoute.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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,
Expand All @@ -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,
Expand All @@ -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/";

Expand Down Expand Up @@ -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);

Expand Down Expand Up @@ -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);

Expand Down Expand Up @@ -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);

Expand Down Expand Up @@ -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);

Expand Down Expand Up @@ -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);
});
});
});

0 comments on commit 6b2a77c

Please sign in to comment.