def osrm_query(self, list_points, tries=0, max_tries=5): try: client = ORS(base_url=self.base_url) response = client.directions(list_points, 'driving-car', format='geojson', instructions=False,) response_dict = {'coordinates': response.geometry, 'type': 'LineString'} route_line = shape(response_dict) except: tries += 1 if tries > max_tries: route_line = self.route_from_nodes(list_points) else: route_line = self.osrm_query(list_points, tries) return route_line
def setUp(self): self.key = "sample_key" self.client = ORS(api_key=self.key)
class ORSTest(_test.TestCase): name = "ors" def setUp(self): self.key = "sample_key" self.client = ORS(api_key=self.key) @responses.activate def test_directions_json(self): query = deepcopy(ENDPOINTS_QUERIES[self.name]["directions"]) responses.add( responses.POST, "https://api.openrouteservice.org/v2/directions/{}/json".format( query["profile"]), status=200, json=ENDPOINTS_RESPONSES[self.name]["directions"]["json"], content_type="application/json", ) routes = self.client.directions(**query, format="json") query["coordinates"] = query["locations"] del query["locations"] del query["profile"] self.assertEqual(1, len(responses.calls)) self.assertEqual( query, json.loads(responses.calls[0].request.body.decode("utf-8"))) self.assertIsInstance(routes, Direction) self.assertIsInstance(routes.geometry, list) self.assertIsInstance(routes.duration, int) self.assertIsInstance(routes.distance, int) self.assertIsInstance(routes.raw, dict) @responses.activate def test_directions_geojson(self): query = deepcopy(ENDPOINTS_QUERIES[self.name]["directions"]) responses.add( responses.POST, "https://api.openrouteservice.org/v2/directions/{}/geojson".format( query["profile"]), status=200, json=ENDPOINTS_RESPONSES[self.name]["directions"]["geojson"], content_type="application/json", ) routes = self.client.directions(**query, format="geojson") query["coordinates"] = query["locations"] del query["locations"] del query["profile"] self.assertEqual(1, len(responses.calls)) self.assertEqual( query, json.loads(responses.calls[0].request.body.decode("utf-8"))) self.assertIsInstance(routes, Direction) self.assertIsInstance(routes.geometry, list) self.assertIsInstance(routes.duration, int) self.assertIsInstance(routes.distance, int) self.assertIsInstance(routes.raw, dict) @responses.activate def test_full_isochrones(self): query = deepcopy(ENDPOINTS_QUERIES[self.name]["isochrones"]) responses.add( responses.POST, "https://api.openrouteservice.org/v2/isochrones/{}/geojson".format( query["profile"]), status=200, json=ENDPOINTS_RESPONSES[self.name]["isochrones"], content_type="application/json", ) isochrones = self.client.isochrones(**query) expected = query expected["locations"] = [expected["locations"]] expected["range"] = expected["intervals"] expected["range_type"] = expected["interval_type"] del expected["intervals"] del expected["interval_type"] self.assertEqual(1, len(responses.calls)) self.assertEqual( expected, json.loads(responses.calls[0].request.body.decode("utf-8"))) self.assertIsInstance(isochrones, Isochrones) self.assertEqual(4, len(isochrones)) self.assertIsInstance(isochrones.raw, dict) for iso in isochrones: self.assertIsInstance(iso, Isochrone) self.assertIsInstance(iso.geometry, list) self.assertIsInstance(iso.center, list) self.assertIsInstance(iso.interval, int) @responses.activate def test_full_matrix(self): query = deepcopy(ENDPOINTS_QUERIES[self.name]["matrix"]) responses.add( responses.POST, "https://api.openrouteservice.org/v2/matrix/{}/json".format( query["profile"]), status=200, json=ENDPOINTS_RESPONSES[self.name]["matrix"], content_type="application/json", ) matrix = self.client.matrix(**query) expected = query self.assertEqual(1, len(responses.calls)) self.assertEqual( expected, json.loads(responses.calls[0].request.body.decode("utf-8"))) self.assertIsInstance(matrix, Matrix) self.assertIsInstance(matrix.durations, list) self.assertIsInstance(matrix.distances, list) self.assertIsInstance(matrix.raw, dict) @responses.activate def test_key_in_header(self): # Test that API key is being put in the Authorization header query = ENDPOINTS_QUERIES["ors"]["directions"] responses.add( responses.POST, "https://api.openrouteservice.org/v2/directions/{}/geojson".format( query["profile"]), json=ENDPOINTS_RESPONSES[self.name]["directions"]["geojson"], status=200, content_type="application/json", ) self.client.directions(**query) self.assertDictContainsSubset({"Authorization": self.key}, responses.calls[0].request.headers) @responses.activate def test_alternative_routes_error(self): # Test that alternative route works and also throws right errors query = deepcopy(ENDPOINTS_QUERIES["ors"]["directions"]) query["alternative_routes"] = { "target_count": 0, "a": 0, "weight_factor": 0 } responses.add( responses.POST, "https://api.openrouteservice.org/v2/directions/{}/geojson".format( query["profile"]), json=ENDPOINTS_RESPONSES[self.name]["directions"]["geojson"], status=200, content_type="application/json", ) with self.assertRaises(ValueError): self.client.directions(**query) query["alternative_routes"] = [0, 1, 2, 3] with self.assertRaises(TypeError): self.client.directions(**query)
def setUp(self): self.key = 'sample_key' self.client = ORS(api_key=self.key)
class ORSTest(_test.TestCase): name = 'ors' def setUp(self): self.key = 'sample_key' self.client = ORS(api_key=self.key) @responses.activate def test_directions_json(self): query = deepcopy(ENDPOINTS_QUERIES[self.name]['directions']) responses.add( responses.POST, 'https://api.openrouteservice.org/v2/directions/{}/json'.format(query['profile']), status=200, json=ENDPOINTS_RESPONSES[self.name]['directions']['json'], content_type='application/json' ) routes = self.client.directions(**query, format='json') query['coordinates'] = query['locations'] del query['locations'] del query['profile'] self.assertEqual(1, len(responses.calls)) self.assertEqual(query, json.loads(responses.calls[0].request.body.decode('utf-8'))) self.assertIsInstance(routes, Direction) self.assertIsInstance(routes.geometry, list) self.assertIsInstance(routes.duration, int) self.assertIsInstance(routes.distance, int) self.assertIsInstance(routes.raw, dict) @responses.activate def test_directions_geojson(self): query = deepcopy(ENDPOINTS_QUERIES[self.name]['directions']) responses.add( responses.POST, 'https://api.openrouteservice.org/v2/directions/{}/geojson'.format(query['profile']), status=200, json=ENDPOINTS_RESPONSES[self.name]['directions']['geojson'], content_type='application/json' ) routes = self.client.directions(**query, format='geojson') query['coordinates'] = query['locations'] del query['locations'] del query['profile'] self.assertEqual(1, len(responses.calls)) self.assertEqual(query, json.loads(responses.calls[0].request.body.decode('utf-8'))) self.assertIsInstance(routes, Direction) self.assertIsInstance(routes.geometry, list) self.assertIsInstance(routes.duration, int) self.assertIsInstance(routes.distance, int) self.assertIsInstance(routes.raw, dict) @responses.activate def test_full_isochrones(self): query = deepcopy(ENDPOINTS_QUERIES[self.name]['isochrones']) responses.add( responses.POST, 'https://api.openrouteservice.org/v2/isochrones/{}/geojson'.format(query['profile']), status=200, json=ENDPOINTS_RESPONSES[self.name]['isochrones'], content_type='application/json' ) isochrones = self.client.isochrones(**query) expected = query expected['locations'] = [expected['locations']] expected['range'] = expected['intervals'] expected['range_type'] = expected['interval_type'] del expected['intervals'] del expected['interval_type'] self.assertEqual(1, len(responses.calls)) self.assertEqual(expected, json.loads(responses.calls[0].request.body.decode('utf-8'))) self.assertIsInstance(isochrones, Isochrones) self.assertEqual(4, len(isochrones)) self.assertIsInstance(isochrones.raw, dict) for iso in isochrones: self.assertIsInstance(iso, Isochrone) self.assertIsInstance(iso.geometry, list) self.assertIsInstance(iso.center, list) self.assertIsInstance(iso.interval, int) @responses.activate def test_full_matrix(self): query = deepcopy(ENDPOINTS_QUERIES[self.name]['matrix']) responses.add( responses.POST, 'https://api.openrouteservice.org/v2/matrix/{}/json'.format(query['profile']), status=200, json=ENDPOINTS_RESPONSES[self.name]['matrix'], content_type='application/json' ) matrix = self.client.matrix(**query) expected = query self.assertEqual(1, len(responses.calls)) self.assertEqual(expected, json.loads(responses.calls[0].request.body.decode('utf-8'))) self.assertIsInstance(matrix, Matrix) self.assertIsInstance(matrix.durations, list) self.assertIsInstance(matrix.distances, list) self.assertIsInstance(matrix.raw, dict) @responses.activate def test_key_in_header(self): # Test that API key is being put in the Authorization header query = ENDPOINTS_QUERIES['ors']['directions'] responses.add( responses.POST, 'https://api.openrouteservice.org/v2/directions/{}/geojson'.format(query['profile']), json=ENDPOINTS_RESPONSES[self.name]['directions']['geojson'], status=200, content_type='application/json' ) resp = self.client.directions(**query) self.assertDictContainsSubset({'Authorization': self.key}, responses.calls[0].request.headers)
import matplotlib.pyplot as plt import matplotlib.colors as clrs from routingpy import ORS from shipment import Shipment from shipment_step import Shipment_step client = ORS(api_key=API_KEY) def find_unopt_distance(shipment_list, repetition): plot_file = f'route_plots/plot_unopt_{repetition}.svg' fig, ax = plt.subplots() route_color = 'darkcyan' xmin = None ymin = None distances = [] for shipment in shipment_list: pickup_loc = shipment.pickup.location dropoff_loc = shipment.delivery.location if xmin == None or ymin == None: xmin = pickup_loc[0] ymin = pickup_loc[1] xmax = xmin ymax = ymin xmin = min(xmin, pickup_loc[0], dropoff_loc[0]) ymin = min(xmin, pickup_loc[1], pickup_loc[1]) xmax = max(xmin, pickup_loc[0], dropoff_loc[0]) ymax = max(xmin, pickup_loc[1], pickup_loc[1]) lons = [dropoff_loc[0], pickup_loc[0], dropoff_loc[0]] lats = [dropoff_loc[1], pickup_loc[1], dropoff_loc[1]]