{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Mapboxgl Python Library for location data visualization\n",
    "\n",
    "https://github.com/mapbox/mapboxgl-jupyter\n",
    "\n",
    "### Requirements\n",
    "\n",
    "These examples require the installation of the following python modules\n",
    "\n",
    "```\n",
    "pip install mapboxgl\n",
    "```\n",
    "\n",
    "### Notes\n",
    "\n",
    "`ImageViz` object accepts either an url, a local path or a numpy ndarray data as input for an image source"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "import os\n",
    "import numpy\n",
    "from matplotlib.pyplot import imread\n",
    "from mapboxgl.viz import ImageViz"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Set your Mapbox access token.  \n",
    "Set a  `MAPBOX_ACCESS_TOKEN` environment variable or copy/paste your token  \n",
    "If you do not have a Mapbox access token, sign up for an account at https://www.mapbox.com/  \n",
    "If you already have an account, you can grab your token at https://www.mapbox.com/account/"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# Must be a public token, starting with `pk`\n",
    "token = os.getenv('MAPBOX_ACCESS_TOKEN')"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Display an image given an URL"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "scrolled": false
   },
   "outputs": [],
   "source": [
    "img_url = 'https://raw.githubusercontent.com/mapbox/mapboxgl-jupyter/master/examples/data/mosaic.png'\n",
    "\n",
    "# Coordinates must be an array in the form of [UL, UR, LR, LL]\n",
    "coordinates = [[-123.40515640309, 38.534294809274336],\n",
    "               [-115.92938988349292, 38.534294809274336],\n",
    "               [-115.92938988349292, 32.08296982365502], \n",
    "               [-123.40515640309, 32.08296982365502]]\n",
    "\n",
    "# Create the viz from the dataframe\n",
    "viz = ImageViz(img_url, \n",
    "               coordinates, \n",
    "               access_token=token,\n",
    "               height='600px',\n",
    "               center=(-119, 35),\n",
    "               zoom=5,\n",
    "               below_layer='waterway-label')\n",
    "viz.show()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Display an image given a numpy.ndarray"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "scrolled": false
   },
   "outputs": [],
   "source": [
    "img = imread(img_url)\n",
    "img = numpy.mean(img[::10, ::10], axis=2)\n",
    "\n",
    "# Coordinates must be an array in the form of [UL, UR, LR, LL]\n",
    "coordinates = [[-123.40515640309, 38.534294809274336],\n",
    "               [-115.92938988349292, 38.534294809274336],\n",
    "               [-115.92938988349292, 32.08296982365502], \n",
    "               [-123.40515640309, 32.08296982365502]]\n",
    "\n",
    "# Create the viz from the dataframe\n",
    "viz = ImageViz(img, \n",
    "               coordinates, \n",
    "               access_token=token,\n",
    "               height='600px',\n",
    "               center=(-119, 35),\n",
    "               zoom=5,\n",
    "               below_layer='waterway-label')\n",
    "viz.show()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Display an image given a local path"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "scrolled": false
   },
   "outputs": [],
   "source": [
    "# Coordinates must be an array in the form of [UL, UR, LR, LL]\n",
    "coordinates = [[-123.40515640309, 38.534294809274336],\n",
    "               [-115.92938988349292, 38.534294809274336],\n",
    "               [-115.92938988349292, 32.08296982365502], \n",
    "               [-123.40515640309, 32.08296982365502]]\n",
    "\n",
    "# Create the viz from the dataframe\n",
    "viz = ImageViz(img_url, \n",
    "               coordinates, \n",
    "               access_token=token,\n",
    "               height='600px',\n",
    "               center=(-119, 35),\n",
    "               zoom=5,\n",
    "               below_layer='waterway-label')\n",
    "viz.show()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Choose a colormap"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from matplotlib import cm\n",
    "\n",
    "img = imread(img_url)\n",
    "img = numpy.mean(img[::10, ::10], axis=2)\n",
    "img = cm.magma(img)\n",
    "\n",
    "# Coordinates must be an array in the form of [UL, UR, LR, LL]\n",
    "coordinates = [[-123.40515640309, 38.534294809274336],\n",
    "               [-115.92938988349292, 38.534294809274336],\n",
    "               [-115.92938988349292, 32.08296982365502], \n",
    "               [-123.40515640309, 32.08296982365502]]\n",
    "\n",
    "# Create the viz from the dataframe\n",
    "viz = ImageViz(img_url, \n",
    "               coordinates, \n",
    "               access_token=token,\n",
    "               height='600px',\n",
    "               center=(-119, 35),\n",
    "               zoom=5,\n",
    "               below_layer='waterway-label')\n",
    "viz.show()"
   ]
  }
 ],
 "metadata": {
  "anaconda-cloud": {
   "attach-environment": true,
   "environment": "Root",
   "summary": "Mapboxgl Python Data Visualization example"
  },
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.1"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 1
}
