1

I am sending a request to OpenRouteService API from a set of client addresses to a foodbank site:

I am passing coordinates in the form ((x1,x2),(y1,y2)) where x coordinates are from a large csv file, and the y coordinate is the extra appended coordinate.

I sent a successful request (for a single coordinate pair) to openrouteservice in the form

cords = ((8.34234,48.23424),(8.34423,48.26424))

I am now trying to send a request for a large batch of coordinate pairs.

import csv
import openrouteservice
client = openrouteservice.Client(key='myAPIkey')

with open ('ClientCoordinates.csv' , 'r') as csv_coords:
   csv_reader = csv.reader(csv_coords) 
   EXTRA_COORD = [53.464774, 2.23179]
   csv_coords = []
   for line in csv_reader:
       csv_coords.append([line, EXTRA_COORD])

coords = (csv_coords)
routes = client.directions(coords)

print(routes)

This returns ApiError:

400 ({'error': {'code': 2002, 'message': "Parameter 'coordinates' has incorrect value or format."}, 'info': {'engine': {'version': '5.0.1', 'build_date': '2019-05-29T14:58:37Z'}, 'timestamp': 1567170534336}})

Perhaps my use of brackets is incorrect, as OpenRouteService requires coordinate inputs in ().

  • Please , format your code properly. Python is an indented language. Without proper indentation the code you provided should give you an Indent Error. – umbe1987 Aug 30 at 13:21
  • What is the exact format the API needs, and what are you passing as coords? Please, print it and include these info in your question by editing it. – umbe1987 Aug 30 at 13:22
  • 1
    I highly recommend removing your API key and locking it down if at all possible. Even if this is a free API, someone else using your key could limit the amount of requests you can make, and it's good practice for other APIs. – smiller Aug 30 at 13:33
  • Thanks for the replies, I have corrected the indentation and added coordinate info. I have also hidden my API key. – Stuey17 Aug 30 at 13:39
  • (Keep in mind that people can look back at the edits, so if there's a way to lock the API key down, that's still recommended.) – smiller Aug 30 at 13:41
1

I guess from this link that OpenRouteService requires a tuples of coordinates within a parent tuple like:

coords = ((8.34234,48.23424),(8.34423,48.26424))

You have to provide the API the exact format it needs otherwise it will complain.

Also, you are overwriting the csv_coords variable name, by defining it twice

I don't know how your 'ClientCoordinates.csv' looks like, but, as long as it is something like:

1,2
3,4

you can do:

import csv
import openrouteservice
client = openrouteservice.Client(key='YOUR_SECRET_KEY_HERE') # NEVER SHOW YOUR KEY IN PUBLIC WEBSITE!

with open ('ClientCoordinates.csv' , 'r') as csv_coords:
   csv_reader = csv.reader(csv_coords)
   EXTRA_COORD = (53.464774, 2.23179) # CHANGE THIS to a tuple
   new_csv_coords = []
   for line in csv_reader:
       new_csv_coords.append(((float(line[0]), float(line[1])), EXTRA_COORD))

        coords = (new_csv_coords)
        print(coords[0])
        routes = client.directions(coords[0])

        print(routes)
  • Unless the format changed considerably since this video was produced, coordinates are OK in lists instead of tuples. Either way, I don't see any option for multiple sets of coordinates to be passed at once except in route optimization (multiple stops). – smiller Aug 30 at 13:59
  • 1
    You're probably right, although as you also found out, it is hard to tell without any accessible API docs (at least, without the need to create an account). – umbe1987 Aug 30 at 14:15
  • Yes, it looks like I need to pass pairs of coordinates through one at a time, rather than all at once. – Stuey17 Aug 30 at 15:15
  • I tried this method and got this error - ApiError: 404 ({'error': {'code': 2010, 'message': 'Cannot find point 0: -2.262773,53.457321; Cannot find point 1: 2.23179,53.464774'}, 'info': {'engine': {'version': '5.0.1', 'build_date': '2019-05-29T14:22:56Z'}, 'timestamp': 1567179201693}}) – Stuey17 Aug 30 at 15:44
  • Did other coordinates worked or everything is giving you this issue? Seems the format is fine now, but that ORS cannot find the points. Maybe these links can help you finding out a solution:ask.openrouteservice.org/t/cannot-find-point-1-46-918-10-09555/…, ask.openrouteservice.org/t/… – umbe1987 Aug 30 at 15:58
1

The API's website has its documentation behind a login, so I could not get a written version of this, but based on the slide at 14:13 of this video https://www.youtube.com/watch?v=flJx1rFvccE the directions API needs two pairs of coordinates -- I assume the from and two coordinates. From the examples linked by @umbe1987 in the comments above, it's possible to send more than 2 coordinates, but those appear only in the section for Traveling Salesman (optimizing a route of multiple stops.)

Your code is passing an entire list of coordinates at once.

Suggestion: Adjust indentation and add another list to capture the routes.

import csv
import openrouteservice
client = openrouteservice.Client(key='myAPIkey')

with open ('ClientCoordinates.csv' , 'r') as csv_coords:
   csv_reader = csv.reader(csv_coords) 
   EXTRA_COORD = [53.464774, 2.23179]
   csv_coords = []
   routelist = []
   for line in csv_reader:
       csv_coords.append([line, EXTRA_COORD])

        coords = (csv_coords)
        routes = client.directions(coords)
        print(routes)
        routelist.append(routes)
  • Thanks for your help! – Stuey17 Aug 30 at 17:28

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.