def _get_matrix(cls, json_response): ''' build the 1-n response matrix from geovelo table each element is ["start", "end", duration] (first being the header) as it is ordered, we only consider the third ''' sn_routing_matrix = response_pb2.StreetNetworkRoutingMatrix() row = sn_routing_matrix.rows.add() # checking header of geovelo's response if json_response[0] != [ "start_reference", "end_reference", "duration" ]: logging.getLogger(__name__).error( 'Geovelo parsing error. Response: {}'.format(json_response)) raise UnableToParse( 'Geovelo parsing error. Response: {}'.format(json_response)) add_ = row.routing_response.add for e in itertools.islice(json_response, 1, sys.maxint): duration, routing_status = ( e[2], response_pb2.reached) if e[2] else (-1, response_pb2.unknown) add_(duration=duration, routing_status=routing_status) return sn_routing_matrix
def _get_matrix(cls, json_response): ''' build the 1-n response matrix from geovelo table each element is ["start", "end", duration] (first being the header) as it is ordered, we only consider the third ''' sn_routing_matrix = response_pb2.StreetNetworkRoutingMatrix() row = sn_routing_matrix.rows.add() #checking header of geovelo's response if json_response[0] != [ "start_reference", "end_reference", "duration" ]: logging.getLogger(__name__).error( 'Geovelo parsing error. Response: {}'.format(json_response)) raise UnableToParse( 'Geovelo parsing error. Response: {}'.format(json_response)) for e in json_response[1:]: routing = row.routing_response.add() if e[2]: routing.duration = e[2] routing.routing_status = response_pb2.reached else: routing.duration = -1 routing.routing_status = response_pb2.unknown return sn_routing_matrix
def _create_matrix_response(self, json_response, origins, destinations): travel_times = json_response.get('matrix', {}).get('travelTimes') sn_routing_matrix = response_pb2.StreetNetworkRoutingMatrix() row = sn_routing_matrix.rows.add() num_destinations = len(destinations) for i_o, _ in enumerate( itertools.islice(origins, int(self.max_matrix_points))): for i_d, _ in enumerate( itertools.islice(destinations, int(self.max_matrix_points))): routing = row.routing_response.add() # the idx algorithm is describe inside the doc # https://developer.here.com/documentation/matrix-routing-api/api-reference-swagger.html idx = num_destinations * i_o + i_d if idx < self.max_matrix_points: routing.duration = travel_times[idx] routing.routing_status = response_pb2.reached else: # since we limit the number of points given to HERE, we say that all the others cannot be # reached (and not response_pb2.unkown since we don't want a crow fly section) routing.duration = -1 routing.routing_status = response_pb2.unreached return sn_routing_matrix
def _get_matrix(cls, json_response): sn_routing_matrix = response_pb2.StreetNetworkRoutingMatrix() for one_to_many in json_response['one_to_many']: row = sn_routing_matrix.rows.add() for one in one_to_many[1:]: routing = row.routing_response.add() if one['time']: routing.duration = one['time'] routing.routing_status = response_pb2.reached else: routing.duration = -1 routing.routing_status = response_pb2.unknown return sn_routing_matrix
def _get_matrix(cls, json_response, mode_park_cost): sn_routing_matrix = response_pb2.StreetNetworkRoutingMatrix() # kraken dosn't handle n-m, we can't have more than one row as they will be ignored row = sn_routing_matrix.rows.add() for source_to_target in json_response['sources_to_targets']: for one in source_to_target: routing = row.routing_response.add() if one['time']: # the mode's base cost represent the initial cost to take the given mode # it is used to represent that it takes time to park a car or a bike # since valhalla does not handle such a time, we tweak valhalla's result to add it routing.duration = one['time'] + (mode_park_cost or 0) routing.routing_status = response_pb2.reached else: routing.duration = -1 routing.routing_status = response_pb2.unknown return sn_routing_matrix
def _get_matrix(cls, json_response, origins, destinations): sn_routing_matrix = response_pb2.StreetNetworkRoutingMatrix() # by convenience we create a temporary structure. If it's a bottleneck, refactor this entries = { (e.get('startIndex'), e.get('destinationIndex')): e.get('summary', {}).get('travelTime') for e in json_response.get('response', {}).get('matrixEntry', []) } for i_o, o in enumerate(origins): row = sn_routing_matrix.rows.add() for i_d, d in enumerate(destinations): routing = row.routing_response.add() travel_time = entries.get((i_o, i_d)) if travel_time: routing.duration = travel_time routing.routing_status = response_pb2.reached else: # since we limit the number of points given to HERE, we say that all the others cannot be # reached (and not response_pb2.unkown since we don't want a crow fly section) routing.duration = -1 routing.routing_status = response_pb2.unreached return sn_routing_matrix
def _get_sn_routing_matrix_with_multi_direct_path(self, origins, destinations, mode, max_duration, request, realtime_traffic, max_matrix_points): sn_routing_matrix = response_pb2.StreetNetworkRoutingMatrix() row = sn_routing_matrix.rows.add() for origin in itertools.islice(origins, int(max_matrix_points)): for destination in itertools.islice(destinations, int(max_matrix_points)): params = self.get_direct_path_params( origin, destination, mode, PeriodExtremity(datetime=request['datetime'], represents_start=request['clockwise']), request, realtime_traffic, ) r = self._call_here(self.routing_service_url, params=params) r.raise_for_status() self._create_matrix_response_with_direct_path(r.json(), row) return sn_routing_matrix