-
Notifications
You must be signed in to change notification settings - Fork 23
/
DCCACompareMap.js
323 lines (283 loc) · 7.9 KB
/
DCCACompareMap.js
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
import React, { Component } from 'react'
import Map from 'ol/Map'
import View from 'ol/View'
import TileLayer from 'ol/layer/Tile'
import VectorLayer from 'ol/layer/Vector'
import VectorSource from 'ol/source/Vector'
import XYZ from 'ol/source/XYZ'
import GeoJSON from 'ol/format/GeoJSON'
import { Style, Stroke, Fill, Text, Icon } from 'ol/style'
import Select from 'ol/interaction/Select'
import Feature from 'ol/Feature'
import Point from 'ol/geom/Point'
import styled from 'styled-components'
import { COLORS } from 'ui/theme'
import dc2003 from '../data/DCCA_2003'
import dc2007 from '../data/DCCA_2007'
import dc2011 from '../data/DCCA_2011'
import dc2015 from '../data/DCCA_2015'
import dc2019 from '../data/DCCA_2019'
import { adjustConcavePolygonCentroid } from 'utils/features'
import { withLanguage } from 'utils/helper'
import { fireEvent } from 'utils/ga_fireevent'
const MapContainer = styled.div`
width: 100%;
height: 100%;
`
const regionStyle = new Style({
fill: new Fill({
color: 'rgba(255, 255, 255, 0.1)',
}),
stroke: new Stroke({
color: '#356272',
width: 1,
}),
text: new Text({
font: '14px Noto Sans TC, sans-serif',
fill: new Fill({
color: '#356272',
}),
stroke: new Stroke({
color: '#fff',
width: 3,
}),
}),
})
const highlightStyle = new Style({
stroke: new Stroke({
color: COLORS.main.primary,
width: 2,
}),
fill: new Fill({
color: 'rgba(255,255,255,0.5)',
}),
text: new Text({
font: 'bold 16px Noto Sans TC, sans-serif',
fill: new Fill({
color: COLORS.main.primary,
}),
stroke: new Stroke({
color: 'white',
width: 3,
}),
}),
})
const hoverStyle = new Style({
stroke: new Stroke({
color: COLORS.main.primary,
width: 2,
}),
fill: new Fill({
color: 'rgba(255,255,0,0.2)',
}),
text: new Text({
font: 'bold 16px Noto Sans TC, sans-serif',
fill: new Fill({
color: COLORS.main.primary,
}),
stroke: new Stroke({
color: 'white',
width: 3,
}),
}),
})
class DCCACompareMap extends Component {
featureOverlay
featureHover
highlightedFeature
hoveredFeature
constructor(props) {
super(props)
this.state = {}
}
componentDidMount() {
const { year, code, handleMapLoaded } = this.props
const dc = [dc2003, dc2007, dc2011, dc2015, dc2019].find(
d => d.name === `DCCA_${year}`
)
let isDCDataExist = dc ? true : false
let featuresLayer
let vectorLayer
let geoMarker
if (isDCDataExist) {
this.featureSource = new VectorSource({
features: new GeoJSON().readFeatures(dc),
})
featuresLayer = new VectorLayer({
source: this.featureSource,
style: feature => {
regionStyle
.getText()
.setText(
withLanguage(
feature.getProperties().ENAME,
feature.getProperties().CNAME
)
)
return regionStyle
},
})
}
// create map object with feature layer
const layers = [
//default OSM layer
new TileLayer({
source: new XYZ({
url:
'https://server.arcgisonline.com/ArcGIS/rest/services/World_Street_Map/MapServer/tile/{z}/{y}/{x}',
}),
}),
]
if (isDCDataExist) {
layers.push(featuresLayer)
}
const map = new Map({
target: this.refs.mapContainer,
layers,
view: new View({
projection: 'EPSG:4326',
center: [114.2029, 22.3844],
zoom: 13,
}),
controls: [],
})
this.featureOverlay = new VectorLayer({
source: new VectorSource(),
map: map,
style: function(feature) {
highlightStyle
.getText()
.setText(
`${withLanguage(
feature.getProperties().ENAME,
feature.getProperties().CNAME
)}`
)
return highlightStyle
},
})
// Add layer for placing marker, set marker's style
let markerCoordinates = this.props.currentPoint
vectorLayer = new VectorLayer({
name: 'marker',
source: new VectorSource(),
style: function(feature) {
return new Style({
image: new Icon({
anchor: [0.5, 1],
scale: 0.25,
src: '/static/images/marker.png',
}),
})
},
})
map.addLayer(vectorLayer)
this.featureHover = new VectorLayer({
source: new VectorSource(),
map: map,
style: function(feature) {
hoverStyle.getText().setText(`${feature.getProperties().CNAME}`)
return hoverStyle
},
})
if (isDCDataExist) {
// Fit to feature
const features = this.featureSource.getFeatures()
for (let i = 0; i < features.length; i++) {
if (features[i].getProperties().CACODE === code) {
const extent = features[i].getGeometry().getExtent()
map.getView().fit(extent, {
size: map.getSize(),
padding: [10, 10, 10, 10],
})
this.highlightFeature(features[i])
const loadedResult = {
centroid:
adjustConcavePolygonCentroid(2019, code) ||
features[i] // hardcode as 2019
.getGeometry()
.getInteriorPoint()
.getCoordinates(),
}
handleMapLoaded(loadedResult)
break
}
}
}
// Add marker to the place user clicked
geoMarker = new Feature({
type: 'geoMarker',
geometry: new Point([markerCoordinates['lng'], markerCoordinates['lat']]),
})
geoMarker.setProperties({ ENAME: '', CNAME: '' })
vectorLayer.getSource().addFeature(geoMarker)
this.setState({
map,
view: map.getView(),
featuresLayer: isDCDataExist ? featuresLayer : null,
})
const selectBySingleClick = new Select()
map.addInteraction(selectBySingleClick)
selectBySingleClick.on('select', this.mapClick)
// const selectByHover = new Select({
// condition: pointerMove,
// })
// map.addInteraction(selectByHover)
// selectByHover.on('select', this.mapHover)
}
highlightFeature(feature) {
if (this.highlightedFeature) {
this.featureOverlay.getSource().removeFeature(this.highlightedFeature)
}
this.featureOverlay.getSource().addFeature(feature)
this.highlightedFeature = feature
}
highlightHoveredFeature(feature) {
if (this.hoveredFeature) {
this.featureHover.getSource().removeFeature(this.hoveredFeature)
}
this.featureHover.getSource().addFeature(feature)
this.hoveredFeature = feature
}
mapClick = e => {
const { year, changeDistrict, handleMapClick } = this.props
const selectedFeature = e.target.getFeatures().item(0)
let geoMarker = new Feature({
type: 'geoMarker',
geometry: new Point(e.mapBrowserEvent.coordinate),
})
geoMarker.setProperties({ ENAME: '', CNAME: '' })
this.state.map.getLayers().forEach(layer => {
if (layer.get('name') !== undefined && layer.get('name') === 'marker') {
layer.getSource().clear()
layer.getSource().addFeature(geoMarker)
}
})
handleMapClick(e.mapBrowserEvent.coordinate)
if (selectedFeature) {
this.highlightFeature(selectedFeature)
changeDistrict(year, selectedFeature.get('CACODE'))
this.state.map.getView().fit(selectedFeature.getGeometry().getExtent(), {
duration: 200,
})
}
// No more select polygon
e.target.getFeatures().clear()
fireEvent({
ca: 'battleground',
ac: 'click',
lb: `map`,
})
}
mapHover = e => {
//const { year, changeDistrict, handleMapClick } = this.props
const selectedFeature = e.target.getFeatures().item(0)
if (selectedFeature) {
this.highlightHoveredFeature(selectedFeature)
}
}
render() {
return <MapContainer ref="mapContainer"> </MapContainer>
}
}
export default DCCACompareMap