1 start point and 3 different endpoints, how to get these multiple distances and durations?

Hello!

I’ve got 1 start coordinate (city downtown) and 3 different end coordinates. I’d like to know if it is possible to iterate the ors_client.directions over the 3 end coordinates mantaining the start coordinate? The 3 end coordinate are in a .xlsx file with the location’s Name, Longitude and Latitude.

My idea was to make something like this:

path = ‘…/Data/OSR/Test.xlsx’
test = pd.read_excel(path)

ors_client = ors.Client(key=‘…abc123…’)

start = [-47.024, -23.623]
end = [test.Lon, test.Lat]

coordinates = [(start), (end)]

route = ors_client.directions(coordinates=coordinates,
profile=‘driving-car’,
format=‘geojson’)

I’m pretty new to Python and coding, so i keep getting a few error messsages… (I guess it’s because the way i’m trying iterate over the end coordinates). Should i use a “for” to iterate? Or declare a function “def” to make the request for the directions to the endpoints in the .xlsx?

Hi @fspamb99,

there are always multiple ways to the goal. you either need to explicitly call ors_client.directions for every start-end-pair, or loop over an array of end coordinates and do the calculations in iterations.

As test.Lon should represent all the column values, you need to do something similar to this:

start = ...

for index, value in test.Lon.items():
  coordinates = [(start), ([test.Lon[i], test.Lat[i])]
  route = ors_client.directions(coordinates=coordinates, profile='driving-car', format='geojson')
  # do something with routes
  ...

Of course you can always define functions for separate tasks.

For python questions, please use e.g. stackoverflow or something similar, as this forum is for openrouteservice related questions.

Best regards

1 Like

Thank you so much @amandus !

1 Like

Hello @amandus !

I’ve followed your instructions and accomplished to make the loop for each route, thanks again for the help!

Now I want to export each of these routes as a .GeoJSON file to open it at QGIS, any ideas of how i can do it?

I know it’s not the purpose of ORS FAQ to answer questions like these… Sorry. But I couldn’t find anything similar to this and would appreciate any kind of help.

Again, thank you very much for the help and attention!

Writing python files is not really an ors question…

also you could just use our ORS Tools Plugin to generate the routes in QGIS in the first place :sweat_smile:

Best regards