added routing.py
This commit is contained in:
parent
44dcda731f
commit
1c97fbd901
47
amarillo/plugins/enhancer/services/routing.py
Normal file
47
amarillo/plugins/enhancer/services/routing.py
Normal file
|
|
@ -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)
|
||||
|
|
@ -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
|
||||
|
|
|
|||
Loading…
Reference in a new issue