Skip to content

Commit

Permalink
Merge pull request #82 from GIScience/feature/add-snapping-api
Browse files Browse the repository at this point in the history
Use Snapping endpoint
  • Loading branch information
TheGreatRefrigerator authored Jul 5, 2024
2 parents e4cd5ac + 2839ae6 commit 0d235dc
Show file tree
Hide file tree
Showing 11 changed files with 226 additions and 7 deletions.
24 changes: 24 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ This library lets you consume the openrouteservice API in *JavaScript* applicati
- Geocoding | Reverse Geocoding | Structured Geocoding (powered by Pelias)
- Isochrones (accessibility)
- Time-distance matrix
- Snap (Get the closest point on road network)
- POIs (points of interest)
- Elevation (linestring or point)
- Optimization
Expand Down Expand Up @@ -243,6 +244,29 @@ try {
console.error(await err.response.json())
}
```

### Snap example

```js
// Add your api_key here
const Snap = new Openrouteservice.Snap({api_key: "XYZ"})

try {
let response = await Snap.calculate({
locations: [[8.681495,49.51461],[8.686507,49.41943]],
radius: 300,
profile: 'driving-car',
format: 'json'
})
// Add your own result handling here
console.log("response: ", response)

} catch (err) {
console.log("An error occurred: " + err.status)
console.error(await err.response.json())
}
```

### Optimization example
Or consume Optimization API to solve [vehicle routing problems](https://en.wikipedia.org/wiki/Vehicle_routing_problem)

Expand Down
2 changes: 1 addition & 1 deletion dist/ors-js-client.cjs

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion dist/ors-js-client.cjs.map

Large diffs are not rendered by default.

32 changes: 31 additions & 1 deletion dist/ors-js-client.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion dist/ors-js-client.js.map

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion dist/ors-js-client.umd.cjs

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion dist/ors-js-client.umd.cjs.map

Large diffs are not rendered by default.

51 changes: 51 additions & 0 deletions examples/snap.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
<!DOCTYPE html>
<html lang="en">
<head>
<title>ORS-js lib examples</title>
<script type="module" src="../dist/ors-js-client.js"></script>
</head>
<body>
<div>
<h1>ORS-js lib examples</h1>
<section>
<h2>Driving car snapping</h2>
<div id="driving-car-snap">
<script type='module'>
import apiKey from './apiKey.js'
window.onload = function() {
const node = document.getElementById("driving-car-snap")

const Snap = new Openrouteservice.Snap({
api_key: apiKey
})

Snap.calculate({
locations: [[8.690958, 49.404662], [8.687868, 49.390139]],
radius: 300,
profile: "driving-car",
format: "json",
api_version: 'v2',
})
.then(function(json) {
// Add your own result handling here
let response = JSON.stringify(json, null, "\t")
console.log(response);
response = response.replace(/(\n)/g, '<br>');
response = response.replace(/(\t)/g, '&nbsp;&nbsp;');
node.innerHTML = "<h3>Response</h3><p>" + response + "</p>";
})
.catch(function(err) {
let response = JSON.stringify(err, null, "\t")
console.error(response);
response = response.replace(/(\n)/g, '<br>');
response = response.replace(/(\t)/g, '&nbsp;&nbsp;');
node.innerHTML = "<h3>Error</h3><p>" + response + "</p>";
})
}
</script>
</div>

</section>
</div>
</body>
</html>
16 changes: 16 additions & 0 deletions src/OrsSnap.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import Constants from './constants.js'
import OrsBase from './OrsBase.js'

class OrsSnap extends OrsBase {
constructor(args) {
super(args);
if (!this.defaultArgs[Constants.propNames.service] && !this.requestArgs[Constants.propNames.service]) {
this.defaultArgs[Constants.propNames.service] = 'snap'
}
if (!args[Constants.propNames.apiVersion]) {
this.defaultArgs.api_version = Constants.defaultAPIVersion
}
}
}

export default OrsSnap
96 changes: 96 additions & 0 deletions src/__tests__/OrsSnap.cy.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
import OrsSnap from '../OrsSnap.js'

const key = Cypress.env('api_key')
const orsSnap = new OrsSnap({ 'api_key': key })

describe('Snap Test', () => {

context('construction', () => {
it('sets correct service and API version', () => {
expect(orsSnap.defaultArgs.service).to.equal('snap')
expect(orsSnap.defaultArgs.api_version).to.equal('v2')
})
})

context('methods', () => {
context('calculate', () => {
it('fails without parameters', (done) => {
orsSnap.calculate({})
.catch((err) => {
expect(err.status).to.equal(400)
expect(err.message).to.equal('Bad Request')
done()
})
})

it('generates a simple json snap', (done) => {
orsSnap.calculate({
locations: [[8.681495,49.51461],[8.686507,49.41943]],
radius: 300,
profile: 'driving-car',
format: 'json'
}).then((json) => {
expect(json['locations'].length).to.be.greaterThan(0)
expect(json['locations'][1].location).to.deep.equal([8.686507,49.41943])
expect(json['locations'][1].name).to.be.equal('Werderplatz')
expect(json['locations'][1].snapped_distance).exist
done()
})
})

it('snapping point not found for first location', (done) => {
orsSnap.calculate({
locations: [[0,0],[8.686507,49.41943]],
radius: 300,
profile: 'driving-car',
format: 'json'
}).then((json) => {
expect(json['locations'][0]).to.be.null
expect(json['locations'][1]).to.exist
done()
})
})

it('generates a simple geojson snap', (done) => {
orsSnap.calculate({
locations: [[8.681495,49.51461],[8.686507,49.41943]],
radius: 300,
profile: 'driving-car',
format: 'geojson'
}).then((json) => {
expect(json['features'].length).to.be.greaterThan(0)
expect(json['features'][0].properties.snapped_distance).exist
expect(json['features'][0].properties['source_id']).to.be.equal(0)
done()
})
})

it('snapping point not found for first location in geojson', (done) => {
orsSnap.calculate({
locations: [[0,0],[8.686507,49.41943]],
radius: 300,
profile: 'driving-car',
format: 'geojson'
}).then((geojson) => {
expect(geojson['features'].length).to.be.equal(1)
expect(geojson['features'][0].properties['source_id']).to.be.equal(1)
done()
})
})

it('sets customHeaders in request', () => {
orsSnap.calculate({
locations: [[8.681495,49.51461],[8.686507,49.41943]],
radius: 300,
profile: 'driving-car',
format: 'json',
customHeaders: {'Accept': 'application/json'}
})
cy.intercept('GET', 'https://api.openrouteservice.org/snap', (req) => {
expect(req.headers).to.include({'Accept': 'application/json'})
})
})

})
})
})
4 changes: 3 additions & 1 deletion src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import OrsDirections from './OrsDirections.js'
import OrsPois from './OrsPois.js'
import OrsElevation from './OrsElevation.js'
import OrsOptimization from './OrsOptimization.js'
import OrsSnap from './OrsSnap.js';

const Openrouteservice = {
Geocode: OrsGeocode,
Expand All @@ -13,7 +14,8 @@ const Openrouteservice = {
Matrix: OrsMatrix,
Pois: OrsPois,
Elevation: OrsElevation,
Optimization: OrsOptimization
Optimization: OrsOptimization,
Snap: OrsSnap
}

// Define Openrouteservice for Node module pattern loaders, including Browserify
Expand Down

0 comments on commit 0d235dc

Please sign in to comment.