-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmembership-map.vue
More file actions
604 lines (566 loc) · 20.2 KB
/
Copy pathmembership-map.vue
File metadata and controls
604 lines (566 loc) · 20.2 KB
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
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
<template>
<v-container>
<layout-header title="Membership map" subtitle="Insights from library membership data" />
<v-divider inset color="info" class="my-2"></v-divider>
<markdown-section :markdownText="mdText" />
<v-divider inset color="info" class="my-2"></v-divider>
<h2 class="text-h5 text-decoration-underline my-3">Map library members</h2>
<v-sheet color="grey-lighten-5" rounded elevation="0" class="px-5 py-5">
<v-alert icon="mdi-numeric-1-circle" class="mb-1" title="Load a file">
First you need a CSV file in the format of the membership data schema.
</v-alert>
<file-upload v-bind:files="lsoaFiles" v-on:change-files="lsoaFiles = $event"
v-on:delete-file="lsoaFiles = null" />
<v-btn append-icon="mdi-map-marker-plus" color="info" class="mt-3" variant="tonal" size="large"
v-on:click="addMembershipData" :disabled="lsoaFiles.length == 0">
Display on map
</v-btn>
<v-alert class="mt-8 mb-4" icon="mdi-numeric-2-circle" title="Map">
<strong>Percentage membership</strong> shades the map to highlight
concentration of membership. When zooming in this also displays the
membership population percentage for individual areas.
<strong>Displaying deprivation</strong> shades the map to highlight
highly deprived areas. This uses the index of multiple deprivation for
each area. 1 represents highly deprived areas and is shaded in red, 10
the least deprived.
</v-alert>
<v-radio-group v-model="mapDisplay" v-on:change="setDisplayOptions" inline>
<v-radio label="Highlight percentage membership" value="populationPercentage"></v-radio>
<v-radio label="Highlight deprivation levels" value="imd"></v-radio>
</v-radio-group>
<v-container class="map">
<mgl-map ref="mglMap" :center="center" :zoom="zoom" :mapStyle="mapStyle">
<mgl-fullscreen-control />
<mgl-navigation-control />
<mgl-scale-control />
<mgl-vector-source source-id="libraries" :tiles="librariesSource.tiles">
<mgl-circle-layer source-layer="libraries" layer-id="libraries_layer_circles"
:paint="librariesLayerCircle.paint" />
</mgl-vector-source>
<mgl-vector-source source-id="lsoas" :tiles="lsoasSource.tiles" :promoteId="lsoasSource.promoteId">
<mgl-fill-layer source-layer="lsoa_boundaries" layer-id="lsoas_layer_fill" :paint="lsoasLayerFill.paint"
:filter="matchFilter" />
<mgl-symbol-layer source-layer="lsoa_boundaries" layer-id="lsoas_layer_label" :paint="lsoasLayerLabel.paint"
:layout="lsoasLayerLabel.layout" :filter="matchFilter" :minzoom="lsoasLayerLabel.minzoom" />
</mgl-vector-source>
<mgl-geo-json-source source-id="authority" :data="authoritySource.data">
<mgl-line-layer v-if="authoritySource.show" layer-id="authority-line" :paint="authorityLayerLine.paint" />
<mgl-symbol-layer v-if="authoritySource.show" layer-id="authority-label"
:layout="authorityLayerLabel.layout" :paint="authorityLayerLabel.paint" />
</mgl-geo-json-source>
</mgl-map>
</v-container>
<v-container>
<span v-if="authorityLsoaStats.length > 0">
<h3 class="text-h6 text-decoration-underline my-3">Authority LSOAs</h3>
<v-card color="grey-lighten-2" variant="outlined" elevation="0" class="mb-2">
<v-data-table no-filter :headers="authorityLsoaStatsSummaryColumns" :items="[authorityLsoaStatsSummary]"
item-key="lsoas">
<template #bottom></template>
</v-data-table>
</v-card>
<h3 class="text-h6 text-decoration-underline my-3">LSOA deprivation stats</h3>
<v-card color="grey-lighten-2" variant="outlined" elevation="0" class="mb-2">
<v-data-table no-filter :headers="authorityDeprivationSummaryColumns" :items="authorityDeprivationSummary"
item-key="imd">
<template #bottom></template>
</v-data-table>
</v-card>
<h3 class="text-h6 text-decoration-underline my-3">LSOA rural/urban classifications</h3>
<v-card color="grey-lighten-2" variant="outlined" elevation="0" class="mb-2">
<v-data-table no-filter :headers="authorityRuralUrbanSummaryColumns" :items="authorityRuralUrbanSummary"
item-key="ruralUrbanClassification">
<template #bottom></template>
</v-data-table>
</v-card>
<v-btn color="info" variant="tonal" size="large" v-on:click="downloadStats" append-icon="mdi-content-save">
Save all authority LSOA stats
</v-btn>
</span>
</v-container>
</v-sheet>
</v-container>
</template>
<script>
import MarkDownData from '../markdown/membershipmap.md?raw'
import '../extensions/strings'
import { saveAs } from 'file-saver'
import FileUpload from '../components/FileUpload.vue'
import * as Papa from 'papaparse'
import * as colorbrewer from 'colorbrewer'
import bbox from '@turf/bbox'
import * as csvHelper from '../helpers/csv'
import * as libraryAuthoritiesHelper from '../helpers/libraryAuthorities'
import * as config from '../helpers/config.json'
export default {
data() {
return {
authorityLsoaStats: [],
authorityLsoaStatsSummary: [],
authorityLsoaStatsSummaryColumns: [
{ title: 'LSOAs', key: 'lsoas' },
{ title: 'Population', key: 'population' },
{ title: 'Members', key: 'members' },
{ title: 'Member percentage', key: 'memberPercentage' },
{ title: 'Min percentage', key: 'minMembersPercentage' },
{ title: 'Max percentage', key: 'maxMembersPercentage' }
],
authorityDeprivationSummaryColumns: [
{ title: 'Index of multiple deprivation', key: 'imd' },
{ title: 'Population', key: 'population' },
{ title: 'Percentage of population', key: 'percentPopulation' },
{ title: 'Members', key: 'members' },
{ title: 'Percentage of members', key: 'percentMembers' }
],
authorityRuralUrbanSummaryColumns: [
{ title: 'Rural Urban Classification', key: 'ruralUrbanClassification' },
{ title: 'Population', key: 'population' },
{ title: 'Percentage of population', key: 'percentPopulation' },
{ title: 'Members', key: 'members' },
{ title: 'Percentage of members', key: 'percentMembers' }
],
authorityDeprivationSummary: [],
authorityRuralUrbanSummary: [],
authoritySource: {
data: {
type: 'FeatureCollection',
features: []
},
show: false
},
authorityLayerLine: {
type: 'line',
minzoom: 10,
paint: {
'line-color': '#697B89',
'line-width': ['interpolate', ['linear'], ['zoom'], 10, 2, 22, 8]
}
},
authorityLayerLabel: {
type: 'symbol',
layout: {
'text-field': ['to-string', ['get', 'utla19nm']],
'text-font': ['Open Sans Bold'],
'text-allow-overlap': true,
'text-ignore-placement': true,
'symbol-placement': 'line',
'text-max-angle': 90,
'text-size': {
base: 1.2,
stops: [
[10, 8],
[22, 48]
]
},
'text-line-height': 1
},
paint: {
'text-color': '#697B89',
'text-halo-color': '#fafaf8',
'text-halo-width': 2
}
},
center: [-2, 52],
zoom: 7,
librariesSource: {
tiles: [config.libraries_tiles]
},
librariesLayerCircle: {
filter: ['!', ['has', 'Year closed']],
'source-layer': 'libraries',
paint: {
'circle-radius': ['interpolate', ['linear'], ['zoom'], 5, 2, 18, 10],
'circle-color': '#1b5e20',
'circle-stroke-width': [
'interpolate',
['linear'],
['zoom'],
5,
1,
18,
4
],
'circle-stroke-color': '#ffffff',
'circle-stroke-opacity': [
'interpolate',
['linear'],
['zoom'],
5,
0.8,
18,
1
],
'circle-opacity': [
'interpolate',
['linear'],
['zoom'],
5,
0.4,
18,
0.9
]
}
},
lsoaFiles: [],
lsoasSource: {
tiles: [config.lsoa_tiles],
promoteId: { lsoa_boundaries: 'code' },
show: false
},
lsoasLayerFill: {
paint: {
'fill-color': 'rgba(254, 113, 144, 1)',
'fill-opacity': 0.5
}
},
lsoasLayerLabel: {
minzoom: 12,
layout: {
'text-field': ['to-string', ['get', 'code']],
'text-font': ['Source Sans Pro SemiBold'],
'symbol-placement': 'point',
'text-size': {
base: 1.2,
stops: [
[12, 14],
[22, 48]
]
},
'text-line-height': 1
},
paint: {
'text-color': '#697B89',
'text-halo-color': '#fafaf8',
'text-halo-width': 1
}
},
matchColourLsoaPopulation: 'rgba(254, 113, 144, 1)',
matchColourLsoaDeprivation: 'rgba(254, 113, 144, 1)',
matchFilter: ['in', ['get', 'code'], ['literal', []]],
mapDisplay: 'populationPercentage',
mapStyle: 'https://zoomstack.librarydata.uk/light.json',
minZoom: 5,
maxZoom: 16,
matchFieldLsoaPopulation: ['to-string', ['get', 'code']],
matchFieldLsoaDeprivation: ['to-string', ['get', 'code']],
mdText: MarkDownData
}
},
methods: {
addMembershipData: async function () {
const self = this
if (self.lsoaFiles[0].name) {
const data = await csvHelper.parseFile(self.lsoaFiles[0], false)
this.setLsoaFields(data.slice(1))
const authority =
await libraryAuthoritiesHelper.getLibraryAuthorityByName(data[1][0])
const authorityLsoaStats =
await libraryAuthoritiesHelper.getAuthorityLsoaStatsByCode(
authority.code
)
const authorityDeprivationSummary = []
const authorityRuralUrbanSummary = []
authorityLsoaStats.forEach(lsoaStat => {
// Find the lsoa in the data
const matchingLibraryLsoaData = data.find(
lsoa => lsoa[2] === lsoaStat.code
)
if (matchingLibraryLsoaData) {
const memberString = matchingLibraryLsoaData[3].replace('x', '2')
const memberInt = memberString.length > 0 ? parseInt(memberString) : 0
lsoaStat.members = memberInt
lsoaStat.memberPercentage =
Math.round((memberInt / lsoaStat.population) * 100)
const existingDeprivation = authorityDeprivationSummary.find(
summary => summary.imd === lsoaStat.imd_decile
)
if (existingDeprivation) {
existingDeprivation.population += lsoaStat.population
existingDeprivation.members += memberInt
} else {
if (lsoaStat.imd_decile) {
authorityDeprivationSummary.push({
imd: lsoaStat.imd_decile,
population: lsoaStat.population,
members: memberInt
})
}
}
const existingRuralUrban = authorityRuralUrbanSummary.find(
summary => summary.ruralUrbanClassification === lsoaStat.rural_urban_code
)
if (existingRuralUrban) {
existingRuralUrban.population += lsoaStat.population
existingRuralUrban.members += memberInt
} else {
if (lsoaStat.rural_urban_code && lsoaStat.rural_urban_code.length > 0) {
authorityRuralUrbanSummary.push({
ruralUrbanClassification: lsoaStat.rural_urban_code,
population: lsoaStat.population,
members: lsoaStat.members
})
}
}
} else {
lsoaStat.members = 0
lsoaStat.memberPercentage = 0
}
})
// Add the percentages to the deprivation and rural urban summaries
authorityDeprivationSummary.forEach(summary => {
summary.percentPopulation = Math.round(
(summary.population / authorityDeprivationSummary.reduce(
(acc, lsoa) => acc + lsoa.population,
0
)) * 100
)
summary.percentMembers = Math.round(
(summary.members / authorityDeprivationSummary.reduce(
(acc, lsoa) => acc + lsoa.members,
0
)) * 100
)
})
authorityRuralUrbanSummary.forEach(summary => {
summary.percentPopulation = Math.round(
(summary.population / authorityRuralUrbanSummary.reduce(
(acc, ruc) => acc + ruc.population,
0
)) * 100
)
summary.percentMembers = Math.round(
(summary.members / authorityRuralUrbanSummary.reduce(
(acc, ruc) => acc + ruc.members,
0
)) * 100
)
})
this.authorityLsoaStats = authorityLsoaStats
this.authorityDeprivationSummary = authorityDeprivationSummary.sort(
(a, b) => a.imd - b.imd
)
this.authorityRuralUrbanSummary = authorityRuralUrbanSummary.sort(
(a, b) => a.ruralUrbanClassification.localeCompare(b.ruralUrbanClassification)
)
this.authorityLsoaStatsSummary = {
lsoas: authorityLsoaStats.length,
population: authorityLsoaStats.reduce(
(acc, lsoa) => acc + lsoa.population,
0
),
members: authorityLsoaStats.reduce(
(acc, lsoa) => acc + lsoa.members,
0
),
minMembersPercentage: Math.round(
Math.min(...authorityLsoaStats.map(lsoa => lsoa.memberPercentage))
),
maxMembersPercentage: Math.round(
Math.max(...authorityLsoaStats.map(lsoa => lsoa.memberPercentage))
)
}
this.authorityLsoaStatsSummary.memberPercentage = Math.round(
(this.authorityLsoaStatsSummary.members /
this.authorityLsoaStatsSummary.population) *
100
)
const geojson = JSON.parse(authority.geojson)
this.authoritySource.data = geojson
this.authoritySource.show = true
const bounds = bbox(geojson)
this.$refs.mglMap.map.fitBounds(bounds, { padding: 10 })
}
},
setDisplayOptions: function () {
if (this.mapDisplay === 'populationPercentage') {
this.lsoasLayerFill.paint['fill-color'] = this.matchColourLsoaPopulation
this.lsoasLayerLabel.layout['text-field'] =
this.matchFieldLsoaPopulation
this.$refs.mglMap.map.setPaintProperty(
'lsoas_layer_fill',
'fill-color',
this.matchColourLsoaPopulation
)
this.$refs.mglMap.map.setLayoutProperty(
'lsoas_layer_label',
'text-field',
this.matchFieldLsoaPopulation
)
}
if (this.mapDisplay === 'imd') {
this.lsoasLayerFill.paint['fill-color'] =
this.matchColourLsoaDeprivation
this.lsoasLayerLabel.layout['text-field'] =
this.matchFieldLsoaDeprivation
this.$refs.mglMap.map.setPaintProperty(
'lsoas_layer_fill',
'fill-color',
this.matchColourLsoaDeprivation
)
this.$refs.mglMap.map.setLayoutProperty(
'lsoas_layer_label',
'text-field',
this.matchFieldLsoaDeprivation
)
}
},
setLsoaFields: function (lsoas) {
const filters = []
const matchFieldLsoaPopulation = ['match', ['get', 'code']]
const matchFieldLsoaDeprivation = ['match', ['get', 'code']]
lsoas.forEach(lsoa => {
const members = parseInt(lsoa[3].replace('x', '2'))
this.$refs.mglMap.map.setFeatureState(
{
source: 'lsoas',
sourceLayer: 'lsoa_boundaries',
id: lsoa[2]
},
{
members: members
}
)
filters.push(lsoa[2])
matchFieldLsoaPopulation.push(lsoa[2])
matchFieldLsoaPopulation.push([
'concat',
[
'to-string',
[
'round',
['*', ['/', members, ['to-number', ['get', 'population']]], 100]
]
],
'%'
])
matchFieldLsoaDeprivation.push(lsoa[2])
matchFieldLsoaDeprivation.push(['get', 'imd'])
})
matchFieldLsoaPopulation.push('')
matchFieldLsoaDeprivation.push('')
const estimatedMinPopulationPercentage = Math.floor(
Math.min(
...lsoas.map(lsoa => {
return (parseInt(lsoa[3].replace('x', '2')) / 1800) * 100
})
)
)
const estimatedMaxPopulationPercentage = Math.ceil(
Math.max(
...lsoas
.filter(lsoa => lsoa[3] <= 1200)
.map(lsoa => {
return (parseInt(lsoa[3].replace('x', '2')) / 1500) * 100
})
)
)
// From the min and max, create 10 deciles
const percentageDeciles = [1, 2, 3, 4, 5, 6, 7, 8, 9].map(decile => {
return Math.round(
estimatedMinPopulationPercentage +
(estimatedMaxPopulationPercentage -
estimatedMinPopulationPercentage) *
(decile / 10)
)
})
const decilesPlusColourFlattened = percentageDeciles.map(
(decile, index) => {
return [decile, colorbrewer.default.OrRd[9][index]]
}
)
const matchColourLsoaPopulation = [
'interpolate',
['linear'],
[
'round',
[
'*',
[
'/',
['to-number', ['feature-state', 'members']],
['to-number', ['get', 'population']]
],
100
]
],
...decilesPlusColourFlattened.flat()
]
const matchColourLsoaDeprivation = [
'interpolate',
['linear'],
['to-number', ['get', 'imd']],
1,
colorbrewer.default.OrRd[9][8],
2,
colorbrewer.default.OrRd[9][7],
3,
colorbrewer.default.OrRd[9][6],
4,
colorbrewer.default.OrRd[9][5],
5,
colorbrewer.default.OrRd[9][4],
6,
colorbrewer.default.OrRd[9][3],
7,
colorbrewer.default.OrRd[9][2],
8,
colorbrewer.default.OrRd[9][1],
9,
colorbrewer.default.OrRd[9][0],
10,
colorbrewer.default.OrRd[9][0]
]
// Only show where lsoas exist in data
const matchFilter = ['in', ['get', 'code'], ['literal', filters]]
this.matchFilter = matchFilter
this.$refs.mglMap.map.setFilter('lsoas_layer_fill', matchFilter)
this.$refs.mglMap.map.setFilter('lsoas_layer_label', matchFilter)
// Store permanent definitions
this.matchFieldLsoaPopulation = matchFieldLsoaPopulation
this.matchFieldLsoaDeprivation = matchFieldLsoaDeprivation
this.matchColourLsoaPopulation = matchColourLsoaPopulation
this.matchColourLsoaDeprivation = matchColourLsoaDeprivation
this.setDisplayOptions()
},
downloadStats: function () {
const statsCsvData = [
[
'LSOA name',
'LSOA code',
'IMD',
'Rural urban code',
'Population',
'Members',
'Member percentage'
]
].concat(
this.authorityLsoaStats.map(lsoa => [
lsoa.area_name,
lsoa.code,
lsoa.imd_decile,
lsoa.rural_urban_code,
lsoa.population,
lsoa.members,
lsoa.memberPercentage
])
)
const blob = new Blob([Papa.unparse(statsCsvData)], {
type: 'text/plain;charset=utf-8'
})
saveAs(blob, 'authority-lsoa-stats.csv')
}
},
components: {
'file-upload': FileUpload
}
}
</script>
<style>
.map {
position: relative;
height: 500px;
width: 100%;
border: 1px solid #e5e5e5;
padding: 0;
}
</style>