-
Notifications
You must be signed in to change notification settings - Fork 34
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #82 from GIScience/feature/add-snapping-api
Use Snapping endpoint
- Loading branch information
Showing
11 changed files
with
226 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Large diffs are not rendered by default.
Oops, something went wrong.
Large diffs are not rendered by default.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Large diffs are not rendered by default.
Oops, something went wrong.
Large diffs are not rendered by default.
Oops, something went wrong.
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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, ' '); | ||
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, ' '); | ||
node.innerHTML = "<h3>Error</h3><p>" + response + "</p>"; | ||
}) | ||
} | ||
</script> | ||
</div> | ||
|
||
</section> | ||
</div> | ||
</body> | ||
</html> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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'}) | ||
}) | ||
}) | ||
|
||
}) | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters