Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add choropleth support #40

Merged
merged 20 commits into from
Mar 29, 2018
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
20 commits
Select commit Hold shift + click to select a range
d3fcd8e
Add basic chloropleth support
Feb 3, 2018
d5a5435
Center zoom on click coordinates; rename polygons layer to 'cholorple…
Feb 19, 2018
07bf5e5
Add line_color, line_stroke, and line_width properties to Chloropleth…
Feb 20, 2018
3a5b3e6
Add chloropleth label layer; add template block for styling legend keys
Feb 20, 2018
bfd6a04
Add line styling to example chloropleth notebook
Feb 27, 2018
6b47dbc
Merge branch 'master' into chloropleth-support
Feb 27, 2018
79fd36a
Update internal documentation in ChloroplethViz class; add chloroplet…
Feb 27, 2018
360ef08
Update spelling to mapbox standard: 'choropleth'
Feb 28, 2018
ac8c100
Add tests for ChoroplethViz (currently uses polygons.geojson)
Feb 28, 2018
f1bcd21
Formatting and headers on choropleth notebook
Mar 2, 2018
9fa3853
Add vector layer support options; requesting direction for reconcilin…
Mar 6, 2018
b11c006
Single ChoroplethViz class defines either vector-based or geojson-bas…
Mar 17, 2018
722d747
Add support for categorical data-driven styling with vector data-join…
Mar 23, 2018
cb9972b
Merge branch 'master' into chloropleth-support; update styleUrl to st…
Mar 23, 2018
07b2ca7
Attempt to add test for utils.rgb_tuple_from_str
Mar 23, 2018
8084c7c
Merge branch 'master' into chloropleth-support
ryanbaumann Mar 27, 2018
cc147a0
Additional test coverage for ChoroplethViz and color_map, rgb_tuple_f…
Mar 29, 2018
29a3c36
Per @ryanbaumann 's comments: revert color data-join style to the old…
Mar 29, 2018
6af2548
Test fix
Mar 29, 2018
d80e99e
Minor refactor to color_map to prioritize returning exact match color…
Mar 29, 2018
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 18 additions & 26 deletions docs-markdown/viz.md
Original file line number Diff line number Diff line change
Expand Up @@ -282,30 +282,24 @@ viz.show()
![screen shot 2018-02-21 at 3 34 55 pm](https://user-images.githubusercontent.com/11286381/36511775-cfc4d794-171c-11e8-86b9-5f1a6060a387.png)


## class
## class ChoroplethViz

ChoroplethViz

The `

ChoroplethViz` object handles the creation of a choropleth map and is built on top of the `MapViz` class.
The `ChoroplethViz` object handles the creation of a choropleth map and inherits from the `MapViz` class. It applies a thematic map style to polygon features with color shading in proportion to the intensity of the data being displayed.

### Params
**

ChoroplethViz**(_data, label_property=None, color_property=None, color_stops=None, color_default='grey', color_function_type='interpolate', line_color='white', line_stroke='solid', line_width=1, *args, **kwargs_)
**ChoroplethViz**(_data, label_property=None, color_property=None, color_stops=None, color_default='grey', color_function_type='interpolate', line_color='white', line_stroke='solid', line_width=1, *args, **kwargs_)

Parameter | Description | Example
--|--|--
data | name of GeoJson file or object
data | GeoJson file name or object
label_property | property to use for marker label | "density"
color_property | property to determine circle color | "density"
color_stops | property to determine circle color | [[0, "red"], [0.5, "blue"], [1, "green"]]
color_default | property to determine default circle color if match lookup fails | "#F0F0F0"
color_function_type | property to determine `type` used by Mapbox to assign color | "interpolate"
line_color | property to determine choropleth line color | "#FFFFFF"
line_stroke | property to determine choropleth line stroke (one of solid, dashed, dotted, dash dot) | "solid"
line_width | property to determine choropleth line width | 1
color_property | property to determine fill color | "density"
color_stops | property to determine fill color | [[0, "red"], [0.5, "blue"], [1, "green"]]
color_default | property to determine default fill color in match lookups | "#F0F0F0"
color_function_type | property to determine type of expression used by Mapbox to assign color | "interpolate"
line_color | property to determine choropleth border line color | "#FFFFFF"
line_stroke | property to determine choropleth border line stroke (one of solid, dashed, dotted, dash dot) | "solid"
line_width | property to determine choropleth border line width | 1

[View options](https://github.com/mapbox/mapboxgl-jupyter/blob/master/docs-markdown/viz.md#params)

Expand All @@ -320,20 +314,18 @@ token = os.getenv('MAPBOX_ACCESS_TOKEN')
# Color stops
color_stops = [
[0.0, 'rgb(255,255,204)'],
[100.0, 'rgb(255,237,160)'],
[500.0, 'rgb(253,141,60)'],
[2000.0, 'rgb(227,26,28)'],
[5000.0, 'rgb(189,0,38)'],
[10000.0,'rgb(128,0,38)']
[50.0, 'rgb(255,237,160)'],
[100.0, 'rgb(253,141,60)'],
[500.0, 'rgb(227,26,28)'],
[2500.0, 'rgb(189,0,38)'],
[5000.0, 'rgb(128,0,38)']
]

# Create Choropleth
viz =

ChoroplethViz('us-states.geojson',
viz = ChoroplethViz('us-states.geojson',
access_token=token,
color_property='density',
color_stops=sample_color_stops,
color_stops=color_stops,
color_function_type='interpolate',
line_stroke='dashed',
line_color='rgb(128,0,38)',
Expand Down
150 changes: 17 additions & 133 deletions examples/choropleth-viz-example.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
"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')"
Expand All @@ -37,20 +38,11 @@
},
"outputs": [],
"source": [
"sample_color_stops = [\n",
" [0.0, 'rgb(255,255,204)'],\n",
" [50.0, 'rgb(255,237,160)'],\n",
" [100.0, 'rgb(253,141,60)'],\n",
" [500.0, 'rgb(227,26,28)'],\n",
" [2500.0, 'rgb(189,0,38)'],\n",
" [5000.0, 'rgb(128,0,38)']\n",
"]\n",
" \n",
"viz = ChoroplethViz('us-states.geojson', \n",
" color_property='density',\n",
" color_stops=sample_color_stops,\n",
" color_stops=create_color_stops([0, 50, 100, 500, 1500], colors='YlOrRd'),\n",
" color_function_type='interpolate',\n",
" line_stroke='dashed',\n",
" line_stroke='--',\n",
" line_color='rgb(128,0,38)',\n",
" line_width=1,\n",
" opacity=0.8,\n",
Expand Down Expand Up @@ -111,131 +103,23 @@
"\n",
"# Must be a public token, starting with `pk`\n",
"token = os.getenv('MAPBOX_ACCESS_TOKEN')\n",
"sample_color_stops = [\n",
" [0.0, 'rgb(255,255,204)'],\n",
" [50.0, 'rgb(255,237,160)'],\n",
" [100.0, 'rgb(253,141,60)'],\n",
" [500.0, 'rgb(227,26,28)'],\n",
" [2500.0, 'rgb(189,0,38)'],\n",
" [5000.0,'rgb(128,0,38)']\n",
"]\n",
"\n",
"# must be json object (need to extend to use referenced json file)\n",
"join_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",
"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",
"viz = AltChoroplethViz(None, \n",
" vector_polygon_source=True,\n",
" vector_url='mapbox://mapbox.us_census_states_2015',\n",
" vector_layer_name='states',\n",
" vector_join_color_property='STATEFP',\n",
" join_data=join_data,\n",
" data_join_property='id',\n",
" color_property='density',\n",
" color_stops=sample_color_stops,\n",
" line_stroke='dashed',\n",
" line_color='rgb(128,0,38)',\n",
" center=(-96, 37.8),\n",
" zoom=3,\n",
" below_layer='waterway-label'\n",
" )\n",
"viz.show()"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Pre-computed colors for styling polygons in vector layer"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"sample_color_stops = [\n",
" [0.0, 'rgb(255,255,204)'],\n",
" [50.0, 'rgb(255,237,160)'],\n",
" [100.0, 'rgb(253,141,60)'],\n",
" [500.0, 'rgb(227,26,28)'],\n",
" [2500.0, 'rgb(189,0,38)'],\n",
" [5000.0,'rgb(128,0,38)']\n",
"]\n",
"\n",
"# some pre-computed colors (avoid computing colors in JavaScript template)\n",
"color_stops = [\n",
" [\"Alabama\", \"rgba(255, 100, 94.65, 0.5)\"], \n",
" [\"Alaska\", \"rgba(255, 100, 1.264, 0.5)\"], \n",
" [\"Arizona\", \"rgba(255, 100, 57.05, 0.5)\"], \n",
" [\"Arkansas\", \"rgba(255, 100, 56.43, 0.5)\"], \n",
" [\"California\", \"rgba(255, 100, 241.7, 0.5)\"], \n",
" [\"Colorado\", \"rgba(255, 100, 49.33, 0.5)\"], \n",
" [\"Connecticut\", \"rgba(255, 100, 739.1, 0.5)\"], \n",
" [\"Delaware\", \"rgba(255, 100, 464.3, 0.5)\"], \n",
" [\"District of Columbia\", \"rgba(255, 100, 10065, 0.5)\"], \n",
" [\"Florida\", \"rgba(255, 100, 353.4, 0.5)\"], \n",
" [\"Georgia\", \"rgba(255, 100, 169.5, 0.5)\"], \n",
" [\"Hawaii\", \"rgba(255, 100, 214.1, 0.5)\"], \n",
" [\"Idaho\", \"rgba(255, 100, 19.15, 0.5)\"], \n",
" [\"Illinois\", \"rgba(255, 100, 231.5, 0.5)\"], \n",
" [\"Indiana\", \"rgba(255, 100, 181.7, 0.5)\"], \n",
" [\"Iowa\", \"rgba(255, 100, 54.81, 0.5)\"], \n",
" [\"Kansas\", \"rgba(255, 100, 35.09, 0.5)\"], \n",
" [\"Kentucky\", \"rgba(255, 100, 110, 0.5)\"], \n",
" [\"Louisiana\", \"rgba(255, 100, 105, 0.5)\"], \n",
" [\"Maine\", \"rgba(255, 100, 43.04, 0.5)\"], \n",
" [\"Maryland\", \"rgba(255, 100, 596.3, 0.5)\"], \n",
" [\"Massachusetts\", \"rgba(255, 100, 840.2, 0.5)\"], \n",
" [\"Michigan\", \"rgba(255, 100, 173.9, 0.5)\"], \n",
" [\"Minnesota\", \"rgba(255, 100, 67.14, 0.5)\"], \n",
" [\"Mississippi\", \"rgba(255, 100, 63.5, 0.5)\"], \n",
" [\"Missouri\", \"rgba(255, 100, 87.26, 0.5)\"], \n",
" [\"Montana\", \"rgba(255, 100, 6.858, 0.5)\"], \n",
" [\"Nebraska\", \"rgba(255, 100, 23.97, 0.5)\"], \n",
" [\"Nevada\", \"rgba(255, 100, 24.8, 0.5)\"], \n",
" [\"New Hampshire\", \"rgba(255, 100, 147, 0.5)\"], \n",
" [\"New Jersey\", \"rgba(255, 100, 1189, 0.5)\"], \n",
" [\"New Mexico\", \"rgba(255, 100, 17.16, 0.5)\"], \n",
" [\"New York\", \"rgba(255, 100, 412.3, 0.5)\"], \n",
" [\"North Carolina\", \"rgba(255, 100, 198.2, 0.5)\"], \n",
" [\"North Dakota\", \"rgba(255, 100, 9.916, 0.5)\"], \n",
" [\"Ohio\", \"rgba(255, 100, 281.9, 0.5)\"], \n",
" [\"Oklahoma\", \"rgba(255, 100, 55.22, 0.5)\"], \n",
" [\"Oregon\", \"rgba(255, 100, 40.33, 0.5)\"], \n",
" [\"Pennsylvania\", \"rgba(255, 100, 284.3, 0.5)\"], \n",
" [\"Rhode Island\", \"rgba(255, 100, 1006, 0.5)\"], \n",
" [\"South Carolina\", \"rgba(255, 100, 155.4, 0.5)\"], \n",
" [\"South Dakota\", \"rgba(255, 100, 98.07, 0.5)\"], \n",
" [\"Tennessee\", \"rgba(255, 100, 88.08, 0.5)\"], \n",
" [\"Texas\", \"rgba(255, 100, 98.07, 0.5)\"], \n",
" [\"Utah\", \"rgba(255, 100, 34.3, 0.5)\"], \n",
" [\"Vermont\", \"rgba(255, 100, 67.73, 0.5)\"], \n",
" [\"Virginia\", \"rgba(255, 100, 204.5, 0.5)\"], \n",
" [\"Washington\", \"rgba(255, 100, 102.6, 0.5)\"], \n",
" [\"West Virginia\", \"rgba(255, 100, 77.06, 0.5)\"], \n",
" [\"Wisconsin\", \"rgba(255, 100, 105.2, 0.5)\"], \n",
" [\"Wyoming\", \"rgba(255, 100, 5.851, 0.5)\"], \n",
" [\"Puerto Rico\", \"rgba(255, 100, 1082, 0.5)\"]\n",
"]\n",
"\n",
"viz = ChoroplethViz(None, \n",
" vector_polygon_source=True,\n",
" vector_url='mapbox://mapbox.us_census_states_2015',\n",
" vector_layer_name='states',\n",
" vector_color_stops=color_stops,\n",
" vector_join_color_property='NAME',\n",
" color_property='density',\n",
" color_stops=sample_color_stops,\n",
" color_function_type='interpolate',\n",
" line_stroke='dashed',\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 = ChoroplethViz(data, \n",
" vector_url='mapbox://mapbox.us_census_states_2015',\n",
" vector_layer_name='states',\n",
" vector_join_color_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",
" center=(-96, 37.8),\n",
" zoom=3,\n",
" below_layer='waterway-label'\n",
" )\n",
"viz.show()"
]
}
Expand Down
Loading