def test_encode_limits(): expected = "-87.337875,36.539157;-88.247681,36.922175" assert expected == encode_waypoints(gj_point_features) with pytest.raises(ValueError) as exc: encode_waypoints(gj_point_features, min_limit=3) assert 'at least' in str(exc.value) with pytest.raises(ValueError) as exc: encode_waypoints(gj_point_features, max_limit=1) assert 'at most' in str(exc.value)
def directions(self, features, profile='mapbox.driving', alternatives=None, instructions=None, geometry=None, steps=None): """Request directions for waypoints encoded as GeoJSON features. Parameters ---------- features : list List of GeoJSON features. profile : str Name of a Mapbox profile such as 'mapbox.driving'. alternatives : bool instructions : str geometry : str steps : bool Returns ------- response It returns a response object with a geojson() method for accessing the route(s) as a GeoJSON-like FeatureCollection dictionary. """ profile = self._validate_profile(profile) instructions = self._validate_instruction_format(instructions) geometry = self._validate_geom_encoding(geometry) waypoints = encode_waypoints(features, precision=6, min_limit=2, max_limit=30) params = {} if alternatives is not None: params.update( {'alternatives': 'true' if alternatives is True else 'false'}) if instructions is not None: params.update({'instructions': instructions}) if geometry is not None: params.update( {'geometry': 'false' if geometry is False else geometry}) if steps is not None: params.update({'steps': 'true' if steps is True else 'false'}) uri = URITemplate(self.baseuri + '/{profile}/{waypoints}.json').expand( profile=profile, waypoints=waypoints) resp = self.session.get(uri, params=params) self.handle_http_error(resp) def geojson(): return self._geojson(resp.json()) resp.geojson = geojson return resp
def matrix(self, coordinates, profile='mapbox/driving', sources=None, destinations=None): """Request a directions matrix for trips between coordinates In the default case, the matrix returns a symmetric matrix, using all input coordinates as sources and destinations. You may also generate an asymmetric matrix, with only some coordinates as sources or destinations: Parameters ---------- coordinates : sequence A sequence of coordinates, which may be represented as GeoJSON features, GeoJSON geometries, or (longitude, latitude) pairs. profile : str The trip travel mode. Valid modes are listed in the class's valid_profiles attribute. sources : list Indices of source coordinates to include in the matrix. Default is all coordinates. destinations : list Indices of destination coordinates to include in the matrix. Default is all coordinates. Returns ------- requests.Response Note: the directions matrix itself is obtained by calling the response's json() method. The resulting mapping has 4 items. code : str Status of the response durations : list An array of arrays representing the matrix in row-major order. durations[i][j] gives the travel time from the i-th source to the j-th destination. All values are in seconds. The duration between the same coordinate is always 0. If a duration can not be found, the result is null. destinations : list Results of snapping selected coordinates to the nearest addresses. destinations : list Results of snapping selected coordinates to the nearest addresses. """ profile = self._validate_profile(profile) coords = encode_waypoints(coordinates) query = self._make_query(sources, destinations) uri = '{0}/{1}/{2}'.format(self.baseuri, profile, coords) res = self.session.get(uri, params=query) self.handle_http_error(res) return res
def test_encode_waypoints_rounding(): expected = "1.0,0.0" int_coord_features = [{ "type": "Feature", "geometry": { "type": "Point", "coordinates": [1, 0] }, "properties": {} }] assert expected == encode_waypoints(int_coord_features)
def surface(self, features, mapid="mapbox.mapbox-terrain-v1", layer="contour", fields=["ele"], geojson=True, polyline=False, interpolate=None, zoom=None): warnings.warn( "The surface module will be removed in the next version. " "It has no replacement.", MapboxDeprecationWarning) params = { 'layer': layer, 'fields': ','.join(fields), 'geojson': 'true' if geojson else 'false', } if interpolate is not None: params['interpolate'] = 'true' if interpolate else 'false', if zoom is not None: params['zoom'] = zoom if polyline: params['encoded_polyline'] = encode_polyline(features) else: params['points'] = encode_waypoints(features, precision=6, min_limit=1, max_limit=300) uri = URITemplate(self.baseuri + '/{mapid}.json').expand(mapid=mapid) res = self.session.get(uri, params=params) self.handle_http_error(res) def geojson(): return res.json()['results'] res.geojson = geojson return res
def directions(self, features, profile='mapbox.driving', alternatives=None, instructions=None, geometry=None, steps=None): """Request directions for waypoints encoded as GeoJSON features. :param features: sequence of GeoJSON features. :param profile: name of a profile. """ profile = self._validate_profile(profile) instructions = self._validate_instruction_format(instructions) geometry = self._validate_geom_encoding(geometry) waypoints = encode_waypoints(features, precision=6, min_limit=2, max_limit=30) params = {} if alternatives is not None: params.update( {'alternatives': 'true' if alternatives is True else 'false'}) if instructions is not None: params.update({'instructions': instructions}) if geometry is not None: params.update( {'geometry': 'false' if geometry is False else geometry}) if steps is not None: params.update({'steps': 'true' if steps is True else 'false'}) uri = URITemplate('%s/{profile}/{waypoints}.json' % self.baseuri).expand(profile=profile, waypoints=waypoints) resp = self.session.get(uri, params=params) self.handle_http_error(resp) def geojson(): return self._geojson(resp.json()) resp.geojson = geojson return resp
def surface(self, features, mapid="mapbox.mapbox-terrain-v1", layer="contour", fields=["ele"], geojson=True, polyline=False, interpolate=None, zoom=None): params = { 'layer': layer, 'fields': ','.join(fields), 'geojson': 'true' if geojson else 'false', } if interpolate is not None: params['interpolate'] = 'true' if interpolate else 'false', if zoom is not None: params['zoom'] = zoom if polyline: params['encoded_polyline'] = encode_polyline(features) else: params['points'] = encode_waypoints( features, precision=6, min_limit=1, max_limit=300) uri = URITemplate(self.baseuri + '/{mapid}.json').expand(mapid=mapid) res = self.session.get(uri, params=params) self.handle_http_error(res) def geojson(): return res.json()['results'] res.geojson = geojson return res
def directions(self, features, profile='mapbox.driving', alternatives=None, instructions=None, geometry=None, steps=None): """Request directions for waypoints encoded as GeoJSON features. :param features: sequence of GeoJSON features. :param profile: name of a profile. """ profile = self._validate_profile(profile) instructions = self._validate_instruction_format(instructions) geometry = self._validate_geom_encoding(geometry) waypoints = encode_waypoints(features, precision=6, min_limit=2, max_limit=30) params = {} if alternatives is not None: params.update( {'alternatives': 'true' if alternatives is True else 'false'}) if instructions is not None: params.update({'instructions': instructions}) if geometry is not None: params.update({'geometry': 'false' if geometry is False else geometry}) if steps is not None: params.update( {'steps': 'true' if steps is True else 'false'}) uri = URITemplate('%s/{profile}/{waypoints}.json' % self.baseuri).expand( profile=profile, waypoints=waypoints) resp = self.session.get(uri, params=params) self.handle_http_error(resp) def geojson(): return self._geojson(resp.json()) resp.geojson = geojson return resp
def test_encode_waypoints(): expected = "-87.337875,36.539157;-88.247681,36.922175" assert expected == encode_waypoints(gj_point_features) assert expected == encode_waypoints(gj_multipoint_features) assert expected == encode_waypoints(gj_line_features)