Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

added routing module #382

Merged
merged 4 commits into from
Nov 14, 2018
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
206 changes: 103 additions & 103 deletions package-lock.json

Large diffs are not rendered by default.

73 changes: 73 additions & 0 deletions packages/arcgis-rest-routing/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
[![npm version][npm-img]][npm-url]
[![build status][travis-img]][travis-url]
[![Coverage Status][coverage-img]][coverage-url]
[![apache licensed](https://img.shields.io/badge/license-Apache-green.svg?style=flat-square)](https://raw.githubusercontent.com/Esri/arcgis-rest-js/master/LICENSE)

[npm-img]: https://img.shields.io/npm/v/@esri/arcgis-rest-routing.svg?style=flat-square
[npm-url]: https://www.npmjs.com/package/@esri/arcgis-rest-routing
[travis-img]: https://img.shields.io/travis/Esri/arcgis-rest-js/master.svg?style=flat-square
[travis-url]: https://travis-ci.org/Esri/arcgis-rest-js
[coverage-img]: https://coveralls.io/repos/github/Esri/arcgis-rest-js/badge.svg
[coverage-url]: https://coveralls.io/github/Esri/arcgis-rest-js

# @esri/arcgis-rest-routing

> Routing helpers for [`@esri/arcgis-rest-request`](https://github.com/Esri/arcgis-rest-js).

### Example

```bash
npm install @esri/arcgis-rest-common-types
npm install @esri/arcgis-rest-request
npm install @esri/arcgis-rest-routing
```

```js
solveRoute({
stops: [
[-117.195677, 34.056383],
[-117.918976, 33.812092],
],
authentication
})
.then(response)
// {routes: {features: [{attributes: { ... }, geometry:{ ... }}]}}
```

### [API Reference](https://esri.github.io/arcgis-rest-js/api/routing/)

* [`solveRoute({stops: [...], authentication})`](https://esri.github.io/arcgis-rest-js/api/routing/solveRoute/)

### Issues

If something isn't working the way you expected, please take a look at [previously logged issues](https://github.com/Esri/arcgis-rest-js/issues) first. Have you found a new bug? Want to request a new feature? We'd [**love**](https://github.com/Esri/arcgis-rest-js/issues/new) to hear from you.

If you're looking for help you can also post issues on [GIS Stackexchange](http://gis.stackexchange.com/questions/ask?tags=esri-oss).

### Versioning

For transparency into the release cycle and in striving to maintain backward compatibility, @esri/arcgis-rest-js is maintained under Semantic Versioning guidelines and will adhere to these rules whenever possible.

For more information on SemVer, please visit <http://semver.org/>.

### Contributing

Esri welcomes contributions from anyone and everyone. Please see our [guidelines for contributing](CONTRIBUTING.md).

### License

Copyright &copy; 2018 Esri

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

> http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.

A copy of the license is available in the repository's [LICENSE](../../LICENSE) file.
54 changes: 54 additions & 0 deletions packages/arcgis-rest-routing/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
{
"name": "@esri/arcgis-rest-routing",
"version": "1.13.2",
"description": "Routing helpers for @esri/arcgis-rest-request",
"main": "dist/node/index.js",
"browser": "dist/umd/routing.umd.js",
"module": "dist/esm/index.js",
"js:next": "dist/esm/index.js",
"types": "dist/esm/index.d.ts",
"license": "Apache-2.0",
"files": [
"dist/**"
],
"dependencies": {
"tslib": "^1.7.1"
},
"devDependencies": {
"@esri/arcgis-rest-auth": "^1.13.1",
"@esri/arcgis-rest-common-types": "^1.13.1",
"@esri/arcgis-rest-request": "^1.13.1"
},
"peerDependencies": {
"@esri/arcgis-rest-auth": "^1.13.1",
"@esri/arcgis-rest-common-types": "^1.13.1",
"@esri/arcgis-rest-request": "^1.13.1"
},
"scripts": {
"prepare": "npm run build",
"build": "npm run build:node && npm run build:umd && npm run build:esm",
"build:esm": "tsc -p ./tsconfig.json --module es2015 --outDir ./dist/esm --declaration",
"build:umd": "rollup -c ../../umd-base-profile.js && rollup -c ../../umd-production-profile.js",
"build:node": "tsc -p ./tsconfig.json --module commonjs --outDir ./dist/node",
"dev:esm": "tsc -w --module es2015 --outDir ./dist/esm --declaration",
"dev:umd": "rollup -w -c ../../umd-base-profile.js",
"dev:node": "tsc -w --module commonjs --outDir ./dist/node"
},
"publishConfig": {
"access": "public"
},
"repository": {
"type": "git",
"url": "git+https://github.com/Esri/arcgis-rest-js.git"
},
"contributors": [
{
"name": "Gavin Rehkemper",
"email": "gavin@gavinr.com"
}
],
"bugs": {
"url": "https://github.com/Esri/arcgis-rest-js/issues"
},
"homepage": "https://github.com/Esri/arcgis-rest-js#readme"
}
23 changes: 23 additions & 0 deletions packages/arcgis-rest-routing/src/helpers.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
/* Copyright (c) 2018 Environmental Systems Research Institute, Inc.
* Apache-2.0 */

import { IRequestOptions } from "@esri/arcgis-rest-request";

// https always
export const worldRoutingService =
"https://route.arcgis.com/arcgis/rest/services/World/Route/NAServer/Route_World/";

export interface ILocation {
latitude?: number;
longitude?: number;
lat?: number;
long?: number;
}

// nice to have: verify custom endpoints contain 'NAServer' and end in a '/'
export interface IEndpointRequestOptions extends IRequestOptions {
/**
* Any ArcGIS Routing service (example: https://sampleserver3.arcgisonline.com/ArcGIS/rest/services/Network/USA/NAServer/Route/ ) to use for the routing service request.
*/
endpoint?: string;
}
4 changes: 4 additions & 0 deletions packages/arcgis-rest-routing/src/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
/* Copyright (c) 2018 Environmental Systems Research Institute, Inc.
* Apache-2.0 */

export * from "./solveRoute";
124 changes: 124 additions & 0 deletions packages/arcgis-rest-routing/src/solveRoute.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,124 @@
/* Copyright (c) 2018 Environmental Systems Research Institute, Inc.
* Apache-2.0 */

import { request } from "@esri/arcgis-rest-request";

import {
ISpatialReference,
IPoint,
IFeature
} from "@esri/arcgis-rest-common-types";

import {
worldRoutingService,
ILocation,
IEndpointRequestOptions
} from "./helpers";

export interface ISolveRouteRequestOptions extends IEndpointRequestOptions {
/**
* Specify two or more locations between which the route is to be found.
*/
stops: Array<IPoint | ILocation | [number, number]>;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

you can see an example of how to include inline TSDoc for an interface property like stops here:

/**
* Any ArcGIS Geocoding service (example: http://sampleserver6.arcgisonline.com/arcgis/rest/services/Locators/SanDiego/GeocodeServer )
*/
endpoint?: string;

}

export interface ISolveRouteResponse {
messages: string[];
checksum: string;
routes: {
fieldAliases: object;
geometryType: string;
spatialReference: ISpatialReference;
features: IFeature[];
};
directions?: Array<{
routeId: number;
routeName: string;
summary: object;
features: IFeature[];
}>;
}

function isLocationArray(
coords: ILocation | IPoint | [number, number]
): coords is [number, number] {
return (coords as [number, number]).length === 2;
}

function isLocation(
coords: ILocation | IPoint | [number, number]
): coords is ILocation {
return (
(coords as ILocation).latitude !== undefined ||
(coords as ILocation).lat !== undefined
);
}

/**
* 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.
*
* ```js
* import { solveRoute } from '@esri/arcgis-rest-routing';
* import { ApplicationSession } from '@esri/arcgis-rest-auth';
*
* solveRoute({
* stops: [
* {
* latitude: 34.056383,
* longitude: -117.195677
* },
* {
* latitude: 33.812092,
* longitude: -117.918976
* }
* ],
* authentication: session
* })
* .then((response) => {
* response.routes.features; // => {routes: {features: [{attributes: { ... }, geometry:{ ... }}]}}
* });
* ```
*
* @param requestOptions Options to pass through to the routing service.
* @returns A Promise that will resolve with routes and directions for the request.
*/
export function solveRoute(
requestOptions: ISolveRouteRequestOptions
): Promise<ISolveRouteResponse> {
const options: ISolveRouteRequestOptions = {
endpoint: requestOptions.endpoint || worldRoutingService,
params: {},
...requestOptions
};

// the SAAS service does not support anonymous requests
if (
!requestOptions.authentication &&
options.endpoint === worldRoutingService
) {
return Promise.reject(
"Routing using the ArcGIS service requires authentication"
);
}

const stops: string[] = requestOptions.stops.map(coords => {
if (isLocationArray(coords)) {
return coords.join();
} else if (isLocation(coords)) {
if (coords.lat) {
return coords.long + "," + coords.lat;
} else {
return coords.longitude + "," + coords.latitude;
}
} else {
return coords.y + "," + coords.x;
}
});
options.params.stops = stops.join(";");

return request(options.endpoint + "solve", options);
}

export default {
solveRoute
};
Loading