-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMap.vue
372 lines (317 loc) · 10.4 KB
/
Map.vue
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
<template>
<div id="app-map">
<!-- OL map -->
<div id="map" ref="OLMap" class="map position-absolute vh-100 vw-100"></div>
<!-- Progress bar load tiles -->
<div v-show="!progress.isLoaded" class="position-absolute m-0 btn-dark" style="width: 100%; height: 10px; opacity: 0.8" :style="{'max-width': progress.progressPercent + '%'}">
<div class="spinner-border text-dark" style="position: relative; margin-top: 20px; margin-left: 20px" role="status">
<span class="visually-hidden">Loading...</span>
</div>
</div>
<!-- Legend -->
<wms-legend @legendClicked="changeStyle($event)" ref="legendWMS" class="position-absolute top-0 end-0 d-sm-flex me-2 mt-5"></wms-legend>
</div>
</template>
<script>
import WMSLegend from "WMSLegend.vue";
export default {
name: 'app-map',
created (){
// Declare non-reactive variables
this.map= undefined;
this.layers = {
bathymetry: new ol.layer.Tile({
name: 'bathymetry',
source: new ol.source.XYZ ({ // https://openlayers.org/en/latest/examples/xyz.html
url: 'https://tiles.emodnet-bathymetry.eu/2020/baselayer/web_mercator/{z}/{x}/{y}.png', // https://tiles.emodnet-bathymetry.eu/
attributions: "© EMODnet Bathymetry Consortium",
cacheSize: 500,
crossOrigin: 'anonymous',
}),
zIndex: -2,
}),
graticule: new ol.layer.Graticule({
name: 'graticule',
showLabels: true,
wrapX: false,
lonLabelPosition: 1,
strokeStyle: new ol.style.Stroke({
color: 'rgba(0,0,0,0.2)',
width: 2,
lineDash: [0.5, 4],
}),
lonLabelStyle: new ol.style.Text({
font: '12px Calibri,sans-serif',
textAlign: 'center',
textBaseline: 'top',
fill: new ol.style.Fill({
color: 'rgba(0,0,0,0.9)',
}),
stroke: new ol.style.Stroke({
color: 'rgba(255,255,255,0.5)',
width: 3
})
}),
latLabelStyle: new ol.style.Text({
font: '12px Calibri,sans-serif',
textAlign: 'end',
textBaseline: 'top',
fill: new ol.style.Fill({
color: 'rgba(0,0,0,0.9)',
}),
stroke: new ol.style.Stroke({
color: 'rgba(255,255,255,0.5)',
width: 3
})
}),
}),
shoreline: new ol.layer.VectorTile({
name: 'shoreline',
maxZoom: 22,
source: new ol.source.VectorTile({
attributions: '© European Environment Agency',
format: new ol.format.MVT(),
url: 'shoreline-tiles/{z}/{x}/{y}.pbf',
maxZoom: 10, // Defined in MVT folders
zDirection: -1
}),
style: new ol.style.Style({
stroke: new ol.style.Stroke({
color: 'rgba(0,0,0,0.7)',
width: 1
})
}),
}),
eez12nm: new ol.layer.VectorTile({
name: '12nauticmiles',
maxZoom: 22,
source: new ol.source.VectorTile({
attributions: '© Flanders Marine Institute',
format: new ol.format.MVT(),
url: 'eez_12nm/{z}/{x}/{y}.pbf',
maxZoom: 9, // Defined in MVT folders
zDirection: -1
}),
style: new ol.style.Style({
stroke: new ol.style.Stroke({
color: 'rgba(240,150,150,0.6)',
width: 1
})
}),
}),
data: new ol.layer.Tile({
name: 'data',
zIndex: -1,
//opacity: 0.9
})
};
this.layerData = undefined;
this.pixelColor = [0, 0, 0, 0];
},
mounted () {
this.initMap();
this.$refs.OLMap.addEventListener('mousemove', this.onMouseMove);
},
umounted () {
this.$refs.OLMap.removeEventListener('mousemove', this.onMouseMove);
this.map.un('moveend', this.onMapMoveEnd);
this.map.un('movestart', this.onMapMoveStart);
},
data () {
return {
progress: {
loading: 0,
loaded: 0
},
isLayerDataReady: false,
}
},
methods: {
// PRIVATE METHODS
// Figure clicked (TODO: emit)
initMap: function () {
// Initialize map
this.map = new ol.Map({
layers : [
// Data layer
this.layers.data,
// Bathymetry
//this.layers.bathymetry,
// Graticule layer
this.layers.graticule,
// Shoreline
this.layers.shoreline,
// 12 nm
this.layers.eez12nm,
],
target: 'map',
view: new ol.View({
center: ol.proj.fromLonLat([3,41.5]),
zoom: 6,
maxZoom: 22,
extent: ol.proj.fromLonLat([-28,20]).concat(ol.proj.fromLonLat([40, 50]))
}),
});
// Set css
document.getElementsByClassName('ol-attribution')[0].style.bottom = 'auto';
document.getElementsByClassName('ol-attribution')[0].style.top = '.5em';
// Declare onmapmove events
this.map.on('moveend', this.onMapMoveEnd);
this.map.on('movestart', this.onMapMoveStart);
},
// Get layer function
getMapLayer: function(layerName){
let selLayer;
this.map.getLayers().forEach(layerItem => {
//console.log(layerItem.get('name'));
if (layerItem.get('name') == layerName)
selLayer = layerItem;
})
return selLayer;
},
// INTERNAL EVENTS
// Change the styles (WMSLegend.vue emit)
changeStyle: function(newStyle){
// Get params
let params = this.getMapLayer('data').getSource().getParams();
// Check if the new style is the current
if (params.STYLES == newStyle)
return;
// If style is different, update source
params.STYLES = newStyle;
// Set params
this.getMapLayer('data').getSource().updateParams(params);
// Source needs to reload
this.isLayerDataReady = false;
// Update ForecastBar if it exists
this.$emit('changeWMSStyle', newStyle);
},
// Mouse move on map
onMouseMove: function(event){
// Return if map is moving
if (this.isMapMoving)
return;
// Get lat long coordinates
let coord = this.map.getCoordinateFromPixel([event.clientX, event.clientY]);
coord = ol.proj.transform(coord, 'EPSG:3857', 'EPSG:4326');
// Emit
this.$emit('mouseMove', coord);
// Change legend tooltip value
if (this.$refs.legendWMS){
if (this.isLayerDataReady){
let color = this.getDataAtPixel(event.clientX, event.clientY);
this.$refs.legendWMS.showValueAtColor(color);
}
}
},
// Map moves
onMapMoveEnd: function(){
this.isMapMoving = false;
// If data is loaded, update the pixel information once the map move finishes
// TODO: this could be optimized --> get a canvas with all data and relate lat-long to that canvas
if (this.isLayerDataReady)
this.updateSourceData();
},
onMapMoveStart: function(){
this.isMapMoving = true;
},
// Declare loading tile events
registerLoadTilesEvents: function(source){
// Source is a ol.source
let progress = this.progress;
progress.loading = 0;
progress.loaded = 0;
progress.isLoaded = false;
progress.progressPercent = 0;
this.isLayerDataReady = false;
source.on('tileloadstart',() => {
progress.loading += 1;
progress.isLoaded = false;
});
source.on('tileloadend', () => {
progress.loaded += 1;
progress.progressPercent = 100*progress.loaded/progress.loading;
if (progress.loading == progress.loaded){
this.onTilesLoaded();
progress.isLoaded = true;
}
});
/*source.on('tileloaderror', () => {
progress.loaded += 1;
if (progress.loading == progress.loaded)
this.onTilesLoaded();
});*/
},
// Store pixel information once tiles are loaded
onTilesLoaded: function(){
this.isLayerDataReady = true;
this.updateSourceData();
},
// Update the data pixels
updateSourceData: function(){
// Get ol layer
let layer = this.getMapLayer('data');
// Get canvas
let tmpCnv = layer.getRenderer().getImage();
// Get data
this.layerData = tmpCnv.getContext("2d").getImageData(0,0,tmpCnv.width,tmpCnv.height);
// Store width to access pixels
this.layerDataWidth = tmpCnv.width;
},
// Get pixel data
getDataAtPixel: function(x , y){
let imgArrayPos = (x + y * this.layerDataWidth) * 4; // + 1,2,3 if you want (R)GBA
let imgData = this.layerData.data;
let color = this.pixelColor;
color[0] = imgData[imgArrayPos]
color[1] = imgData[imgArrayPos+1]
color[2] = imgData[imgArrayPos+2]
color[3] = imgData[imgArrayPos+3];
return color;
},
// PUBLIC METHODS
// Update WMS data source. This function is called from AppManager.vue
updateSourceWMS: function (infoWMS){
// Create tile grid for faster rendering for low resolution WMS
let extent = ol.proj.get('EPSG:3857').getExtent();
let tileSize = 512;
let maxResolution = ol.extent.getWidth(extent) / tileSize;
let resolutions = new Array(5);
for (let i = 0; i < resolutions.length; i++){
resolutions[i] = maxResolution / Math.pow(2,i);
}
// Assign to openlayers WMS tile source
infoWMS.tileGrid = new ol.tilegrid.TileGrid({
extent: extent,
resolutions: resolutions,
tileSize: tileSize
});
// Avoid cross origin problems when getting pixel data (The canvas has been tainted by cross-origin data.)
infoWMS.crossOrigin= 'anonymous';
// Create OL source from ForecastBar.vue object
let source = new ol.source.TileWMS(infoWMS);
this.getMapLayer('data').setSource(source);
// Tracking the load progress
this.registerLoadTilesEvents(source);
// Update legend
if (this.$refs.legendWMS)
this.$refs.legendWMS.setWMSLegend(infoWMS);
},
// Get OL map object
getOLMap: function(){
return this.map;
}
},
components: {
"wms-legend": WMSLegend
},
computed: {
//foo: function () {}
}
}
</script>
<style scoped>
.map {
background: #f8f4f0;
}
</style>