Connect To Local Instance of ORS

Hello, I am attempting to follow an example on the ORS website, but with my own self hosted instance of ORS on docker. The example I’m trying to follow is the Disaster Optimization guide.

All the code is essentially the same, except for this line:

ors_client = ors.Client(key='your_key')  # Get an API key from https://openrouteservice.org/dev/#/signup

Which I have changed to:

ors_client = ors.Client(base_url='http://localhost:8080/ors')

The output I receive is below:

{
	"name": "HTTPError",
	"message": "HTTP Error: 404",
	"stack": "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m\n\u001b[0;31mJSONDecodeError\u001b[0m                           Traceback (most recent call last)\nFile \u001b[0;32m/opt/homebrew/lib/python3.10/site-packages/requests/models.py:971\u001b[0m, in \u001b[0;36mResponse.json\u001b[0;34m(self, **kwargs)\u001b[0m\n\u001b[1;32m    970\u001b[0m \u001b[39mtry\u001b[39;00m:\n\u001b[0;32m--> 971\u001b[0m     \u001b[39mreturn\u001b[39;00m complexjson\u001b[39m.\u001b[39;49mloads(\u001b[39mself\u001b[39;49m\u001b[39m.\u001b[39;49mtext, \u001b[39m*\u001b[39;49m\u001b[39m*\u001b[39;49mkwargs)\n\u001b[1;32m    972\u001b[0m \u001b[39mexcept\u001b[39;00m JSONDecodeError \u001b[39mas\u001b[39;00m e:\n\u001b[1;32m    973\u001b[0m     \u001b[39m# Catch JSON-related errors and raise as requests.JSONDecodeError\u001b[39;00m\n\u001b[1;32m    974\u001b[0m     \u001b[39m# This aliases json.JSONDecodeError and simplejson.JSONDecodeError\u001b[39;00m\n\nFile \u001b[0;32m/opt/homebrew/Cellar/python@3.10/3.10.8/Frameworks/Python.framework/Versions/3.10/lib/python3.10/json/__init__.py:346\u001b[0m, in \u001b[0;36mloads\u001b[0;34m(s, cls, object_hook, parse_float, parse_int, parse_constant, object_pairs_hook, **kw)\u001b[0m\n\u001b[1;32m    343\u001b[0m \u001b[39mif\u001b[39;00m (\u001b[39mcls\u001b[39m \u001b[39mis\u001b[39;00m \u001b[39mNone\u001b[39;00m \u001b[39mand\u001b[39;00m object_hook \u001b[39mis\u001b[39;00m \u001b[39mNone\u001b[39;00m \u001b[39mand\u001b[39;00m\n\u001b[1;32m    344\u001b[0m         parse_int \u001b[39mis\u001b[39;00m \u001b[39mNone\u001b[39;00m \u001b[39mand\u001b[39;00m parse_float \u001b[39mis\u001b[39;00m \u001b[39mNone\u001b[39;00m \u001b[39mand\u001b[39;00m\n\u001b[1;32m    345\u001b[0m         parse_constant \u001b[39mis\u001b[39;00m \u001b[39mNone\u001b[39;00m \u001b[39mand\u001b[39;00m object_pairs_hook \u001b[39mis\u001b[39;00m \u001b[39mNone\u001b[39;00m \u001b[39mand\u001b[39;00m \u001b[39mnot\u001b[39;00m kw):\n\u001b[0;32m--> 346\u001b[0m     \u001b[39mreturn\u001b[39;00m _default_decoder\u001b[39m.\u001b[39;49mdecode(s)\n\u001b[1;32m    347\u001b[0m \u001b[39mif\u001b[39;00m \u001b[39mcls\u001b[39m \u001b[39mis\u001b[39;00m \u001b[39mNone\u001b[39;00m:\n\nFile \u001b[0;32m/opt/homebrew/Cellar/python@3.10/3.10.8/Frameworks/Python.framework/Versions/3.10/lib/python3.10/json/decoder.py:337\u001b[0m, in \u001b[0;36mJSONDecoder.decode\u001b[0;34m(self, s, _w)\u001b[0m\n\u001b[1;32m    333\u001b[0m \u001b[39m\"\"\"Return the Python representation of ``s`` (a ``str`` instance\u001b[39;00m\n\u001b[1;32m    334\u001b[0m \u001b[39mcontaining a JSON document).\u001b[39;00m\n\u001b[1;32m    335\u001b[0m \n\u001b[1;32m    336\u001b[0m \u001b[39m\"\"\"\u001b[39;00m\n\u001b[0;32m--> 337\u001b[0m obj, end \u001b[39m=\u001b[39m \u001b[39mself\u001b[39;49m\u001b[39m.\u001b[39;49mraw_decode(s, idx\u001b[39m=\u001b[39;49m_w(s, \u001b[39m0\u001b[39;49m)\u001b[39m.\u001b[39;49mend())\n\u001b[1;32m    338\u001b[0m end \u001b[39m=\u001b[39m _w(s, end)\u001b[39m.\u001b[39mend()\n\nFile \u001b[0;32m/opt/homebrew/Cellar/python@3.10/3.10.8/Frameworks/Python.framework/Versions/3.10/lib/python3.10/json/decoder.py:355\u001b[0m, in \u001b[0;36mJSONDecoder.raw_decode\u001b[0;34m(self, s, idx)\u001b[0m\n\u001b[1;32m    354\u001b[0m \u001b[39mexcept\u001b[39;00m \u001b[39mStopIteration\u001b[39;00m \u001b[39mas\u001b[39;00m err:\n\u001b[0;32m--> 355\u001b[0m     \u001b[39mraise\u001b[39;00m JSONDecodeError(\u001b[39m\"\u001b[39m\u001b[39mExpecting value\u001b[39m\u001b[39m\"\u001b[39m, s, err\u001b[39m.\u001b[39mvalue) \u001b[39mfrom\u001b[39;00m \u001b[39mNone\u001b[39m\n\u001b[1;32m    356\u001b[0m \u001b[39mreturn\u001b[39;00m obj, end\n\n\u001b[0;31mJSONDecodeError\u001b[0m: Expecting value: line 1 column 1 (char 0)\n\nDuring handling of the above exception, another exception occurred:\n\n\u001b[0;31mJSONDecodeError\u001b[0m                           Traceback (most recent call last)\nFile \u001b[0;32m/opt/homebrew/lib/python3.10/site-packages/openrouteservice/client.py:229\u001b[0m, in \u001b[0;36mClient._get_body\u001b[0;34m(response)\u001b[0m\n\u001b[1;32m    228\u001b[0m \u001b[39mtry\u001b[39;00m:\n\u001b[0;32m--> 229\u001b[0m     body \u001b[39m=\u001b[39m response\u001b[39m.\u001b[39;49mjson()\n\u001b[1;32m    230\u001b[0m \u001b[39mexcept\u001b[39;00m json\u001b[39m.\u001b[39mJSONDecodeError:\n\nFile \u001b[0;32m/opt/homebrew/lib/python3.10/site-packages/requests/models.py:975\u001b[0m, in \u001b[0;36mResponse.json\u001b[0;34m(self, **kwargs)\u001b[0m\n\u001b[1;32m    972\u001b[0m \u001b[39mexcept\u001b[39;00m JSONDecodeError \u001b[39mas\u001b[39;00m e:\n\u001b[1;32m    973\u001b[0m     \u001b[39m# Catch JSON-related errors and raise as requests.JSONDecodeError\u001b[39;00m\n\u001b[1;32m    974\u001b[0m     \u001b[39m# This aliases json.JSONDecodeError and simplejson.JSONDecodeError\u001b[39;00m\n\u001b[0;32m--> 975\u001b[0m     \u001b[39mraise\u001b[39;00m RequestsJSONDecodeError(e\u001b[39m.\u001b[39mmsg, e\u001b[39m.\u001b[39mdoc, e\u001b[39m.\u001b[39mpos)\n\n\u001b[0;31mJSONDecodeError\u001b[0m: Expecting value: line 1 column 1 (char 0)\n\nDuring handling of the above exception, another exception occurred:\n\n\u001b[0;31mHTTPError\u001b[0m                                 Traceback (most recent call last)\nCell \u001b[0;32mIn [34], line 80\u001b[0m\n\u001b[1;32m     78\u001b[0m \u001b[38;5;66;03m# Initialize a client and make the request\u001b[39;00m\n\u001b[1;32m     79\u001b[0m ors_client \u001b[38;5;241m=\u001b[39m ors\u001b[38;5;241m.\u001b[39mClient(base_url\u001b[38;5;241m=\u001b[39m\u001b[38;5;124m'\u001b[39m\u001b[38;5;124mhttp://localhost:8080/ors\u001b[39m\u001b[38;5;124m'\u001b[39m)\n\u001b[0;32m---> 80\u001b[0m result \u001b[38;5;241m=\u001b[39m \u001b[43mors_client\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43moptimization\u001b[49m\u001b[43m(\u001b[49m\n\u001b[1;32m     81\u001b[0m \u001b[43m    \u001b[49m\u001b[43mjobs\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[43mdeliveries\u001b[49m\u001b[43m,\u001b[49m\n\u001b[1;32m     82\u001b[0m \u001b[43m    \u001b[49m\u001b[43mvehicles\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[43mvehicles\u001b[49m\u001b[43m,\u001b[49m\n\u001b[1;32m     83\u001b[0m \u001b[43m    \u001b[49m\u001b[43mgeometry\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[38;5;28;43;01mTrue\u001b[39;49;00m\n\u001b[1;32m     84\u001b[0m \u001b[43m)\u001b[49m\n\u001b[1;32m     86\u001b[0m \u001b[38;5;66;03m# Add the output to the map\u001b[39;00m\n\u001b[1;32m     87\u001b[0m \u001b[38;5;28;01mfor\u001b[39;00m color, route \u001b[38;5;129;01min\u001b[39;00m \u001b[38;5;28mzip\u001b[39m([\u001b[38;5;124m'\u001b[39m\u001b[38;5;124mgreen\u001b[39m\u001b[38;5;124m'\u001b[39m, \u001b[38;5;124m'\u001b[39m\u001b[38;5;124mred\u001b[39m\u001b[38;5;124m'\u001b[39m, \u001b[38;5;124m'\u001b[39m\u001b[38;5;124mblue\u001b[39m\u001b[38;5;124m'\u001b[39m, \u001b[38;5;124m'\u001b[39m\u001b[38;5;124myellow\u001b[39m\u001b[38;5;124m'\u001b[39m, \u001b[38;5;124m'\u001b[39m\u001b[38;5;124mpurple\u001b[39m\u001b[38;5;124m'\u001b[39m], result[\u001b[38;5;124m'\u001b[39m\u001b[38;5;124mroutes\u001b[39m\u001b[38;5;124m'\u001b[39m]):\n\nFile \u001b[0;32m/opt/homebrew/lib/python3.10/site-packages/openrouteservice/client.py:299\u001b[0m, in \u001b[0;36m_make_api_method.<locals>.wrapper\u001b[0;34m(*args, **kwargs)\u001b[0m\n\u001b[1;32m    296\u001b[0m \u001b[39m@functools\u001b[39m\u001b[39m.\u001b[39mwraps(func)\n\u001b[1;32m    297\u001b[0m \u001b[39mdef\u001b[39;00m \u001b[39mwrapper\u001b[39m(\u001b[39m*\u001b[39margs, \u001b[39m*\u001b[39m\u001b[39m*\u001b[39mkwargs):\n\u001b[1;32m    298\u001b[0m     args[\u001b[39m0\u001b[39m]\u001b[39m.\u001b[39m_extra_params \u001b[39m=\u001b[39m kwargs\u001b[39m.\u001b[39mpop(\u001b[39m\"\u001b[39m\u001b[39mextra_params\u001b[39m\u001b[39m\"\u001b[39m, \u001b[39mNone\u001b[39;00m)\n\u001b[0;32m--> 299\u001b[0m     result \u001b[39m=\u001b[39m func(\u001b[39m*\u001b[39;49margs, \u001b[39m*\u001b[39;49m\u001b[39m*\u001b[39;49mkwargs)\n\u001b[1;32m    300\u001b[0m     \u001b[39mtry\u001b[39;00m:\n\u001b[1;32m    301\u001b[0m         \u001b[39mdel\u001b[39;00m args[\u001b[39m0\u001b[39m]\u001b[39m.\u001b[39m_extra_params\n\nFile \u001b[0;32m/opt/homebrew/lib/python3.10/site-packages/openrouteservice/optimization.py:97\u001b[0m, in \u001b[0;36moptimization\u001b[0;34m(client, jobs, vehicles, shipments, matrix, geometry, dry_run)\u001b[0m\n\u001b[1;32m     94\u001b[0m \u001b[39mif\u001b[39;00m matrix:\n\u001b[1;32m     95\u001b[0m     params[\u001b[39m'\u001b[39m\u001b[39mmatrix\u001b[39m\u001b[39m'\u001b[39m] \u001b[39m=\u001b[39m matrix\n\u001b[0;32m---> 97\u001b[0m \u001b[39mreturn\u001b[39;00m client\u001b[39m.\u001b[39;49mrequest(\u001b[39m\"\u001b[39;49m\u001b[39m/optimization\u001b[39;49m\u001b[39m\"\u001b[39;49m, {}, post_json\u001b[39m=\u001b[39;49mparams, dry_run\u001b[39m=\u001b[39;49mdry_run)\n\nFile \u001b[0;32m/opt/homebrew/lib/python3.10/site-packages/openrouteservice/client.py:204\u001b[0m, in \u001b[0;36mClient.request\u001b[0;34m(self, url, get_params, first_request_time, retry_counter, requests_kwargs, post_json, dry_run)\u001b[0m\n\u001b[1;32m    200\u001b[0m     \u001b[39mreturn\u001b[39;00m \u001b[39mself\u001b[39m\u001b[39m.\u001b[39mrequest(url, get_params, first_request_time,\n\u001b[1;32m    201\u001b[0m                         retry_counter \u001b[39m+\u001b[39m \u001b[39m1\u001b[39m, requests_kwargs, post_json)\n\u001b[1;32m    203\u001b[0m \u001b[39mtry\u001b[39;00m:\n\u001b[0;32m--> 204\u001b[0m     result \u001b[39m=\u001b[39m \u001b[39mself\u001b[39;49m\u001b[39m.\u001b[39;49m_get_body(response)\n\u001b[1;32m    206\u001b[0m     \u001b[39mreturn\u001b[39;00m result\n\u001b[1;32m    207\u001b[0m \u001b[39mexcept\u001b[39;00m exceptions\u001b[39m.\u001b[39m_RetriableRequest \u001b[39mas\u001b[39;00m e:\n\nFile \u001b[0;32m/opt/homebrew/lib/python3.10/site-packages/openrouteservice/client.py:231\u001b[0m, in \u001b[0;36mClient._get_body\u001b[0;34m(response)\u001b[0m\n\u001b[1;32m    229\u001b[0m     body \u001b[39m=\u001b[39m response\u001b[39m.\u001b[39mjson()\n\u001b[1;32m    230\u001b[0m \u001b[39mexcept\u001b[39;00m json\u001b[39m.\u001b[39mJSONDecodeError:\n\u001b[0;32m--> 231\u001b[0m     \u001b[39mraise\u001b[39;00m exceptions\u001b[39m.\u001b[39mHTTPError(response\u001b[39m.\u001b[39mstatus_code)\n\u001b[1;32m    233\u001b[0m \u001b[39m# error = body.get('error')\u001b[39;00m\n\u001b[1;32m    234\u001b[0m status_code \u001b[39m=\u001b[39m response\u001b[39m.\u001b[39mstatus_code\n\n\u001b[0;31mHTTPError\u001b[0m: HTTP Error: 404"
}

I am able to confirm that the instance is running correctly if I check ‘http://localhost:8080/ors/health’ and ‘http://localhost:8080/ors/status’, but I don’t know where to go from here.

Can anyone help please?

Hey,

your error message has a lot of what looks like unicode and ANSI escape sequences in it.
Could you reformat it to make it readable?

Are you sure the error is caused by the single line you reference?
If not, could you paste the rest of the code that is causing the error?

Best regards

Apologies, here it is:

---------------------------------------------------------------------------
JSONDecodeError                           Traceback (most recent call last)
File /opt/homebrew/lib/python3.10/site-packages/requests/models.py:971, in Response.json(self, **kwargs)
    970 try:
--> 971     return complexjson.loads(self.text, **kwargs)
    972 except JSONDecodeError as e:
    973     # Catch JSON-related errors and raise as requests.JSONDecodeError
    974     # This aliases json.JSONDecodeError and simplejson.JSONDecodeError

File /opt/homebrew/Cellar/python@3.10/3.10.8/Frameworks/Python.framework/Versions/3.10/lib/python3.10/json/__init__.py:346, in loads(s, cls, object_hook, parse_float, parse_int, parse_constant, object_pairs_hook, **kw)
    343 if (cls is None and object_hook is None and
    344         parse_int is None and parse_float is None and
    345         parse_constant is None and object_pairs_hook is None and not kw):
--> 346     return _default_decoder.decode(s)
    347 if cls is None:

File /opt/homebrew/Cellar/python@3.10/3.10.8/Frameworks/Python.framework/Versions/3.10/lib/python3.10/json/decoder.py:337, in JSONDecoder.decode(self, s, _w)
    333 """Return the Python representation of ``s`` (a ``str`` instance
    334 containing a JSON document).
    335 
    336 """
--> 337 obj, end = self.raw_decode(s, idx=_w(s, 0).end())
    338 end = _w(s, end).end()

File /opt/homebrew/Cellar/python@3.10/3.10.8/Frameworks/Python.framework/Versions/3.10/lib/python3.10/json/decoder.py:355, in JSONDecoder.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:

JSONDecodeError                           Traceback (most recent call last)
File /opt/homebrew/lib/python3.10/site-packages/openrouteservice/client.py:229, in Client._get_body(response)
    228 try:
--> 229     body = response.json()
    230 except json.JSONDecodeError:

File /opt/homebrew/lib/python3.10/site-packages/requests/models.py:975, in Response.json(self, **kwargs)
    972 except JSONDecodeError as e:
    973     # Catch JSON-related errors and raise as requests.JSONDecodeError
    974     # This aliases json.JSONDecodeError and simplejson.JSONDecodeError
--> 975     raise RequestsJSONDecodeError(e.msg, e.doc, e.pos)

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

During handling of the above exception, another exception occurred:

HTTPError                                 Traceback (most recent call last)
Cell In [1], line 80
     78 # Initialize a client and make the request
     79 ors_client = openrouteservice.Client(base_url='http://localhost:8080/ors')
---> 80 result = ors_client.optimization(
     81     jobs=deliveries,
     82     vehicles=vehicles,
     83     geometry=True
     84 )
     86 # Add the output to the map
     87 for color, route in zip(['green', 'red', 'blue'], result['routes']):

File /opt/homebrew/lib/python3.10/site-packages/openrouteservice/client.py:299, in _make_api_method.<locals>.wrapper(*args, **kwargs)
    296 @functools.wraps(func)
    297 def wrapper(*args, **kwargs):
    298     args[0]._extra_params = kwargs.pop("extra_params", None)
--> 299     result = func(*args, **kwargs)
    300     try:
    301         del args[0]._extra_params

File /opt/homebrew/lib/python3.10/site-packages/openrouteservice/optimization.py:97, in optimization(client, jobs, vehicles, shipments, matrix, geometry, dry_run)
     94 if matrix:
     95     params['matrix'] = matrix
---> 97 return client.request("/optimization", {}, post_json=params, dry_run=dry_run)

File /opt/homebrew/lib/python3.10/site-packages/openrouteservice/client.py:204, in Client.request(self, url, get_params, first_request_time, retry_counter, requests_kwargs, post_json, dry_run)
    200     return self.request(url, get_params, first_request_time,
    201                         retry_counter + 1, requests_kwargs, post_json)
    203 try:
--> 204     result = self._get_body(response)
    206     return result
    207 except exceptions._RetriableRequest as e:

File /opt/homebrew/lib/python3.10/site-packages/openrouteservice/client.py:231, in Client._get_body(response)
    229     body = response.json()
    230 except json.JSONDecodeError:
--> 231     raise exceptions.HTTPError(response.status_code)
    233 # error = body.get('error')
    234 status_code = response.status_code

HTTPError: HTTP Error: 404

I have also tried installing the server on a separate computer (vs localhost) and I am seeing the same error, except instead of localhost, it is the server’s IP.

I’m quite sure, given that’s the only line that I’ve changed, except for the CSV (I am linking directly to the example’s CSV on Github https://raw.githubusercontent.com/GIScience/openrouteservice-examples/master/resources/data/idai_health_sites.csv).

I am able to run the code perfectly fine when I enter my API key, but as soon as I change that one line and attempt to use my own server instead, that’s when it breaks and gives me this error message.

I’d appreciate any help you’re able to give me,
Thanks

Hey,

the error in question stems from this line:

Have a look at this thread (or similar ones on the forum) regarding the usage of the optimization endpoint with a local installation - you’ll have to set up vroom yourself, and run the request through your vroom instance connected to the ors instance.

Best regards