Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def _build_locations(self, coordinates, matrix=False):
"""Build the locations object for all methods"""
locations = []
# Directions and matrix calls which are lists of list
if isinstance(coordinates[0], (list, tuple, self.Waypoint)):
for idx, coord in enumerate(coordinates):
if isinstance(locations, self.Waypoint):
locations.append(locations._make_waypoint())
elif isinstance(locations, (list, tuple)):
wp = 'geo!' + convert._delimit_list(
[convert._format_float(f) for f in list(reversed(coord))], ','
)
locations.append(wp)
else:
raise TypeError(
"Location type {} at index {} is not supported: {}".format(
type(coord), idx, coord
)
)
# Isochrones
elif isinstance(coordinates[0], float):
center = 'geo!' + convert._delimit_list(
[convert._format_float(f) for f in list(reversed(coordinates))], ','
)
locations.append(center)
# Isochrones using waypoint class
elif isinstance(locations, (list, tuple)):
wp = 'geo!' + convert._delimit_list(
[convert._format_float(f) for f in list(reversed(coord))], ','
)
locations.append(wp)
else:
raise TypeError(
"Location type {} at index {} is not supported: {}".format(
type(coord), idx, coord
)
)
# Isochrones
elif isinstance(coordinates[0], float):
center = 'geo!' + convert._delimit_list(
[convert._format_float(f) for f in list(reversed(coordinates))], ','
)
locations.append(center)
# Isochrones using waypoint class
elif isinstance(coordinates, self.Waypoint):
locations.append(coordinates._make_waypoint())
return locations
except TypeError:
# Raised when sources == None
pass
try:
destinations_out = []
for idx in destinations:
destinations_out.append(locations[idx])
except IndexError:
raise IndexError(
"Parameter destinations out of locations range at index {}.".format(idx)
)
except TypeError:
# Raised when destinations == None
pass
sources_out = (reversed([convert._format_float(f) for f in coord]) for coord in sources_out)
params.extend([("from_point", ",".join(coord)) for coord in sources_out])
destinations_out = (
reversed([convert._format_float(f) for f in coord]) for coord in destinations_out
)
params.extend([("to_point", ",".join(coord)) for coord in destinations_out])
if out_array is not None:
for e in out_array:
params.append(("out_array", e))
if debug is not None:
params.append(('debug', convert._convert_bool(debug)))
return self._parse_matrix_json(self._request('/matrix', get_params=params, dry_run=dry_run), )
if departure is not None:
params["departure"] = departure
elif arrival is not None:
params["arrival"] = arrival
if alternatives is not None:
params["alternatives"] = alternatives
if metric_system is not None:
params["metricSystem"] = metric_system
if view_bounds is not None:
params["viewBounds"] = convert._delimit_list(
[
convert._delimit_list([convert._format_float(f)
for f in list(reversed(pair))], ',')
for pair in view_bounds
], ';'
)
if resolution is not None:
params["resolution"] = str(resolution['viewresolution'])
if 'snapresolution' in resolution:
params["resolution"] += ':' + str(resolution['snapresolution'])
if instruction_format is not None:
params["instructionFormat"] = instruction_format
if json_attributes is not None:
params["jsonAttributes"] = json_attributes
if roundabout_exits:
params['roundabout_exits'] = convert._convert_bool(roundabout_exits)
if voice_instructions:
params['voide_instructions'] = convert._convert_bool(voice_instructions)
if voice_units:
params['voice_units'] = voice_units
if waypoint_names:
params['waypoint_names'] = convert._delimit_list(waypoint_names, ';')
if waypoint_targets:
params['waypoint_targets'] = ';' + convert._delimit_list(
[
convert._delimit_list([convert._format_float(f)
for f in pair])
for pair in waypoint_targets
], ';'
)
get_params = {'access_token': self.api_key} if self.api_key else {}
return self._parse_direction_json(
self._request(
"/directions/v5/mapbox/" + profile,
get_params=get_params,
post_params=params,
dry_run=dry_run,
requests_kwargs={"headers": {
"Content-Type": 'application/x-www-form-urlencoded'
}},
:returns: An isochrone with the specified range.
:rtype: :class:`routingpy.isochrone.Isochrones`
"""
params = [('vehicle', profile), ('type', type)]
if convert._is_list(intervals):
if interval_type in (None, 'time'):
params.append(('time_limit', intervals[0]))
elif interval_type == 'distance':
params.append(('distance_limit', intervals[0]))
else:
raise TypeError("Parameter range={} must be of type list or tuple".format(range))
center = [convert._format_float(f) for f in locations]
center.reverse()
params.append(("point", ",".join(center)))
if self.key is not None:
params.append(("key", self.key))
if buckets is not None:
params.append(('buckets', buckets))
if reverse_flow is not None:
params.append(('reverse_flow', convert._convert_bool(reverse_flow)))
if debug is not None:
params.append(('debug', convert._convert_bool(debug)))
return self._parse_isochrone_json(
(starting with 0). If not passed, all indices are considered.
:type sources: list of int
:param destinations: A list of indices that refer to the list of locations
(starting with 0). If not passed, all indices are considered.
:type destinations: list of int
:param dry_run: Print URL and parameters without sending the request.
:param dry_run: bool
:returns: A matrix from the specified sources and destinations.
:rtype: :class:`routingpy.matrix.Matrix`
"""
coords = convert._delimit_list(
[convert._delimit_list([convert._format_float(f) for f in pair]) for pair in locations], ';'
)
params = dict()
if sources:
params['sources'] = convert._delimit_list(sources, ';')
if destinations:
params['destinations'] = convert._delimit_list(destinations, ';')
return self._parse_matrix_json(
self._request("/table/v1/" + profile + '/' + coords, get_params=params, dry_run=dry_run)
)