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

JSONDecodeError when requesting directions in gpx format #43

Closed
kuznetsov-a opened this issue Jun 3, 2020 · 5 comments · Fixed by #67
Closed

JSONDecodeError when requesting directions in gpx format #43

kuznetsov-a opened this issue Jun 3, 2020 · 5 comments · Fixed by #67
Assignees
Labels
enhancement New feature or request good first issue Good for newcomers

Comments

@kuznetsov-a
Copy link

Here's what I did

 [-0.0743414, 51.5015841],
 [-0.05273140000000039, 51.46430039999999],
 [-0.0531279, 51.4637547],
 [0.0320358, 51.3579627],
 [0.030053528946478628, 51.3510888399767],
 [0.0373899, 51.3508184],
 [0.05585272964791204, 51.33450105290082],
 [0.0528395, 51.3311335],
 [0.1014503, 51.246361],
 [0.14227140698558516, 51.18576100547525],
 [0.1458905, 51.1853518],
 [0.1459325, 51.1858536]]

gpx = directions.directions(clnt, coordinates_lon_lat, profile ='cycling-regular', 
                    format = 'gpx',preference = 'recommended' )

The same request is processed without any errors with format='json' or format='geojson' and the correct route is returned

dry_run output:

https://api.openrouteservice.org/v2/directions/driving-car/json?
Headers:
{
  "headers": {
    "User-Agent": "ORSClientPython.v2.3.0",
    "Content-type": "application/json",
    "Authorization": "5b3ce3597851110001cf62484bacb83d2107475198ea22650a25b872"
  },
  "timeout": 60,
  "json": {
    "coordinates": [
      [
        8.34234,
        48.23424
      ],
      [
        8.34423,
        48.26424
      ]
    ]
  }
}```
---
#### Here's what I got
<!-- we :heart: json outputs -->
```---------------------------------------------------------------------------
JSONDecodeError                           Traceback (most recent call last)
~/miniconda3/envs/ox/lib/python3.8/site-packages/openrouteservice/client.py in _get_body(response)
    228         try:
--> 229             body = response.json()
    230         except json.JSONDecodeError:

~/miniconda3/envs/ox/lib/python3.8/site-packages/requests/models.py in json(self, **kwargs)
    897                     pass
--> 898         return complexjson.loads(self.text, **kwargs)
    899 

~/miniconda3/envs/ox/lib/python3.8/json/__init__.py in loads(s, cls, object_hook, parse_float, parse_int, parse_constant, object_pairs_hook, **kw)
    356             parse_constant is None and object_pairs_hook is None and not kw):
--> 357         return _default_decoder.decode(s)
    358     if cls is None:

~/miniconda3/envs/ox/lib/python3.8/json/decoder.py in decode(self, s, _w)
    336         """
--> 337         obj, end = self.raw_decode(s, idx=_w(s, 0).end())
    338         end = _w(s, end).end()

~/miniconda3/envs/ox/lib/python3.8/json/decoder.py in raw_decode(self, s, idx)
    354         except StopIteration as err:
--> 355             raise JSONDecodeError("Expecting value", s, err.value) from None
    356         return obj, end

JSONDecodeError: Expecting value: line 1 column 1 (char 0)

During handling of the above exception, another exception occurred:

HTTPError                                 Traceback (most recent call last)
<ipython-input-129-0570940137d3> in <module>
----> 1 gpx = directions.directions(clnt, coordinates_lon_lat, profile ='cycling-regular', 
      2                     format = 'gpx',preference = 'recommended' )

~/miniconda3/envs/ox/lib/python3.8/site-packages/openrouteservice/directions.py in directions(client, coordinates, profile, format_out, format, preference, units, language, geometry, geometry_simplify, instructions, instructions_format, alternative_routes, roundabout_exits, attributes, maneuvers, radiuses, bearings, skip_segments, continue_straight, elevation, extra_info, suppress_warnings, optimized, optimize_waypoints, options, validate, dry_run)
    279         params['options'] = options
    280 
--> 281     return client.request("/v2/directions/" + profile + '/' + format, {}, post_json=params, dry_run=dry_run)
    282 
    283 

~/miniconda3/envs/ox/lib/python3.8/site-packages/openrouteservice/client.py in request(self, url, get_params, first_request_time, retry_counter, requests_kwargs, post_json, dry_run)
    202 
    203         try:
--> 204             result = self._get_body(response)
    205 
    206             return result

~/miniconda3/envs/ox/lib/python3.8/site-packages/openrouteservice/client.py in _get_body(response)
    229             body = response.json()
    230         except json.JSONDecodeError:
--> 231             raise exceptions.HTTPError(response.status_code)
    232 
    233         # error = body.get('error')

HTTPError: HTTP Error: 200

Here's what I was expecting

Same directions in GPX format

The same request generated as Python snippet from ORS API playground works correctly:


body = {"coordinates":[[-0.0858923,51.5050313],[-0.0743414,51.5015841],[-0.05273140000000039,51.46430039999999],[-0.0531279,51.4637547],[0.0320358,51.3579627],[0.030053528946478628,51.3510888399767],[0.0373899,51.3508184],[0.05585272964791204,51.33450105290082],[0.0528395,51.3311335],[0.1014503,51.246361],[0.14227140698558516,51.18576100547525],[0.1458905,51.1853518],[0.1459325,51.1858536]],"preference":"recommended"}

headers = {
    'Accept': 'application/json, application/geo+json, application/gpx+xml, img/png; charset=utf-8',
    'Authorization': '5b3ce3597851110001cf62484bacb83d2107475198ea22650a25b872',
    'Content-Type': 'application/json; charset=utf-8'
}
call = requests.post('https://api.openrouteservice.org/v2/directions/cycling-regular/gpx', json=body, headers=headers)

print(call.status_code, call.reason)
print(call.text)

Here's what I think could be improved

API response in GPX format should be correctly parsed or delivered as is

@nilsnolde
Copy link
Contributor

Raw requests will work (as in the playground). However, ors-py offers some level of abstraction and I'm fairly sure this won't be implemented any time soon (if ever), sorry. It's actually the first time this was raised in > 2 years, so (luckily) not a lot of people have a use case for GPX in Python.

Of course happy to review a PR if you wanna add it upstream. The gpxpy package could be an option, seems to be well maintained I wouldn't like the maintenance of GPX parsing in this repo.

@nilsnolde nilsnolde added the help wanted Extra attention is needed label Jun 3, 2020
@nilsnolde
Copy link
Contributor

I just realized that the doc string for directions.directions mentions gpx. If you're not interested in a PR, I'll change that and throw an appropriate error when it's tried.

@kuznetsov-a
Copy link
Author

Understood, if gpx is actually not supported, it should not be available as a format option

@nilsnolde
Copy link
Contributor

Or it could just return the gpx response as plain text, so you can parse it in the client. I also maintain the routing-py package where we actually parse the JSON response, forgot for a moment that ors-py only gives the loaded JSON back anyways without parsing. So it could do the same for GPX, that's true. Would that be enough?

@kuznetsov-a
Copy link
Author

Plain text response would be great, thanks. My use case for gpx is to save the route to a file that I can load into a navigation program. It would be great to be able to do it without resorting to raw api calls

@nilsnolde nilsnolde added enhancement New feature or request good first issue Good for newcomers and removed help wanted Extra attention is needed labels Jun 4, 2020
@nilsnolde nilsnolde self-assigned this Jun 4, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request good first issue Good for newcomers
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants