#!/usr/bin/env python # coding: utf-8 # # Mapboxgl Python Library for location data visualization # # https://github.com/mapbox/mapboxgl-jupyter # # ### Requirements # # These examples require the installation of the following python modules # # ``` # pip install mapboxgl # ``` # # ### Notes # # `ImageViz` object accepts either an url, a local path or a numpy ndarray data as input for an image source # In[ ]: import os import numpy from matplotlib.pyplot import imread from mapboxgl.viz import ImageViz # ## Set your Mapbox access token. # Set a `MAPBOX_ACCESS_TOKEN` environment variable or copy/paste your token # If you do not have a Mapbox access token, sign up for an account at https://www.mapbox.com/ # If you already have an account, you can grab your token at https://www.mapbox.com/account/ # In[ ]: # Must be a public token, starting with `pk` token = os.getenv('MAPBOX_ACCESS_TOKEN') # ## Display an image given an URL # In[ ]: img_url = 'https://raw.githubusercontent.com/mapbox/mapboxgl-jupyter/master/examples/data/mosaic.png' # Coordinates must be an array in the form of [UL, UR, LR, LL] coordinates = [[-123.40515640309, 38.534294809274336], [-115.92938988349292, 38.534294809274336], [-115.92938988349292, 32.08296982365502], [-123.40515640309, 32.08296982365502]] # Create the viz from the dataframe viz = ImageViz(img_url, coordinates, access_token=token, height='600px', center=(-119, 35), zoom=5, below_layer='waterway-label') viz.show() # ## Display an image given a numpy.ndarray # In[ ]: img = imread(img_url) img = numpy.mean(img[::10, ::10], axis=2) # Coordinates must be an array in the form of [UL, UR, LR, LL] coordinates = [[-123.40515640309, 38.534294809274336], [-115.92938988349292, 38.534294809274336], [-115.92938988349292, 32.08296982365502], [-123.40515640309, 32.08296982365502]] # Create the viz from the dataframe viz = ImageViz(img, coordinates, access_token=token, height='600px', center=(-119, 35), zoom=5, below_layer='waterway-label') viz.show() # ## Display an image given a local path # In[ ]: # Coordinates must be an array in the form of [UL, UR, LR, LL] coordinates = [[-123.40515640309, 38.534294809274336], [-115.92938988349292, 38.534294809274336], [-115.92938988349292, 32.08296982365502], [-123.40515640309, 32.08296982365502]] # Create the viz from the dataframe viz = ImageViz(img_url, coordinates, access_token=token, height='600px', center=(-119, 35), zoom=5, below_layer='waterway-label') viz.show() # ## Choose a colormap # In[ ]: from matplotlib import cm img = imread(img_url) img = numpy.mean(img[::10, ::10], axis=2) img = cm.magma(img) # Coordinates must be an array in the form of [UL, UR, LR, LL] coordinates = [[-123.40515640309, 38.534294809274336], [-115.92938988349292, 38.534294809274336], [-115.92938988349292, 32.08296982365502], [-123.40515640309, 32.08296982365502]] # Create the viz from the dataframe viz = ImageViz(img_url, coordinates, access_token=token, height='600px', center=(-119, 35), zoom=5, below_layer='waterway-label') viz.show()