From bc4c9fea33dd4c39e89f3676c3a63268c65ce279 Mon Sep 17 00:00:00 2001 From: Antonio Date: Mon, 26 Feb 2018 12:45:08 +0100 Subject: [PATCH] Using Mapbox returned destinations as coordinates for isolines --- .../cartodb_services/mapbox/isolines.py | 24 +++++++++++++------ .../cartodb_services/mapbox/matrix_client.py | 2 ++ 2 files changed, 19 insertions(+), 7 deletions(-) diff --git a/server/lib/python/cartodb_services/cartodb_services/mapbox/isolines.py b/server/lib/python/cartodb_services/cartodb_services/mapbox/isolines.py index e7f06c1..b837a3b 100644 --- a/server/lib/python/cartodb_services/cartodb_services/mapbox/isolines.py +++ b/server/lib/python/cartodb_services/cartodb_services/mapbox/isolines.py @@ -4,6 +4,7 @@ Uses the Mapbox Time Matrix service. ''' import json +from cartodb_services.tools import Coordinate from cartodb_services.tools.spherical import (get_angles, calculate_dest_location) from cartodb_services.mapbox.matrix_client import (validate_profile, @@ -11,7 +12,9 @@ from cartodb_services.mapbox.matrix_client import (validate_profile, PROFILE_WALKING, PROFILE_DRIVING, PROFILE_CYCLING, - ENTRY_DURATIONS) + ENTRY_DURATIONS, + ENTRY_DESTINATIONS, + ENTRY_LOCATION) MAX_SPEEDS = { PROFILE_WALKING: 3.3333333, # In m/s, assuming 12km/h walking speed @@ -53,6 +56,7 @@ class MapboxIsolines(): return [] costs = [None] * number_of_angles + destinations = [None] * number_of_angles for idx, cost in enumerate(json_response[ENTRY_DURATIONS][0][1:]): if cost: @@ -60,7 +64,11 @@ class MapboxIsolines(): else: costs[idx] = isorange - return costs + for idx, destination in enumerate(json_response[ENTRY_DESTINATIONS][1:]): + destinations[idx] = Coordinate(destination[ENTRY_LOCATION][0], + destination[ENTRY_LOCATION][1]) + + return costs, destinations def calculate_isochrone(self, origin, time_ranges, profile=DEFAULT_PROFILE): @@ -122,10 +130,12 @@ class MapboxIsolines(): # NOTE: sometimes it cannot calculate the cost and returns None. # Just assume isorange and stop the calculations there - costs = cost_method(origin=origin, targets=location_estimates, - isorange=isorange, profile=profile, - unit_factor=unit_factor, - number_of_angles=number_of_angles) + costs, destinations = cost_method(origin=origin, + targets=location_estimates, + isorange=isorange, + profile=profile, + unit_factor=unit_factor, + number_of_angles=number_of_angles) if not costs: continue @@ -153,7 +163,7 @@ class MapboxIsolines(): location_estimates_filtered = [] for i, c in enumerate(costs): if c != isorange: - location_estimates_filtered.append(location_estimates[i]) + location_estimates_filtered.append(destinations[i]) return location_estimates_filtered diff --git a/server/lib/python/cartodb_services/cartodb_services/mapbox/matrix_client.py b/server/lib/python/cartodb_services/cartodb_services/mapbox/matrix_client.py index 8d547b6..fd4cf1f 100644 --- a/server/lib/python/cartodb_services/cartodb_services/mapbox/matrix_client.py +++ b/server/lib/python/cartodb_services/cartodb_services/mapbox/matrix_client.py @@ -30,6 +30,8 @@ VALID_PROFILES = [PROFILE_DRIVING_TRAFFIC, PROFILE_WALKING] ENTRY_DURATIONS = 'durations' +ENTRY_DESTINATIONS = 'destinations' +ENTRY_LOCATION = 'location' def validate_profile(profile):