-
Notifications
You must be signed in to change notification settings - Fork 1
/
maps.html
260 lines (221 loc) · 9.63 KB
/
maps.html
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
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8" />
<title>Minnesota Sample Data</title>
<link rel="stylesheet" href="https://unpkg.com/leaflet@1.2.0/dist/leaflet.css" />
<script src="https://unpkg.com/leaflet@1.2.0/dist/leaflet.js"></script>
<style lang="text/css">
.map
{
display: block;
float: left;
width: 48%;
height: 350px;
border: 1px solid #93A1A1;
margin: 0 1em 1em 0;
}
</style>
</head>
<body>
<p>
<button onclick="return show_race()">Show Non-White</button>
<button onclick="return show_age()">Show Young Population</button>
<button onclick="return show_voting()">Show Party Votes</button>
<a href="https://github.com/gerrymandr/sample-graph-data">
<img src="https://packagecontrol.io/readmes/img/a59a44b1a383ad42e195fa34f0ad2756f46c77a2.png" width="20" height="20" valign="top">
gerrymandr/sample-graph-data on Github
</a>
</p>
<div id="maps"></div>
<script src="geographies.js"></script>
<script>
var plans = [
[1, 2, 2, 2, 2, 2, 2, 2, 2, 1, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2],
[1, 2, 2, 2, 2, 2, 2, 1, 2, 1, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2],
[1, 2, 2, 2, 2, 2, 2, 2, 1, 1, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2],
[1, 1, 1, 2, 1, 1, 1, 1, 1, 2, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 2],
[1, 2, 2, 1, 2, 2, 2, 2, 2, 1, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2]
];
function partisan_style(feature)
{
// Add up votes for various statewide races 2002-2010.
// See http://www.gis.leg.mn/metadata/redist2010.htm
var share = (
feature.properties['USSENDFL02'] + feature.properties['GOVDFL02']
+ feature.properties['SOSDFL02'] + feature.properties['USSENDFL06']
+ feature.properties['GOVDFL06'] + feature.properties['SOSDFL06']
+ feature.properties['USSENDFL08'] + feature.properties['GOVDFL10']
+ feature.properties['SOSDFL10']
) / (
feature.properties['USSENR02'] + feature.properties['GOVR02']
+ feature.properties['SOSR02'] + feature.properties['USSENR06']
+ feature.properties['GOVR06'] + feature.properties['SOSR06']
+ feature.properties['USSENR08'] + feature.properties['GOVR10']
+ feature.properties['SOSR10']
);
// http://soliton.vm.bytemark.co.uk/pub/cpt-city/cb/div/tn/RdBu_06.png.index.html
if(share < 0.65) {
var color = 'rgb(178,24,43)';
} else if(share < 0.90) {
var color = 'rgb(239,138,98)';
} else if(share < 1.00) {
var color = 'rgb(253,219,199)';
} else if(share < 1.10) {
var color = 'rgb(209,229,240)';
} else if(share < 1.50) {
var color = 'rgb(103,169,207)';
} else {
var color = 'rgb(33,102,172)';
}
return { weight: 1, color: '#fff', fillOpacity: .5, fillColor: color };
}
function age_style(feature)
{
// See http://www.gis.leg.mn/metadata/redist2010.htm
var voting_age = feature.properties['F18_POP'] / feature.properties['POPULATION'];
// http://soliton.vm.bytemark.co.uk/pub/cpt-city/cb/seq/tn/YlGn_05.png.index.html
if(voting_age < 0.63) {
var color = 'rgb(0,104,55)';
} else if(voting_age < 0.70) {
var color = 'rgb(49,163,84)';
} else if(voting_age < 0.76) {
var color = 'rgb(120,198,121)';
} else if(voting_age < 0.82) {
var color = 'rgb(194,230,153)';
} else {
var color = 'rgb(255,255,204)';
}
return { weight: 1, color: '#fff', fillOpacity: .5, fillColor: color };
}
function racial_style(feature)
{
// See http://www.gis.leg.mn/metadata/redist2010.htm
var whiteness = feature.properties['NH_WHT'] / feature.properties['POPULATION'];
// http://soliton.vm.bytemark.co.uk/pub/cpt-city/cb/seq/tn/RdPu_05.png.index.html
if(whiteness < 0.25) {
var color = 'rgb(122,1,119)';
} else if(whiteness < 0.43) {
var color = 'rgb(197,27,138)';
} else if(whiteness < 0.61) {
var color = 'rgb(247,104,161)';
} else if(whiteness < 0.78) {
var color = 'rgb(251,180,185)';
} else {
var color = 'rgb(254,235,226)';
}
return { weight: 1, color: '#fff', fillOpacity: .5, fillColor: color };
}
/**
* Create a new GeoJSON object based on a plan array.
*/
function collect_districts(plan, input_geojson)
{
if(plan.length != input_geojson.features.length)
{
throw Error('Plan length mismatch');
}
var districts = {},
output_geojson = {'type': 'FeatureCollection', 'features': []};
for(var i = 0; i < plan.length; i++)
{
var district_id = plan[i],
in_properties = input_geojson.features[i].properties,
in_geometry = input_geojson.features[i].geometry;
// Better initialize a district feature
if(!districts.hasOwnProperty(district_id))
{
districts[district_id] = {'type': 'Feature', 'properties': {},
'geometry': {'type': 'MultiPolygon', 'coordinates': []}};
}
var out_properties = districts[district_id].properties,
out_geometry = districts[district_id].geometry;
// Add numeric feature properties to district properties
for(var p in in_properties)
{
if(typeof in_properties[p] != 'number')
{
continue;
}
if(!out_properties.hasOwnProperty(p))
{
out_properties[p] = 0;
}
out_properties[p] += in_properties[p];
}
// Add feature geometry coordinates to district geometry coordinates
switch(in_geometry.type)
{
case 'Polygon':
out_geometry.coordinates.push(in_geometry.coordinates);
break;
case 'MultiPolygon':
out_geometry.coordinates = out_geometry.coordinates.concat(in_geometry.coordinates);
break;
}
}
// Add each district feature to output feature list
for(var p in districts)
{
output_geojson.features.push(districts[p]);
}
return output_geojson;
}
var geojsons = [],
maps_div = document.getElementById('maps');
for(var i = 0; i < plans.length; i++)
{
var div = document.createElement('div');
maps_div.appendChild(div);
div.className = 'map';
var geojson = L.geoJSON(collect_districts(plans[i], mcd2010_slice), {
style: { weight: 1, color: '#fff', fillOpacity: .5, fillColor: '#f90' },
smoothFactor: 0.7
});
var map = L.map(div, {
scrollWheelZoom: false,
center: [0, 0],
zoom: 8
});
var pane = map.createPane('labels');
pane.style.zIndex = 650; // http://leafletjs.com/examples/map-panes/
pane.style.pointerEvents = 'none';
// Add Toner tiles for base map
L.tileLayer('https://cartodb-basemaps-{s}.global.ssl.fastly.net/light_nolabels/{z}/{x}/{y}{r}.png', {
attribution: '©<a href="http://www.openstreetmap.org/copyright">OpenStreetMap</a>, ©<a href="https://carto.com/attribution">CARTO</a>',
maxZoom: 18
}).addTo(map);
// Add a GeoJSON layer and fit it into view
geojson.addTo(map);
map.fitBounds(geojson.getBounds());
// Add Toner label tiles for base map
L.tileLayer('https://stamen-tiles.a.ssl.fastly.net/toner-labels/{z}/{x}/{y}{r}.png', {
attribution: '©<a href="http://stamen.com/">Stamen</a>',
pane: 'labels',
maxZoom: 18
}).addTo(map);
geojsons.push(geojson);
}
function apply_style(style_function)
{
for(var i = 0; i < geojsons.length; i++)
{
geojsons[i].setStyle(style_function);
}
}
function show_race()
{
apply_style(racial_style);
}
function show_age()
{
apply_style(age_style);
}
function show_voting()
{
apply_style(partisan_style);
}
show_voting();
</script>
</body>
</html>