/
drawing-context.tsx
224 lines (200 loc) · 6.06 KB
/
drawing-context.tsx
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
import * as ol from 'openlayers'
import { GeometryJSON, makeBufferedGeo } from '../geometry'
import { adjustGeoCoordsForAntimeridian } from '../geometry/utilities'
type EventHandler = (e: any) => void
type ListenerTarget = 'draw' | 'snap' | 'modify' | 'map'
type ListenerRecord = {
target: ListenerTarget
event: string
handler: EventHandler
}
/**
* Open Layers drawing context provides a layer between the drawing controls
* and the openlayers map, normalizing interactions with the openlayers map
* accross all drawing controls.
*/
class DrawingContext {
private map: ol.Map
private drawLayer: ol.layer.Vector
private bufferLayer: ol.layer.Vector
private modify: ol.interaction.Modify
private snap: ol.interaction.Snap
private draw: ol.interaction.Interaction | null
private listenerList: ListenerRecord[]
private style: ol.style.Style | ol.StyleFunction | ol.style.Style[]
private geoFormat: ol.format.GeoJSON
private animationFrameId: number
/**
* Constructs an instance of the drawing context
* @param map - reference to openlayers map
* @param drawingStyle - drawingStyle to be used by all drawing controls
*/
constructor({
map,
drawingStyle,
}: {
map: ol.Map
drawingStyle: ol.style.Style | ol.StyleFunction | ol.style.Style[]
}) {
this.bufferUpdateEvent = this.bufferUpdateEvent.bind(this)
this.animationFrameId = 0
this.geoFormat = new ol.format.GeoJSON()
this.style = drawingStyle
this.draw = null
this.listenerList = []
this.map = map
this.drawLayer = new ol.layer.Vector({
source: new ol.source.Vector(),
style: drawingStyle,
zIndex: 2,
updateWhileInteracting: true,
})
this.bufferLayer = new ol.layer.Vector({
source: new ol.source.Vector(),
style: drawingStyle,
zIndex: 1,
})
this.map.addLayer(this.bufferLayer)
this.map.addLayer(this.drawLayer)
this.modify = new ol.interaction.Modify({
source: this.drawLayer.getSource(),
})
this.snap = new ol.interaction.Snap({
source: this.drawLayer.getSource(),
})
}
getStyle(): ol.style.Style | ol.StyleFunction | ol.style.Style[] {
return this.style
}
removeFeature(): void {
this.drawLayer.getSource().clear()
}
updateFeature(feature: ol.Feature): void {
this.removeFeature()
const geo: GeometryJSON = JSON.parse(this.geoFormat.writeFeature(feature))
adjustGeoCoordsForAntimeridian(geo)
const adjustedFeature = this.geoFormat.readFeature(geo)
this.drawLayer.getSource().addFeature(adjustedFeature)
}
updateBufferFeature(feature: ol.Feature, animate: boolean = true): void {
this.bufferLayer.getSource().clear()
const buffer: number | undefined = feature.get('buffer')
if (buffer !== undefined && buffer > 0) {
const geo: GeometryJSON = JSON.parse(this.geoFormat.writeFeature(feature))
adjustGeoCoordsForAntimeridian(geo)
const bufferedGeo = makeBufferedGeo(geo)
// Must adjust the coordinates again because buffering undoes the
// adjustments we made above.
adjustGeoCoordsForAntimeridian(bufferedGeo)
const bufferFeature = this.geoFormat.readFeature(bufferedGeo)
this.bufferLayer.getSource().addFeature(bufferFeature)
if (animate) {
this.setEvent('map', 'pointerdrag', this.bufferUpdateEvent)
}
}
}
protected bufferUpdateEvent() {
const featureList = this.drawLayer.getSource().getFeatures()
if (featureList.length) {
const feature = featureList[0]
this.animationFrameId = requestAnimationFrame(() => {
this.updateBufferFeature(feature)
})
}
}
setModifyInteraction(modify: ol.interaction.Modify): void {
this.modify = modify
}
getSource(): ol.source.Vector {
return this.drawLayer.getSource()
}
setDrawInteraction(draw: ol.interaction.Interaction): void {
this.draw = draw
}
setEvent(target: ListenerTarget, event: string, handler: EventHandler): void {
const listenerTarget = this[target]
if (listenerTarget !== null) {
listenerTarget.on(event, handler)
this.listenerList.push({
target,
event,
handler,
})
}
}
removeListener(
target: ListenerTarget,
event: string,
handler: EventHandler
): void {
if (target === 'map') {
this.map.un(event, handler)
} else {
const listenerTarget = this[target]
if (listenerTarget !== null) {
listenerTarget.un(event, handler)
}
}
}
removeListeners(): void {
for (const listener of this.listenerList) {
this.removeListener(listener.target, listener.event, listener.handler)
}
this.listenerList = []
cancelAnimationFrame(this.animationFrameId)
}
addInteractions(): void {
if (this.draw !== null) {
this.map.addInteraction(this.draw)
}
this.map.addInteraction(this.snap)
this.map.addInteraction(this.modify)
}
addInteractionsWithoutModify(): void {
if (this.draw !== null) {
this.map.addInteraction(this.draw)
}
this.map.addInteraction(this.snap)
}
removeInteractions(): void {
this.map.removeInteraction(this.modify)
this.map.removeInteraction(this.snap)
if (this.draw !== null) {
this.map.removeInteraction(this.draw)
}
this.drawLayer.getSource().clear()
this.bufferLayer.getSource().clear()
}
remakeInteractions(): void {
this.modify = new ol.interaction.Modify({
source: this.drawLayer.getSource(),
})
this.snap = new ol.interaction.Snap({
source: this.drawLayer.getSource(),
})
}
setInteractionsActive(active: boolean): void {
this.modify.setActive(active)
this.snap.setActive(active)
if (this.draw) {
this.draw.setActive(active)
}
}
areInteractionsActive(): boolean {
return (
(this.draw === null || this.draw.getActive()) &&
this.modify.getActive() &&
this.snap.getActive()
)
}
circleRadiusToMeters(radius: number): number {
return (
radius *
this.map
.getView()
.getProjection()
.getMetersPerUnit()
)
}
}
export default DrawingContext