1
+ import VectorLayer from "ol/layer/Vector"
2
+ import { IMDLayer } from "../state"
3
+ import BaseLayer from "ol/layer/Base"
4
+ import { memoize } from "lodash"
5
+ import VectorSource from "ol/source/Vector"
6
+ import { Fill , Stroke , Style , Text } from "ol/style"
7
+ import GeoJSON from "ol/format/GeoJSON"
8
+ import { bbox } from "ol/loadingstrategy"
9
+ import { Map } from "ol"
10
+ import { getColorStops } from "./model_output"
11
+ import { findColor } from "../analysis_panel_tools/subsection"
12
+
13
+ interface IMDProperty {
14
+ name : string
15
+ propName : string
16
+ min : number
17
+ max : number
18
+ }
19
+
20
+ export const IMDProperties : IMDProperty [ ] = [
21
+ {
22
+ name : "IMD - Decile" ,
23
+ propName : "IMD_Decile" ,
24
+ min : 10 ,
25
+ max : 1
26
+ } ,
27
+ {
28
+ name : "IMD - Rank" ,
29
+ propName : "IMD_Rank" ,
30
+ min : 32844 ,
31
+ max : 1
32
+ } ,
33
+ {
34
+ name : "Income - Decile" ,
35
+ propName : "IncDec" ,
36
+ min : 10 ,
37
+ max : 1
38
+ } ,
39
+ {
40
+ name : "Income - Rank" ,
41
+ propName : "IncRank" ,
42
+ min : 32844 ,
43
+ max : 1
44
+ } ,
45
+ {
46
+ name : "Employment - Decile" ,
47
+ propName : "EmpDec" ,
48
+ min : 10 ,
49
+ max : 1
50
+ } ,
51
+ {
52
+ name : "Employment - Rank" ,
53
+ propName : "EmpRank" ,
54
+ min : 32844 ,
55
+ max : 1
56
+ } ,
57
+ {
58
+ name : "Education, Skills and Training - Decile" ,
59
+ propName : "EduDec" ,
60
+ min : 10 ,
61
+ max : 1
62
+ } ,
63
+ {
64
+ name : "Education, Skills and Training - Rank" ,
65
+ propName : "EduRank" ,
66
+ min : 32844 ,
67
+ max : 1
68
+ } ,
69
+ {
70
+ name : "Health Deprivation and Disability - Decile" ,
71
+ propName : "HDDec" ,
72
+ min : 10 ,
73
+ max : 1
74
+ } ,
75
+ {
76
+ name : "Health Deprivation and Disability - Rank" ,
77
+ propName : "HDDRank" ,
78
+ min : 32844 ,
79
+ max : 1
80
+ } ,
81
+ {
82
+ name : "Crime - Decile" ,
83
+ propName : "CriDec" ,
84
+ min : 10 ,
85
+ max : 1
86
+ } ,
87
+ {
88
+ name : "Crime - Rank" ,
89
+ propName : "CriRank" ,
90
+ min : 32844 ,
91
+ max : 1
92
+ } ,
93
+ {
94
+ name : "Barriers to Housing and Services - Decile" ,
95
+ propName : "BHSDec" ,
96
+ min : 10 ,
97
+ max : 1
98
+ } ,
99
+ {
100
+ name : "Barriers to Housing and Services - Rank" ,
101
+ propName : "BHSRank" ,
102
+ min : 32844 ,
103
+ max : 1
104
+ } ,
105
+ {
106
+ name : "Living Environment - Decile" ,
107
+ propName : "EnvDec" ,
108
+ min : 10 ,
109
+ max : 1
110
+ } ,
111
+ {
112
+ name : "Living Environment - Rank" ,
113
+ propName : "EnvRank" ,
114
+ min : 32844 ,
115
+ max : 1
116
+ }
117
+ ]
118
+
119
+ export type IMDProp = typeof IMDProperties [ number ]
120
+
121
+ const imdSource = memoize ( ( ) =>
122
+ new VectorSource ( {
123
+ url : extent => `https://landscapes.wearepal.ai/geoserver/shapefiles/ows?service=WFS&version=1.0.0&request=GetFeature&typeName=shapefiles:IMD_2019&outputFormat=application/json&bbox=${ extent . join ( ',' ) } ,EPSG:3857&crs=EPSG:3857` ,
124
+ format : new GeoJSON ( ) ,
125
+ strategy : bbox ,
126
+ attributions : '© <a href="https://data.cdrc.ac.uk/dataset/index-multiple-deprivation-imd">Consumer Data Research Centre</a>'
127
+ } )
128
+ )
129
+
130
+ const styleFn = memoize ( ( colMap : any [ ] , prop : IMDProperty ) =>
131
+ ( feature ) => {
132
+
133
+ const val = feature . get ( prop . propName )
134
+ const [ min , max ] = prop . min > prop . max ? [ prop . max , prop . min ] : [ prop . min , prop . max ]
135
+ const normalisedValue = prop . min > prop . max ? 1 - ( val - min ) / ( max - min ) : ( val - min ) / ( max - min )
136
+ const col = findColor ( normalisedValue , colMap )
137
+
138
+ return new Style ( {
139
+ fill : new Fill ( { color : `rgba(${ col [ 0 ] } , ${ col [ 1 ] } , ${ col [ 2 ] } , 1)` } )
140
+ } )
141
+ }
142
+ )
143
+
144
+ export function reifyIMDLayer ( layer : IMDLayer , existingLayer : BaseLayer | null , map : Map ) {
145
+
146
+ const colMap = getColorStops ( layer . fill === "heatmap" ? "jet" : ( layer . fill === "greyscale" ? "greys" : layer . fill ) , 100 ) . reverse ( )
147
+
148
+ return new VectorLayer ( {
149
+ source : imdSource ( ) ,
150
+ style : styleFn ( colMap , layer . property ) ,
151
+ } )
152
+ }
153
+
0 commit comments