def parse_routes(response, path_polyline): """ Parses response contents to infer speed. If response returned more than one route, it picks the one closest on average to the original request :param response: request content :param path_polyline: original request path encoded list of lat lon tuples :return: """ data = {} if response.status_code == 200: content = response.json() if content['routes']: if len(content['routes']) > 1: for route in content['routes']: route_data = parse_route(route) route_data['polyline_proximity'] = spatial.compute_average_proximity_to_polyline( route_data['google_polyline'], path_polyline) if data: # pick closest one if data['polyline_proximity'] > route_data['polyline_proximity']: data = route_data else: data = route_data else: data = parse_route(content['routes'][0]) else: logging.info(f"Request did not yield any routes. Status: {content['status']}") if 'error_message' in content: logging.info(f"Error message: {content['error_message']}") else: logging.warning(f'Request was not successful. Status code {response.status_code}. ' f'Content of the unsuccessful response: {response.json()}') return data
def test_compute_average_proximity_to_polyline_when_they_are_the_same_line(): poly_1 = 'ahmyHzvYkCvCuCdDcBrB' poly_2 = 'ahmyHzvYkCvCuCdDcBrB' dist = spatial.compute_average_proximity_to_polyline(poly_1, poly_2) assert dist == 0
def test_compute_average_proximity_to_polyline(): poly_1 = 'ahmyHzvYkCvCuCdDcBrB' poly_2 = 'ahmyHzvYGJyBbCGHq@r@EDIJGBu@~@SToAzAEFEDIJ' dist = spatial.compute_average_proximity_to_polyline(poly_1, poly_2) assert round(dist, 5) == round(1.306345084680333, 5)