diff --git a/amarillo/plugins/enhancer/services/routing.py b/amarillo/plugins/enhancer/services/routing.py new file mode 100644 index 0000000..fbf8e02 --- /dev/null +++ b/amarillo/plugins/enhancer/services/routing.py @@ -0,0 +1,47 @@ +import requests +import logging + +logger = logging.getLogger(__name__) + +class RoutingException(Exception): + def __init__(self, message): + # Call Exception.__init__(message) + # to use the same Message header as the parent class + super().__init__(message) + +class RoutingService(): + def __init__(self, gh_url = 'https://api.mfdz.de/gh'): + self.gh_service_url = gh_url + + def path_for_stops(self, points): + # Retrieve graphhopper route traversing given points + directions = self._get_directions(points) + if directions and len(directions.get("paths"))>0: + return directions.get("paths")[0] + else: + return {} + + def _get_directions(self, points): + req_url = self._create_url(points, True, True) + logger.debug("Get directions via: {}".format(req_url)) + response = requests.get(req_url) + status = response.status_code + if status == 200: + # Found route between points + return response.json() + else: + try: + message = response.json().get('message') + except: + raise RoutingException("Get directions failed with status code {}".format(status)) + else: + raise RoutingException(message) + + def _create_url(self, points, calc_points = False, instructions = False): + """ Creates GH request URL """ + locations = "" + for point in points: + locations += "point={0}%2C{1}&".format(point.y, point.x) + + return "{0}/route?{1}instructions={2}&calc_points={3}&points_encoded=false".format( + self.gh_service_url, locations, instructions, calc_points) diff --git a/amarillo/plugins/enhancer/services/trips.py b/amarillo/plugins/enhancer/services/trips.py index de2b4f0..5715fb6 100644 --- a/amarillo/plugins/enhancer/services/trips.py +++ b/amarillo/plugins/enhancer/services/trips.py @@ -1,7 +1,7 @@ from amarillo.models.gtfs import GtfsTimeDelta, GtfsStopTime from amarillo.models.Carpool import MAX_STOPS_PER_TRIP, Carpool, Weekday, StopTime, PickupDropoffType from amarillo.plugins.enhancer.services.gtfs_constants import * -from amarillo.services.routing import RoutingService, RoutingException +from amarillo.plugins.enhancer.services.routing import RoutingService, RoutingException from amarillo.plugins.enhancer.services.stops import is_carpooling_stop from amarillo.utils.utils import assert_folder_exists, is_older_than_days, yesterday, geodesic_distance_in_m from amarillo.models.Carpool import Driver, RidesharingInfo