def staticmap(ctx, mapid, output, features, lat, lon, zoom, size): """ Generate static map images from existing Mapbox map ids. Optionally overlay with geojson features. $ mapbox staticmap --features features.geojson mapbox.satellite out.png $ mapbox staticmap --lon -61.7 --lat 12.1 --zoom 12 mapbox.satellite out2.png An access token is required, see `mapbox --help`. """ access_token = (ctx.obj and ctx.obj.get('access_token')) or None if features: features = list( cligj.normalize_feature_inputs(None, 'features', [features])) service = mapbox.Static(access_token=access_token) try: res = service.image(mapid, lon=lon, lat=lat, z=zoom, width=size[0], height=size[1], features=features, sort_keys=True) except mapbox.errors.ValidationError as exc: raise click.BadParameter(str(exc)) if res.status_code == 200: output.write(res.content) else: raise MapboxCLIException(res.text.strip())
def test_staticmap_lonlatz_only(): responses.add( responses.GET, 'https://api.mapbox.com/v4/mapbox.satellite/-61.7,12.1,12/600x600.png256?access_token=pk.test', match_querystring=True, body='png123', status=200, content_type='image/png') res = mapbox.Static(access_token='pk.test').image('mapbox.satellite', -61.7, 12.1, 12) assert res.status_code == 200
def fetch_image_at_coords(lat, lon, zoom, retina=True): """ Loads a map image file at the requested coords and zoom from Mapbox. """ client = mapbox.Static() response = client.image( "mapbox.satellite", lon=lon, lat=lat, z=zoom, width=1280, height=1280, retina=retina, ) assert response.status_code == 200, "Failed to fetch map image from Mapbox" return response.content
def test_staticmap_auto_features(points): overlay = json.dumps({ 'type': 'FeatureCollection', 'features': points }, separators=(',', ':')) overlay = quote(overlay) url = ('https://api.mapbox.com/v4/mapbox.satellite/geojson({0})/' 'auto/600x600.png256?access_token=pk.test'.format(overlay)) responses.add(responses.GET, url, match_querystring=True, body='png123', status=200, content_type='image/png') res = mapbox.Static(access_token='pk.test').image('mapbox.satellite', features=points) assert res.status_code == 200
elif isinstance(obj, (list, tuple)): return list(map(pretty_floats, obj)) return obj # ## Plotting if __name__ == '__main__': parser = argparse.ArgumentParser() parser.add_argument('city') parser.add_argument('--folder', default='data/') parser.add_argument('--max_regions', type=int, default=5) args = parser.parse_args() mapbox_service = mapbox.Static() mapbox_service.session.params['access_token'] = os.environ[ 'MAPBOX_ACCESS_TOKEN'] # alternatively, you can uncomment the next line and pass your token directly # mapbox_service.session.params['access_token'] = 'YOUR_TOKEN' mainCats = [ 'Arts & Entertainment', 'College & University', 'Food', 'Nightlife Spot', 'Outdoors & Recreation', 'Shop & Service', 'Professional & Other Places', 'Residence', 'Travel & Transport' ] main_cats_plot = mainCats[:] mainCats = {c: i for i, c in enumerate(mainCats)} mainCats['Event'] = 9
def test_class_attrs(): """Get expected class attr values""" serv = mapbox.Static() assert serv.api_name is None assert serv.api_version == 'v4'
def test_lon_invalid(): service = mapbox.Static(access_token='pk.test') with pytest.raises(mapbox.errors.ValidationError): service._validate_lat(-86.0) with pytest.raises(mapbox.errors.ValidationError): service._validate_lon(-181.0)
def test_latlon(): service = mapbox.Static(access_token='pk.test') assert -179.0 == service._validate_lon(-179.0) assert -85.0 == service._validate_lat(-85.0)
def test_staticmap_imagesize(): service = mapbox.Static(access_token='pk.test') with pytest.raises(mapbox.errors.ValidationError): service._validate_image_size(0) with pytest.raises(mapbox.errors.ValidationError): service._validate_image_size(2000)
def test_staticmap_featurestoolarge(points): service = mapbox.Static(access_token='pk.test') with pytest.raises(mapbox.errors.ValidationError): service._validate_overlay(json.dumps(points * 100))
def test_staticmap_auto_nofeatures(points): with pytest.raises(ValueError): mapbox.Static(access_token='pk.test').image('mapbox.satellite')