@@ -6,23 +6,17 @@ Uses the Mapbox Time Matrix service.
|
|||||||
import json
|
import json
|
||||||
from cartodb_services.tools.spherical import (get_angles,
|
from cartodb_services.tools.spherical import (get_angles,
|
||||||
calculate_dest_location)
|
calculate_dest_location)
|
||||||
|
from cartodb_services.mapbox.matrix_client import (validate_profile,
|
||||||
PROFILE_DRIVING_TRAFFIC = 'driving-traffic'
|
DEFAULT_PROFILE,
|
||||||
PROFILE_DRIVING = 'driving'
|
PROFILE_WALKING,
|
||||||
PROFILE_CYCLING = 'cycling'
|
PROFILE_DRIVING,
|
||||||
PROFILE_WALKING = 'walking'
|
PROFILE_CYCLING,
|
||||||
DEFAULT_PROFILE = PROFILE_DRIVING
|
ENTRY_DURATIONS)
|
||||||
|
|
||||||
VALID_PROFILES = [PROFILE_DRIVING_TRAFFIC,
|
|
||||||
PROFILE_DRIVING,
|
|
||||||
PROFILE_CYCLING,
|
|
||||||
PROFILE_WALKING]
|
|
||||||
|
|
||||||
MAX_SPEEDS = {
|
MAX_SPEEDS = {
|
||||||
PROFILE_WALKING: 3.3333333, # In m/s, assuming 12km/h walking speed
|
PROFILE_WALKING: 3.3333333, # In m/s, assuming 12km/h walking speed
|
||||||
PROFILE_CYCLING: 16.67, # In m/s, assuming 60km/h max speed
|
PROFILE_CYCLING: 16.67, # In m/s, assuming 60km/h max speed
|
||||||
PROFILE_DRIVING: 41.67, # In m/s, assuming 140km/h max speed
|
PROFILE_DRIVING: 41.67 # In m/s, assuming 140km/h max speed
|
||||||
PROFILE_DRIVING_TRAFFIC: 41.67, # In m/s, assuming 140km/h max speed
|
|
||||||
}
|
}
|
||||||
|
|
||||||
DEFAULT_NUM_ANGLES = 24
|
DEFAULT_NUM_ANGLES = 24
|
||||||
@@ -37,8 +31,6 @@ UNIT_FACTOR_ISOCHRONE = 1.0
|
|||||||
UNIT_FACTOR_ISODISTANCE = 1000.0
|
UNIT_FACTOR_ISODISTANCE = 1000.0
|
||||||
DEFAULT_UNIT_FACTOR = UNIT_FACTOR_ISOCHRONE
|
DEFAULT_UNIT_FACTOR = UNIT_FACTOR_ISOCHRONE
|
||||||
|
|
||||||
ENTRY_DURATIONS = 'durations'
|
|
||||||
|
|
||||||
|
|
||||||
class MapboxIsolines():
|
class MapboxIsolines():
|
||||||
'''
|
'''
|
||||||
@@ -50,14 +42,6 @@ class MapboxIsolines():
|
|||||||
self._matrix_client = matrix_client
|
self._matrix_client = matrix_client
|
||||||
self._logger = logger
|
self._logger = logger
|
||||||
|
|
||||||
def _validate_profile(self, profile):
|
|
||||||
if profile not in VALID_PROFILES:
|
|
||||||
raise ValueError('{profile} is not a valid profile. '
|
|
||||||
'Valid profiles are: {valid_profiles}'.format(
|
|
||||||
profile=profile,
|
|
||||||
valid_profiles=', '.join(
|
|
||||||
[x for x in VALID_PROFILES])))
|
|
||||||
|
|
||||||
def _calculate_matrix_cost(self, origin, targets, isorange,
|
def _calculate_matrix_cost(self, origin, targets, isorange,
|
||||||
profile=DEFAULT_PROFILE,
|
profile=DEFAULT_PROFILE,
|
||||||
unit_factor=UNIT_FACTOR_ISOCHRONE,
|
unit_factor=UNIT_FACTOR_ISOCHRONE,
|
||||||
@@ -78,7 +62,7 @@ class MapboxIsolines():
|
|||||||
|
|
||||||
def calculate_isochrone(self, origin, time_ranges,
|
def calculate_isochrone(self, origin, time_ranges,
|
||||||
profile=DEFAULT_PROFILE):
|
profile=DEFAULT_PROFILE):
|
||||||
self._validate_profile(profile)
|
validate_profile(profile)
|
||||||
|
|
||||||
max_speed = MAX_SPEEDS[profile]
|
max_speed = MAX_SPEEDS[profile]
|
||||||
|
|
||||||
@@ -101,7 +85,7 @@ class MapboxIsolines():
|
|||||||
|
|
||||||
def calculate_isodistance(self, origin, distance_range,
|
def calculate_isodistance(self, origin, distance_range,
|
||||||
profile=DEFAULT_PROFILE):
|
profile=DEFAULT_PROFILE):
|
||||||
self._validate_profile(profile)
|
validate_profile(profile)
|
||||||
|
|
||||||
max_speed = MAX_SPEEDS[profile]
|
max_speed = MAX_SPEEDS[profile]
|
||||||
time_range = distance_range / max_speed
|
time_range = distance_range / max_speed
|
||||||
|
|||||||
@@ -29,6 +29,17 @@ VALID_PROFILES = [PROFILE_DRIVING_TRAFFIC,
|
|||||||
PROFILE_CYCLING,
|
PROFILE_CYCLING,
|
||||||
PROFILE_WALKING]
|
PROFILE_WALKING]
|
||||||
|
|
||||||
|
ENTRY_DURATIONS = 'durations'
|
||||||
|
|
||||||
|
|
||||||
|
def validate_profile(profile):
|
||||||
|
if profile not in VALID_PROFILES:
|
||||||
|
raise ValueError('{profile} is not a valid profile. '
|
||||||
|
'Valid profiles are: {valid_profiles}'.format(
|
||||||
|
profile=profile,
|
||||||
|
valid_profiles=', '.join(
|
||||||
|
[x for x in VALID_PROFILES])))
|
||||||
|
|
||||||
|
|
||||||
class MapboxMatrixClient(Traceable):
|
class MapboxMatrixClient(Traceable):
|
||||||
'''
|
'''
|
||||||
@@ -40,21 +51,13 @@ class MapboxMatrixClient(Traceable):
|
|||||||
self._token = token
|
self._token = token
|
||||||
self._logger = logger
|
self._logger = logger
|
||||||
|
|
||||||
def _validate_profile(self, profile):
|
|
||||||
if profile not in VALID_PROFILES:
|
|
||||||
raise ValueError('{profile} is not a valid profile. '
|
|
||||||
'Valid profiles are: {valid_profiles}'.format(
|
|
||||||
profile=profile,
|
|
||||||
valid_profiles=', '.join(
|
|
||||||
[x for x in VALID_PROFILES])))
|
|
||||||
|
|
||||||
def _uri(self, coordinates, profile=DEFAULT_PROFILE):
|
def _uri(self, coordinates, profile=DEFAULT_PROFILE):
|
||||||
return BASEURI.format(profile=profile, coordinates=coordinates,
|
return BASEURI.format(profile=profile, coordinates=coordinates,
|
||||||
token=self._token)
|
token=self._token)
|
||||||
|
|
||||||
@qps_retry(qps=1)
|
@qps_retry(qps=1)
|
||||||
def matrix(self, coordinates, profile=DEFAULT_PROFILE):
|
def matrix(self, coordinates, profile=DEFAULT_PROFILE):
|
||||||
self._validate_profile(profile)
|
validate_profile(profile)
|
||||||
validate_coordinates(coordinates,
|
validate_coordinates(coordinates,
|
||||||
NUM_COORDINATES_MIN, NUM_COORDINATES_MAX)
|
NUM_COORDINATES_MIN, NUM_COORDINATES_MAX)
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user