GIScience / openrouteservice-js

:pushpin: The JavaScript API to consume openrouteservice(s) painlessly!
https://openrouteservice.org
Apache License 2.0
192 stars 35 forks source link

Define service endpoint as a parameter, instead of hard coded #12

Closed amoncaldas closed 4 years ago

amoncaldas commented 5 years ago

As 'directions', 'isochrones' and 'matrix' are hard coded in the code, it is not possible to use an alternative service endpoint, like, for eg. 'pdirections', 'pisochrones' and 'pmatrix'.

A small change would improve the flexibility of the package. The suggestion is:

[OrsDirections.js] L 99: value: function calculate(reqArgs) [becames]: value: function calculate(reqArgs, service = 'directions') L 110: var requestSettings = orsUtil.prepareRequest(that.args, 'directions'); [becames]: var requestSettings = orsUtil.prepareRequest(that.args, service);

Apply the same approach to for ORSIsochones.js and OrsMatrix.js

TimMcCauley commented 5 years ago

@amoncaldas this is included in https://www.npmjs.com/package/openrouteservice-js/v/0.1.2 and can be set with the service parameter, e.g.

Directions.calculate({
  coordinates: [[8.690958, 49.404662], [8.687868, 49.390139]],
  profile: 'driving-hgv',
  service: 'pdirections',
  restrictions: { height: 10, weight: 5 },
  extra_info: ['waytype', 'steepness'],
  avoidables: ['highways', 'tollways', 'ferries', 'fords'],
  avoid_polygons: {
    type: 'Polygon',
    coordinates: [
      [
        [8.683533668518066, 49.41987949639816],
        [8.680272102355957, 49.41812070066643],
        [8.683919906616211, 49.4132348262363],
        [8.689756393432617, 49.41806486484901],
        [8.683533668518066, 49.41987949639816]
      ]
    ]
  },
  format: 'json',
  mime_type: 'application/json'
})
  .then(function(response) {
    console.log('response', JSON.stringify(response))
  })
  .catch(function(err) {
    var str = 'An error occured: ' + err
    console.log(str)
  })
amoncaldas commented 5 years ago

Thanks for the reply. So, at least in the client intended to be used with api version 2, it is not working properly.

In OrsDirections.js the request url is built using the following code:

var requestSettings = orsUtil.prepareRequest(that.args, 'directions'); // directions is hard coded
ar url = [requestSettings.meta.host, requestSettings.meta.apiVersion, requestSettings.meta.service, requestSettings.meta.profile, requestSettings.meta.format].join('/')

and in OrsUtil.js, the arg.service value is not used. Instead it is used the hard coded one passed in OrsDirections.js:


key: 'prepareRequest',
    value: function prepareRequest(args, service) {
      var request = {
        meta: {
          host: args.host,
          apiVersion: args.api_version,
          profile: args.profile,
          format: args.format,
          service: service, // here should be args.service || service
          apiKey: args.api_key,
          mimeType: args.mime_type
        }
      };
[...]
}```

A similar issue occurs in ORSIsochones.js and OrsMatrix.js
TimMcCauley commented 5 years ago

@amoncaldas are you sure? Look here:

https://github.com/GIScience/openrouteservice-js/blob/master/src/OrsDirections.js#L88

&

https://github.com/GIScience/openrouteservice-js/blob/master/src/OrsUtil.js#L58

This request is working for me:

Directions.calculate({
  coordinates: [[8.690958, 49.404662], [8.687868, 49.390139]],
  profile: 'driving-hgv',
  service: 'directions',
  restrictions: { height: 10, weight: 5 },
  extra_info: ['waytype', 'steepness'],
  avoidables: ['highways', 'tollways', 'ferries', 'fords'],
  avoid_polygons: {
    type: 'Polygon',
    coordinates: [
      [
        [8.683533668518066, 49.41987949639816],
        [8.680272102355957, 49.41812070066643],
        [8.683919906616211, 49.4132348262363],
        [8.689756393432617, 49.41806486484901],
        [8.683533668518066, 49.41987949639816]
      ]
    ]
  },
  format: 'json',
  mime_type: 'application/json'
})
  .then(function(response) {
    console.log('response', JSON.stringify(response))
  })
  .catch(function(err) {
    var str = 'An error occured: ' + err
    console.log(str)
  })