-
Notifications
You must be signed in to change notification settings - Fork 135
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Add basic chloropleth support * Center zoom on click coordinates; rename polygons layer to 'cholorpleth-fill' per PR review comments * Add line_color, line_stroke, and line_width properties to ChloroplethViz; update url transformRequest function in map init to match implementation in master * Add chloropleth label layer; add template block for styling legend keys * Add line styling to example chloropleth notebook * Update internal documentation in ChloroplethViz class; add chloropleth documentation to docs-markdown/viz.md template * Update spelling to mapbox standard: 'choropleth' * Add tests for ChoroplethViz (currently uses polygons.geojson) * Formatting and headers on choropleth notebook * Add vector layer support options; requesting direction for reconciling color assignment with data join and Mapboxgl-jupyter color_stops paradigm * Single ChoroplethViz class defines either vector-based or geojson-based viz templates depending on variables in init * Add support for categorical data-driven styling with vector data-join; include helper function for interpolating (mapping) colors when using vector-join technique; update docs and example notebook for vector data support in ChoroplethViz * Attempt to add test for utils.rgb_tuple_from_str * Additional test coverage for ChoroplethViz and color_map, rgb_tuple_from_str utils * Per @ryanbaumann 's comments: revert color data-join style to the old Mapboxgl property functions (faster) and add layer-filter * Test fix * Minor refactor to color_map to prioritize returning exact match color from color_stops
- Loading branch information
1 parent
cd45841
commit 059e39b
Showing
14 changed files
with
1,284 additions
and
21 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,191 @@ | ||
{ | ||
"cells": [ | ||
{ | ||
"cell_type": "markdown", | ||
"metadata": {}, | ||
"source": [ | ||
"# Mapboxgl Python Library\n", | ||
"\n", | ||
"https://github.com/mapbox/mapboxgl-jupyter" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": null, | ||
"metadata": {}, | ||
"outputs": [], | ||
"source": [ | ||
"import os\n", | ||
"from mapboxgl.viz import *\n", | ||
"from mapboxgl.utils import *\n", | ||
"\n", | ||
"# Must be a public token, starting with `pk`\n", | ||
"token = os.getenv('MAPBOX_ACCESS_TOKEN')" | ||
] | ||
}, | ||
{ | ||
"cell_type": "markdown", | ||
"metadata": {}, | ||
"source": [ | ||
"## Choropleths with interpolated color assignment from GeoJSON source" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": null, | ||
"metadata": { | ||
"scrolled": true | ||
}, | ||
"outputs": [], | ||
"source": [ | ||
"# create choropleth from polygon features stored as GeoJSON\n", | ||
"viz = ChoroplethViz('us-states.geojson', \n", | ||
" color_property='density',\n", | ||
" color_stops=create_color_stops([0, 50, 100, 500, 1500], colors='YlOrRd'),\n", | ||
" color_function_type='interpolate',\n", | ||
" line_stroke='--',\n", | ||
" line_color='rgb(128,0,38)',\n", | ||
" line_width=1,\n", | ||
" opacity=0.8,\n", | ||
" center=(-96, 37.8),\n", | ||
" zoom=3,\n", | ||
" below_layer='waterway-label'\n", | ||
" )\n", | ||
"viz.show()" | ||
] | ||
}, | ||
{ | ||
"cell_type": "markdown", | ||
"metadata": {}, | ||
"source": [ | ||
"## Choropleths with match-type color scheme from GeoJSON source" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": null, | ||
"metadata": {}, | ||
"outputs": [], | ||
"source": [ | ||
"match_color_stops = [\n", | ||
" ['Massachusetts', 'rgb(46,204,113)'],\n", | ||
" ['Utah', 'rgb(231,76,60)'],\n", | ||
" ['California', 'rgb(142,68,173)'],\n", | ||
"]\n", | ||
"\n", | ||
"viz = ChoroplethViz('us-states.geojson', \n", | ||
" color_property='name', \n", | ||
" color_stops = match_color_stops, \n", | ||
" color_function_type = 'match', \n", | ||
" color_default = 'rgba(52,73,94,0.5)', \n", | ||
" opacity=0.8, \n", | ||
" center = (-96, 37.8), \n", | ||
" zoom = 3, \n", | ||
" below_layer = 'waterway-label'\n", | ||
" )\n", | ||
"viz.show()" | ||
] | ||
}, | ||
{ | ||
"cell_type": "markdown", | ||
"metadata": {}, | ||
"source": [ | ||
"## Vector polygon source with data-join technique\n", | ||
"\n", | ||
"In this configuration, properties in JSON data are used to calculate colors to style polygons from the vector source." | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": null, | ||
"metadata": {}, | ||
"outputs": [], | ||
"source": [ | ||
"# must be JSON object (need to extend to use referenced JSON file)\n", | ||
"data = [{\"id\": \"01\", \"name\": \"Alabama\", \"density\": 94.65}, {\"id\": \"02\", \"name\": \"Alaska\", \"density\": 1.264}, {\"id\": \"04\", \"name\": \"Arizona\", \"density\": 57.05}, {\"id\": \"05\", \"name\": \"Arkansas\", \"density\": 56.43}, {\"id\": \"06\", \"name\": \"California\", \"density\": 241.7}, {\"id\": \"08\", \"name\": \"Colorado\", \"density\": 49.33}, {\"id\": \"09\", \"name\": \"Connecticut\", \"density\": 739.1}, {\"id\": \"10\", \"name\": \"Delaware\", \"density\": 464.3}, {\"id\": \"11\", \"name\": \"District of Columbia\", \"density\": 10065}, {\"id\": \"12\", \"name\": \"Florida\", \"density\": 353.4}, {\"id\": \"13\", \"name\": \"Georgia\", \"density\": 169.5}, {\"id\": \"15\", \"name\": \"Hawaii\", \"density\": 214.1}, {\"id\": \"16\", \"name\": \"Idaho\", \"density\": 19.15}, {\"id\": \"17\", \"name\": \"Illinois\", \"density\": 231.5}, {\"id\": \"18\", \"name\": \"Indiana\", \"density\": 181.7}, {\"id\": \"19\", \"name\": \"Iowa\", \"density\": 54.81}, {\"id\": \"20\", \"name\": \"Kansas\", \"density\": 35.09}, {\"id\": \"21\", \"name\": \"Kentucky\", \"density\": 110}, {\"id\": \"22\", \"name\": \"Louisiana\", \"density\": 105}, {\"id\": \"23\", \"name\": \"Maine\", \"density\": 43.04}, {\"id\": \"24\", \"name\": \"Maryland\", \"density\": 596.3}, {\"id\": \"25\", \"name\": \"Massachusetts\", \"density\": 840.2}, {\"id\": \"26\", \"name\": \"Michigan\", \"density\": 173.9}, {\"id\": \"27\", \"name\": \"Minnesota\", \"density\": 67.14}, {\"id\": \"28\", \"name\": \"Mississippi\", \"density\": 63.5}, {\"id\": \"29\", \"name\": \"Missouri\", \"density\": 87.26}, {\"id\": \"30\", \"name\": \"Montana\", \"density\": 6.858}, {\"id\": \"31\", \"name\": \"Nebraska\", \"density\": 23.97}, {\"id\": \"32\", \"name\": \"Nevada\", \"density\": 24.8}, {\"id\": \"33\", \"name\": \"New Hampshire\", \"density\": 147}, {\"id\": \"34\", \"name\": \"New Jersey\", \"density\": 1189}, {\"id\": \"35\", \"name\": \"New Mexico\", \"density\": 17.16}, {\"id\": \"36\", \"name\": \"New York\", \"density\": 412.3}, {\"id\": \"37\", \"name\": \"North Carolina\", \"density\": 198.2}, {\"id\": \"38\", \"name\": \"North Dakota\", \"density\": 9.916}, {\"id\": \"39\", \"name\": \"Ohio\", \"density\": 281.9}, {\"id\": \"40\", \"name\": \"Oklahoma\", \"density\": 55.22}, {\"id\": \"41\", \"name\": \"Oregon\", \"density\": 40.33}, {\"id\": \"42\", \"name\": \"Pennsylvania\", \"density\": 284.3}, {\"id\": \"44\", \"name\": \"Rhode Island\", \"density\": 1006}, {\"id\": \"45\", \"name\": \"South Carolina\", \"density\": 155.4}, {\"id\": \"46\", \"name\": \"South Dakota\", \"density\": 98.07}, {\"id\": \"47\", \"name\": \"Tennessee\", \"density\": 88.08}, {\"id\": \"48\", \"name\": \"Texas\", \"density\": 98.07}, {\"id\": \"49\", \"name\": \"Utah\", \"density\": 34.3}, {\"id\": \"50\", \"name\": \"Vermont\", \"density\": 67.73}, {\"id\": \"51\", \"name\": \"Virginia\", \"density\": 204.5}, {\"id\": \"53\", \"name\": \"Washington\", \"density\": 102.6}, {\"id\": \"54\", \"name\": \"West Virginia\", \"density\": 77.06}, {\"id\": \"55\", \"name\": \"Wisconsin\", \"density\": 105.2}, {\"id\": \"56\", \"name\": \"Wyoming\", \"density\": 5.851}, {\"id\": \"72\", \"name\": \"Puerto Rico\", \"density\": 1082}]\n", | ||
"\n", | ||
"# create choropleth map with vector source styling use data in JSON object\n", | ||
"viz = ChoroplethViz(data, \n", | ||
" vector_url='mapbox://mapbox.us_census_states_2015',\n", | ||
" vector_layer_name='states',\n", | ||
" vector_join_property='STATEFP',\n", | ||
" data_join_property='id',\n", | ||
" color_property='density',\n", | ||
" color_stops=create_color_stops([0, 50, 100, 500, 1500], colors='YlOrRd'),\n", | ||
" line_stroke='dashed',\n", | ||
" line_color='rgb(128,0,38)',\n", | ||
" opacity=0.8,\n", | ||
" center=(-96, 37.8),\n", | ||
" zoom=3,\n", | ||
" below_layer='waterway-label'\n", | ||
" )\n", | ||
"viz.show()" | ||
] | ||
}, | ||
{ | ||
"cell_type": "markdown", | ||
"metadata": {}, | ||
"source": [ | ||
"## Vector polygon source with data-join technique, categorical color scheme" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": null, | ||
"metadata": {}, | ||
"outputs": [], | ||
"source": [ | ||
"# must be JSON object (need to extend to use referenced JSON file)\n", | ||
"data = [{\"id\": \"01\", \"name\": \"Alabama\", \"density\": 94.65}, {\"id\": \"02\", \"name\": \"Alaska\", \"density\": 1.264}, {\"id\": \"04\", \"name\": \"Arizona\", \"density\": 57.05}, {\"id\": \"05\", \"name\": \"Arkansas\", \"density\": 56.43}, {\"id\": \"06\", \"name\": \"California\", \"density\": 241.7}, {\"id\": \"08\", \"name\": \"Colorado\", \"density\": 49.33}, {\"id\": \"09\", \"name\": \"Connecticut\", \"density\": 739.1}, {\"id\": \"10\", \"name\": \"Delaware\", \"density\": 464.3}, {\"id\": \"11\", \"name\": \"District of Columbia\", \"density\": 10065}, {\"id\": \"12\", \"name\": \"Florida\", \"density\": 353.4}, {\"id\": \"13\", \"name\": \"Georgia\", \"density\": 169.5}, {\"id\": \"15\", \"name\": \"Hawaii\", \"density\": 214.1}, {\"id\": \"16\", \"name\": \"Idaho\", \"density\": 19.15}, {\"id\": \"17\", \"name\": \"Illinois\", \"density\": 231.5}, {\"id\": \"18\", \"name\": \"Indiana\", \"density\": 181.7}, {\"id\": \"19\", \"name\": \"Iowa\", \"density\": 54.81}, {\"id\": \"20\", \"name\": \"Kansas\", \"density\": 35.09}, {\"id\": \"21\", \"name\": \"Kentucky\", \"density\": 110}, {\"id\": \"22\", \"name\": \"Louisiana\", \"density\": 105}, {\"id\": \"23\", \"name\": \"Maine\", \"density\": 43.04}, {\"id\": \"24\", \"name\": \"Maryland\", \"density\": 596.3}, {\"id\": \"25\", \"name\": \"Massachusetts\", \"density\": 840.2}, {\"id\": \"26\", \"name\": \"Michigan\", \"density\": 173.9}, {\"id\": \"27\", \"name\": \"Minnesota\", \"density\": 67.14}, {\"id\": \"28\", \"name\": \"Mississippi\", \"density\": 63.5}, {\"id\": \"29\", \"name\": \"Missouri\", \"density\": 87.26}, {\"id\": \"30\", \"name\": \"Montana\", \"density\": 6.858}, {\"id\": \"31\", \"name\": \"Nebraska\", \"density\": 23.97}, {\"id\": \"32\", \"name\": \"Nevada\", \"density\": 24.8}, {\"id\": \"33\", \"name\": \"New Hampshire\", \"density\": 147}, {\"id\": \"34\", \"name\": \"New Jersey\", \"density\": 1189}, {\"id\": \"35\", \"name\": \"New Mexico\", \"density\": 17.16}, {\"id\": \"36\", \"name\": \"New York\", \"density\": 412.3}, {\"id\": \"37\", \"name\": \"North Carolina\", \"density\": 198.2}, {\"id\": \"38\", \"name\": \"North Dakota\", \"density\": 9.916}, {\"id\": \"39\", \"name\": \"Ohio\", \"density\": 281.9}, {\"id\": \"40\", \"name\": \"Oklahoma\", \"density\": 55.22}, {\"id\": \"41\", \"name\": \"Oregon\", \"density\": 40.33}, {\"id\": \"42\", \"name\": \"Pennsylvania\", \"density\": 284.3}, {\"id\": \"44\", \"name\": \"Rhode Island\", \"density\": 1006}, {\"id\": \"45\", \"name\": \"South Carolina\", \"density\": 155.4}, {\"id\": \"46\", \"name\": \"South Dakota\", \"density\": 98.07}, {\"id\": \"47\", \"name\": \"Tennessee\", \"density\": 88.08}, {\"id\": \"48\", \"name\": \"Texas\", \"density\": 98.07}, {\"id\": \"49\", \"name\": \"Utah\", \"density\": 34.3}, {\"id\": \"50\", \"name\": \"Vermont\", \"density\": 67.73}, {\"id\": \"51\", \"name\": \"Virginia\", \"density\": 204.5}, {\"id\": \"53\", \"name\": \"Washington\", \"density\": 102.6}, {\"id\": \"54\", \"name\": \"West Virginia\", \"density\": 77.06}, {\"id\": \"55\", \"name\": \"Wisconsin\", \"density\": 105.2}, {\"id\": \"56\", \"name\": \"Wyoming\", \"density\": 5.851}, {\"id\": \"72\", \"name\": \"Puerto Rico\", \"density\": 1082}]\n", | ||
"\n", | ||
"match_color_stops = [\n", | ||
" ['Massachusetts', 'rgb(46,204,113)'],\n", | ||
" ['Utah', 'rgb(231,76,60)'],\n", | ||
" ['California', 'rgb(142,68,173)'],\n", | ||
"]\n", | ||
"\n", | ||
"# create choropleth map with vector source styling use data in JSON object\n", | ||
"viz = ChoroplethViz(data, \n", | ||
" vector_url='mapbox://mapbox.us_census_states_2015',\n", | ||
" vector_layer_name='states',\n", | ||
" vector_join_property='STATEFP',\n", | ||
" data_join_property='id',\n", | ||
" color_property='name',\n", | ||
" color_stops=match_color_stops,\n", | ||
" color_default = 'rgba(52,73,94,0.5)', \n", | ||
" opacity=0.8,\n", | ||
" center=(-96, 37.8),\n", | ||
" zoom=3,\n", | ||
" below_layer='waterway-label'\n", | ||
" )\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 | ||
} |
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.