{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# Mapboxgl Python Library for location data visualization\n", "\n", "https://github.com/mapbox/mapboxgl-jupyter\n", "\n", "\n", "# Linestring Visualizations" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "import random\n", "import os\n", "\n", "from mapboxgl.viz import LinestringViz\n", "from mapboxgl.utils import create_color_stops, create_numeric_stops\n", "\n", "# Must be a public token, starting with `pk`\n", "token = os.getenv('MAPBOX_ACCESS_TOKEN')\n", "\n", "# JSON join-data object\n", "data = [{\"elevation\": x, \"weight\": random.randint(0,100)} for x in range(0, 21000, 10)]\n", "\n", "# GeoJSON data object\n", "geojson = {\n", " \"type\": \"FeatureCollection\",\n", " \"features\": [{\n", " \"type\": \"Feature\",\n", " \"id\": \"01\", \n", " \"properties\": {\"sample\": 50, \"weight\": 1}, \n", " \"geometry\": {\n", " \"type\": \"LineString\",\n", " \"coordinates\": [\n", " [-122.4833858013153, 37.829607404976734],\n", " [-122.4830961227417, 37.82932776098012],\n", " [-122.4830746650696, 37.82932776098012],\n", " [-122.48218417167662, 37.82889558180985],\n", " [-122.48218417167662, 37.82890193740421],\n", " [-122.48221099376678, 37.82868372835086],\n", " [-122.4822163581848, 37.82868372835086],\n", " [-122.48205006122589, 37.82801003030873]\n", " ]\n", " }\n", " }, {\n", " \"type\": \"Feature\",\n", " \"id\": \"02\",\n", " \"properties\": {\"sample\": 500, \"weight\": 2},\n", " \"geometry\": {\n", " \"type\": \"LineString\",\n", " \"coordinates\": [\n", " [-122.4833858013153, 37.929607404976734],\n", " [-122.4830961227417, 37.83]\n", " ]\n", " }\n", " }, {\n", " \"type\": \"Feature\",\n", " \"properties\": {\"sample\": 5000, \"weight\": 1},\n", " \"geometry\": {\n", " \"type\": \"LineString\",\n", " \"coordinates\": [\n", " [-122.48369693756104, 37.83381888486939],\n", " [-122.48348236083984, 37.83317489144141],\n", " [-122.48339653015138, 37.83270036637107],\n", " [-122.48356819152832, 37.832056363179625],\n", " [-122.48404026031496, 37.83114119107971],\n", " [-122.48404026031496, 37.83049717427869],\n", " [-122.48348236083984, 37.829920943955045],\n", " [-122.48356819152832, 37.82954808664175],\n", " [-122.48507022857666, 37.82944639795659],\n", " [-122.48610019683838, 37.82880236636284],\n", " [-122.48695850372314, 37.82931081282506],\n", " [-122.48700141906738, 37.83080223556934],\n", " [-122.48751640319824, 37.83168351665737],\n", " [-122.48803138732912, 37.832158048267786],\n", " [-122.48888969421387, 37.83297152392784],\n", " [-122.48987674713133, 37.83263257682617],\n", " [-122.49043464660643, 37.832937629287755],\n", " [-122.49125003814696, 37.832429207817725],\n", " [-122.49163627624512, 37.832564787218985],\n", " [-122.49223709106445, 37.83337825839438],\n", " [-122.49378204345702, 37.83368330777276]\n", " ]\n", " }\n", " }]\n", "}" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## GeoJSON Test Linestring Source" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "scrolled": false }, "outputs": [], "source": [ "# make viz with GeoJSON source\n", "viz = LinestringViz(geojson, \n", " access_token=token,\n", " color_property='sample',\n", " color_stops=create_color_stops([0, 50, 100, 500, 1500], colors='Blues'),\n", " line_stroke='--',\n", " line_width_property='weight',\n", " line_width_stops=create_numeric_stops([0, 1, 2, 3, 4, 5], 0, 10),\n", " opacity=0.8,\n", " center=(-122.48, 37.83),\n", " zoom=12,\n", " below_layer='waterway-label')\n", "viz.show()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Vector Linestring Source (Topography)" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "scrolled": false }, "outputs": [], "source": [ "viz = LinestringViz(data, \n", " access_token=token,\n", " vector_url='mapbox://mapbox.mapbox-terrain-v2',\n", " vector_layer_name='contour',\n", " vector_join_property='ele',\n", " data_join_property='elevation',\n", " color_property='elevation',\n", " color_stops=create_color_stops([0, 50, 100, 200, 300], colors='YlOrRd'),\n", " line_width_stops=create_numeric_stops([0, 50, 100, 200, 300], 0.1, 4),\n", " line_width_property='elevation',\n", " line_width_function_type='interpolate',\n", " line_width_default='1',\n", " opacity=0.8,\n", " center=(-122.48, 37.83),\n", " zoom=13,\n", " below_layer='waterway-label')\n", "viz.show()" ] } ], "metadata": { "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": 2 }