Is there a way to exclude a rectangle (or any other shape) area from the routing?

I searched the forum, but can’t seem to find a similar issue. Is there a way to exclude certain areas from the routing process, for example a traffic congested area, or i have to make it on my own? I think it would be a really helpful feature.

Thanks in advance

Hey,

the openrouteservice API has this feature - unfortunately it is a bit hidden :frowning:
Have a look at the API-docs for directions, under optionsavoid_polygons.

It’s also available in the python, js and r-sdk and in the Advanced Directions-Tab in the QGIS Plugin, and will come to the processing plugins soon.

Best regards

1 Like

got it. Thank you very much!

1 Like

If possible, could you inform me how i can implement the avoid_polygons in python, as i can’t seem to find the proper documentation.

routes = client.directions(coords, profile=‘foot-walking’,???options=avoid_polygons???)

Thank you in advance

Hi @JerryPapada,

take a look at this example using avoid_polygons.
Both GeoJSON Polygons and MultiPolygons are supported and has to be passed in the options parameter like:

...,options={
    "avoid_polygons": {
      "type": "Polygon",
      "coordinates": [
...
      ]
    }

There are some restrictions to them though:

Best regards

Hello, and thank you for the quick response.

I am trying to implement this, but keep getting this error message: openrouteservice.exceptions.ApiError: 400 ({'error': {'code': 2000, 'message': "Parameter 'avoid_polygons' has incorrect value or format."}, 'info': {'engine': {'version': '6.6.1', 'build_date': '2021-07-05T10:57:48Z'}, 'timestamp': 1636461297917}})

My code is quite simple, could you take a look?

import openrouteservice as ors
import folium



coords = ((x, y), (x1, y1))

client = ors.Client(key='blah') # Specify your personal API key
routes = client.directions(coords, profile='foot-walking',options= {
    "avoid_polygons": {
      "type": "Polygon",
      "coordinates": [
      	[x2, y2],[x3, y3],[x4, y4],[x5, y5]
      ]}})

print(routes)

Thank you in advance

{
    "type": "Polygon", 
    "coordinates": [
        [[30.0, 10.0], [40.0, 40.0], [20.0, 40.0], [10.0, 20.0], [30.0, 10.0]]
    ]
}

That’s a valid polygon. Your’s is missing some brackets

1 Like

Does this error indicate an error in my data or in the syntax of the code? :
openrouteservice.exceptions.ApiError: 400 ({'error': {'code': 2000, 'message': "Parameter 'avoid_polygons' has incorrect value or format."}, 'info': {'engine': {'version': '6.6.1', 'build_date': '2021-07-05T10:5

routes = client.directions(coords, profile='foot-walking',options= {
	"avoid_polygons": {
    "type": "Polygon", 
    "coordinates": [
      	[[x1, y1],[x2, y2],[x3, y3],[x4, y4]]
      ]}})

Thanks again for the quick response

As the error states, your parameter avoid_polygons has incorrect format, thus it is an error in your code.