def test_uknown_mode_raise_exception(self, req_mock):
        req_mock.register_uri('GET',
                              requests_mock.ANY,
                              text=self.GOOD_RESPONSE_SIMPLE)
        origin = Coordinate('-120.2', '38.5')
        destination = Coordinate('-126.4', '43.2')
        waypoints = [origin, destination]

        assert_raises(WrongParams, self.routing.calculate_route_point_to_point,
                      waypoints, 'unknown')
Пример #2
0
    def test_nonstandard_url(self, req_mock):
        url = 'http://serviceurl.com'
        routing = MapzenRouting('api_key', Mock(), {'base_url': url})
        req_mock.register_uri('GET', url, text=self.GOOD_RESPONSE_SIMPLE)
        origin = Coordinate('-120.2', '38.5')
        destination = Coordinate('-126.4', '43.2')
        waypoints = [origin, destination]
        response = routing.calculate_route_point_to_point(waypoints, 'car')

        self.assertEqual(response.shape, self.GOOD_SHAPE_SIMPLE)
        self.assertEqual(response.length, 444.59)
        self.assertEqual(response.duration, 16969)
    def test_calculate_simple_routing_with_valid_params(self, req_mock):
        req_mock.register_uri('GET', requests_mock.ANY,
                              text=self.GOOD_RESPONSE_SIMPLE)
        origin = Coordinate('-120.2', '38.5')
        destination = Coordinate('-126.4', '43.2')
        waypoints = [origin, destination]
        response = self.routing.calculate_route_point_to_point(waypoints,
                                                               'car')

        self.assertEqual(response.shape, self.GOOD_SHAPE_SIMPLE)
        self.assertEqual(response.length, 444.59)
        self.assertEqual(response.duration, 16969)
    def test_calculate_routing_waypoints_with_valid_params(self, req_mock):
        req_mock.register_uri('GET', requests_mock.ANY,
                              text=self.GOOD_RESPONSE_MULTI)
        origin = Coordinate('-3.7', '40.4')
        pass_through = Coordinate('-3.4', '40.1')
        destination = Coordinate('-3.9', '40.6')
        waypoints = [origin, pass_through, destination]

        response = self.routing.calculate_route_point_to_point(waypoints,
                                                               'walk')

        self.assertEqual(response.length, 1.261)
        self.assertEqual(response.duration, 913)
        self.assertEqual(response.shape, self.GOOD_SHAPE_MULTI)
    def _calculate_matrix_cost(self,
                               origin,
                               targets,
                               isorange,
                               profile=DEFAULT_PROFILE,
                               unit_factor=UNIT_FACTOR_ISOCHRONE,
                               number_of_angles=MATRIX_NUM_ANGLES):
        response = self._matrix_client.matrix([origin] + targets, profile)
        json_response = json.loads(response)
        if not json_response:
            return []

        costs = [None] * number_of_angles
        destinations = [None] * number_of_angles

        for idx, cost in enumerate(json_response[ENTRY_DURATIONS][0][1:]):
            if cost:
                costs[idx] = cost * unit_factor
            else:
                costs[idx] = isorange

        for idx, destination in enumerate(
                json_response[ENTRY_DESTINATIONS][1:]):
            destinations[idx] = Coordinate(destination[ENTRY_LOCATION][0],
                                           destination[ENTRY_LOCATION][1])

        return costs, destinations
Пример #6
0
def calculate_dest_location(origin, angle, radius):  # Angle in radians
    origin_lat_radians = radians(origin.latitude)
    origin_long_radians = radians(origin.longitude)
    dest_lat_radians = asin(sin(origin_lat_radians) * cos(radius / EARTH_RADIUS_METERS) + cos(origin_lat_radians) * sin(radius / EARTH_RADIUS_METERS) * cos(angle))
    dest_lng_radians = origin_long_radians + atan2(sin(angle) * sin(radius / EARTH_RADIUS_METERS) * cos(origin_lat_radians), cos(radius / EARTH_RADIUS_METERS) - sin(origin_lat_radians) * sin(dest_lat_radians))

    return Coordinate(degrees(dest_lng_radians), degrees(dest_lat_radians))
import unittest
from mock import Mock
from cartodb_services.mapbox import MapboxMatrixClient
from cartodb_services.mapbox.matrix_client import DEFAULT_PROFILE
from cartodb_services.tools.exceptions import ServiceException
from cartodb_services.tools import Coordinate
from credentials import mapbox_api_key

INVALID_TOKEN = 'invalid_token'
VALID_ORIGIN = Coordinate(-73.989, 40.733)
VALID_TARGET = Coordinate(-74, 40.733)
VALID_COORDINATES = [VALID_ORIGIN] + [VALID_TARGET]
NUM_COORDINATES_MAX = 25
INVALID_COORDINATES_EMPTY = []
INVALID_COORDINATES_MIN = [VALID_ORIGIN]
INVALID_COORDINATES_MAX = [VALID_ORIGIN] + \
                          [VALID_TARGET
                           for x in range(0, NUM_COORDINATES_MAX + 1)]
VALID_PROFILE = DEFAULT_PROFILE
INVALID_PROFILE = 'invalid_profile'


@unittest.skip("Stop using Matrix API. CartoDB/cartodb-management/issues/5199")
class MapboxMatrixTestCase(unittest.TestCase):
    def setUp(self):
        self.matrix_client = MapboxMatrixClient(token=mapbox_api_key(),
                                                logger=Mock())

    def test_invalid_profile(self):
        with self.assertRaises(ValueError):
            self.matrix_client.matrix(VALID_COORDINATES,
Пример #8
0
 def _parse_coordinates(self, boundary):
     coordinates = boundary.get(ENTRY_COORDINATES, [])
     return [Coordinate(c[0], c[1]) for c in coordinates]
import unittest
from mock import Mock
from cartodb_services.mapbox import MapboxRouting
from cartodb_services.mapbox.routing import DEFAULT_PROFILE
from cartodb_services.tools.exceptions import ServiceException
from cartodb_services.tools import Coordinate
from credentials import mapbox_api_key

INVALID_TOKEN = 'invalid_token'
VALID_WAYPOINTS = [Coordinate(-73.989, 40.733), Coordinate(-74, 40.733)]
NUM_WAYPOINTS_MAX = 25
INVALID_WAYPOINTS_EMPTY = []
INVALID_WAYPOINTS_MIN = [Coordinate(-73.989, 40.733)]
INVALID_WAYPOINTS_MAX = [Coordinate(-73.989, 40.733)
                         for x in range(0, NUM_WAYPOINTS_MAX + 2)]
VALID_PROFILE = DEFAULT_PROFILE
INVALID_PROFILE = 'invalid_profile'

WELL_KNOWN_SHAPE = [(40.73312, -73.98891), (40.73353, -73.98987),
                    (40.73398, -73.99095), (40.73453, -73.99227),
                    (40.73531, -73.99412), (40.73467, -73.99459),
                    (40.73442, -73.99477), (40.73435, -73.99482),
                    (40.73403, -73.99505), (40.73344, -73.99549),
                    (40.73286, -73.9959), (40.73226, -73.99635),
                    (40.73186, -73.99664), (40.73147, -73.99693),
                    (40.73141, -73.99698), (40.73147, -73.99707),
                    (40.73219, -73.99856), (40.73222, -73.99861),
                    (40.73293, -74.00007), (40.733, -74.00001)]
WELL_KNOWN_LENGTH = 1317.9

Пример #10
0
 def _parse_coordinates(self, boundary):
     return [
         Coordinate(c[ENTRY_LONGITUDE], c[ENTRY_LATITUDE]) for c in boundary
     ]
import unittest
from mock import Mock
from cartodb_services.mapbox.isolines import MapboxIsolines
from cartodb_services.mapbox.matrix_client import DEFAULT_PROFILE
from cartodb_services.mapbox.matrix_client import MapboxMatrixClient
from cartodb_services.tools import Coordinate
from credentials import mapbox_api_key

VALID_ORIGIN = Coordinate(-73.989, 40.733)


@unittest.skip("Stop using Matrix API. CartoDB/cartodb-management/issues/5199")
class MapboxIsolinesTestCase(unittest.TestCase):
    def setUp(self):
        matrix_client = MapboxMatrixClient(token=mapbox_api_key(),
                                           logger=Mock())
        self.mapbox_isolines = MapboxIsolines(matrix_client, logger=Mock())

    def test_calculate_isochrone(self):
        time_ranges = [300, 900]
        solution = self.mapbox_isolines.calculate_isochrone(
            origin=VALID_ORIGIN,
            profile=DEFAULT_PROFILE,
            time_ranges=time_ranges)

        assert solution

    def test_calculate_isodistance(self):
        distance_range = 10000
        solution = self.mapbox_isolines.calculate_isodistance(
            origin=VALID_ORIGIN,
Пример #12
0
import unittest
from mock import Mock
from cartodb_services.tomtom import TomTomRouting
from cartodb_services.tomtom.routing import DEFAULT_PROFILE
from cartodb_services.tools.exceptions import ServiceException
from cartodb_services.tools import Coordinate
from credentials import tomtom_api_key

INVALID_APIKEY = 'invalid_apikey'
VALID_WAYPOINTS = [Coordinate(13.42936, 52.50931),
                   Coordinate(13.43872, 52.50274)]
NUM_WAYPOINTS_MAX = 20
INVALID_WAYPOINTS_EMPTY = []
INVALID_WAYPOINTS_MIN = [Coordinate(13.42936, 52.50931)]
INVALID_WAYPOINTS_MAX = [Coordinate(13.42936, 52.50931)
                         for x in range(0, NUM_WAYPOINTS_MAX + 2)]
VALID_PROFILE = DEFAULT_PROFILE
INVALID_PROFILE = 'invalid_profile'


class TomTomRoutingTestCase(unittest.TestCase):
    def setUp(self):
        self.routing = TomTomRouting(apikey=tomtom_api_key(), logger=Mock())

    def test_invalid_profile(self):
        with self.assertRaises(ValueError):
            self.routing.directions(VALID_WAYPOINTS, INVALID_PROFILE)

    def test_invalid_waypoints_empty(self):
        with self.assertRaises(ValueError):
            self.routing.directions(INVALID_WAYPOINTS_EMPTY, VALID_PROFILE)
Пример #13
0
import unittest
from mock import Mock
from cartodb_services.tomtom import TomTomRouting
from cartodb_services.tomtom.routing import DEFAULT_PROFILE
from cartodb_services.tools.exceptions import ServiceException
from cartodb_services.tools import Coordinate
from credentials import tomtom_api_key

INVALID_APIKEY = 'invalid_apikey'
VALID_WAYPOINTS = [
    Coordinate(13.42936, 52.50931),
    Coordinate(13.43872, 52.50274)
]
NUM_WAYPOINTS_MAX = 20
INVALID_WAYPOINTS_EMPTY = []
INVALID_WAYPOINTS_MIN = [Coordinate(13.42936, 52.50931)]
INVALID_WAYPOINTS_MAX = [
    Coordinate(13.42936, 52.50931) for x in range(0, NUM_WAYPOINTS_MAX + 2)
]
VALID_PROFILE = DEFAULT_PROFILE
VALID_ROUTE_TYPE = 'fastest'
INVALID_PROFILE = 'invalid_profile'
INVALID_ROUTE_TYPE = 'invalid_route_type'


class TomTomRoutingTestCase(unittest.TestCase):
    def setUp(self):
        self.routing = TomTomRouting(apikey=tomtom_api_key(), logger=Mock())

    def test_invalid_profile(self):
        with self.assertRaises(ValueError):
Пример #14
0
import unittest
from mock import Mock
from cartodb_services.mapbox import MapboxRouting
from cartodb_services.mapbox.routing import DEFAULT_PROFILE
from cartodb_services.tools.exceptions import ServiceException
from cartodb_services.tools import Coordinate
from credentials import mapbox_api_key

INVALID_TOKEN = 'invalid_token'
VALID_WAYPOINTS = [Coordinate(-73.989, 40.733), Coordinate(-74, 40.733)]
NUM_WAYPOINTS_MAX = 25
INVALID_WAYPOINTS_EMPTY = []
INVALID_WAYPOINTS_MIN = [Coordinate(-73.989, 40.733)]
INVALID_WAYPOINTS_MAX = [
    Coordinate(-73.989, 40.733) for x in range(0, NUM_WAYPOINTS_MAX + 2)
]
VALID_PROFILE = DEFAULT_PROFILE
INVALID_PROFILE = 'invalid_profile'

WELL_KNOWN_SHAPE = [(40.73312, -73.98891), (40.73353, -73.98987),
                    (40.73398, -73.99095), (40.73453, -73.99227),
                    (40.73531, -73.99412), (40.73467, -73.99459),
                    (40.73442, -73.99477), (40.73435, -73.99482),
                    (40.73403, -73.99505), (40.73344, -73.99549),
                    (40.73286, -73.9959), (40.73226, -73.99635),
                    (40.73186, -73.99664), (40.73147, -73.99693),
                    (40.73141, -73.99698), (40.73147, -73.99707),
                    (40.73219, -73.99856), (40.73222, -73.99861),
                    (40.73225, -73.99868), (40.73293, -74.00007),
                    (40.733, -74.00001)]
WELL_KNOWN_LENGTH = 1384.9