-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathchoropleth_example.py
170 lines (135 loc) · 4.47 KB
/
choropleth_example.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
# -*- coding: utf-8 -*-
"""
Choropleth example with Dash
Example using data on Limited English Proficiency in Portland.
Shapefile taken from:
https://gis-pdx.opendata.arcgis.com/datasets/a0e5ed95749d4181abfb2a7a2c98d7ef_121
"""
import json
import dash
import dash_core_components as dcc
import dash_html_components as html
import geopandas as gpd
import matplotlib
import matplotlib.cm as cm
mapbox_key = None
if not mapbox_key:
raise RuntimeError("Mapbox key not specified! Edit this file and add it.")
# Example shapefile from:
# https://gis-pdx.opendata.arcgis.com/datasets/a0e5ed95749d4181abfb2a7a2c98d7ef_121
# Portland Limited English Proficiency
lep_shp = 'data/lep/Limited_English_Proficiency.shp'
lep_df = gpd.read_file(lep_shp)
# Generate centroids for each polygon to use as marker locations
lep_df['lon_lat'] = lep_df['geometry'].apply(lambda row: row.centroid)
lep_df['LON'] = lep_df['lon_lat'].apply(lambda row: row.x)
lep_df['LAT'] = lep_df['lon_lat'].apply(lambda row: row.y)
lep_df = lep_df.drop('lon_lat', axis=1)
lon = lep_df['LON'][0]
lat = lep_df['LAT'][0]
# Get list of languages given in the shapefile
langs = [lng for lng in lep_df.columns
if lng.istitle() and
lng not in ['Id', 'Id2', 'Total_Pop_', 'Geography'] and
'Shape' not in lng]
# Generate stats for example
lep_df['NUM_LEP'] = lep_df[langs].sum(axis=1)
# Create hover info text
lep_df['HOVER'] = 'Geography: ' + lep_df.Geography + \
'<br /> Num. LEP:' + lep_df.NUM_LEP.astype(str)
mcolors = matplotlib.colors
def set_overlay_colors(dataset):
"""Create overlay colors based on values
:param dataset: gpd.Series, array of values to map colors to
:returns: dict, hex color value for each language or index value
"""
minima = dataset.min()
maxima = dataset.max()
norm = mcolors.Normalize(vmin=minima, vmax=maxima, clip=True)
mapper = cm.ScalarMappable(norm=norm, cmap=cm.inferno)
colors = [mcolors.to_hex(mapper.to_rgba(v)) for v in dataset]
overlay_color = {
idx: shade
for idx, shade in zip(dataset.index, colors)
}
return overlay_color
# End set_overlay_colors()
# Create layer options that get displayed to the user in the dropdown
all_opt = {'label': 'All', 'value': 'All'}
opts = [{'label': lng.title(), 'value': lng} for lng in langs]
opts.append(all_opt)
# template for map
map_layout = {
'title': 'Portland LEP',
'data': [{
'lon': lep_df['LON'],
'lat': lep_df['LAT'],
'mode': 'markers',
'marker': {
'opacity': 0.0,
},
'type': 'scattermapbox',
'name': 'Portland LEP',
'text': lep_df['HOVER'],
'hoverinfo': 'text',
'showlegend': True,
}],
'layout': {
'autosize': True,
'hovermode': 'closest',
'margin': {'l': 0, 'r': 0, 'b': 0, 't': 0},
'mapbox': {
'accesstoken': mapbox_key,
'center': {
'lat': lat,
'lon': lon
},
'zoom': 8.0,
'bearing': 0.0,
'pitch': 0.0,
},
}
}
app = dash.Dash()
app.layout = html.Div([
html.H1(children='Portland - Limited English Proficiency (Choropleth Example)'),
dcc.Dropdown(
id='overlay-choice',
options=opts,
value='All'
),
html.Div([
dcc.Graph(id='map-display'),
])
])
@app.callback(
dash.dependencies.Output('map-display', 'figure'),
[dash.dependencies.Input('overlay-choice', 'value')])
def update_map(overlay_choice):
tmp = map_layout.copy()
if overlay_choice == 'All':
dataset = lep_df
colors = set_overlay_colors(lep_df.NUM_LEP)
tmp['data'][0]['text'] = lep_df['HOVER']
else:
dataset = lep_df.loc[lep_df[overlay_choice] > 0, :]
colors = set_overlay_colors(dataset[overlay_choice])
# Update hovertext display
hovertext = lep_df['Geography'].str.cat(
lep_df[overlay_choice].astype(str), sep=': ')
tmp['data'][0]['text'] = hovertext
# End if
# Create a layer for each region colored by LEP value
layers = [{
'name': overlay_choice,
'source': json.loads(dataset.loc[dataset.index == i, :].to_json()),
'sourcetype': 'geojson',
'type': 'fill',
'opacity': 1.0,
'color': colors[i]
} for i in dataset.index]
tmp['layout']['mapbox']['layers'] = layers
return tmp
# End update_map()
if __name__ == '__main__':
app.run_server(debug=True, port=8051)