-
-
Notifications
You must be signed in to change notification settings - Fork 2
/
service.js
208 lines (189 loc) · 6.54 KB
/
service.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
import { STEP_UNITS } from './constant';
const { base, inherit, toRawType } = g3wsdk.core.utils;
const { GUI } = g3wsdk.gui;
const { PluginService: BasePluginService } = g3wsdk.core.plugin;
const WMS_PARAMETER = 'TIME';
const UPDATE_MAPLAYER_OPTIONS = { showSpinner: false };
/**
* Plugin service inherit from base plugin service
* @constructor
*/
function PluginService(){
this.setters = {
open() { this._open() },
close() { this._close() }
};
base(this);
this.init = function(config = {}) {
this.project = this.getCurrentProject();
this.config = config;
this.mapService = GUI.getService('map');
this.getChartConfig = {
interaction: null,
keyListener: null,
indexcolor: 0,
chart: null,
layer: new ol.layer.Vector({
source: new ol.source.Vector()
})
};
this.addProjectLayerFromConfigProject();
const show = this.config.layers.length > 0;
if (show) {
this.state = {
loading: false,
layers: this.config.layers,
panel: { open: false }
};
}
this.emit('ready', show);
};
/**
* Method to add layer from project layers configuration qtimseries
*/
this.addProjectLayerFromConfigProject = function() {
this.project.getConfigLayers().forEach(layerConfig => {
if ('Object' === toRawType(layerConfig.qtimeseries)) {
let {
field,
step = 1,
units = 'd',
start_date = null,
end_date = null,
} = layerConfig.qtimeseries;
const startDateTimeZoneOffset = new Date(start_date).getTimezoneOffset();
const endDateTimeZoneOffset = new Date(end_date).getTimezoneOffset();
start_date = moment(start_date).add(startDateTimeZoneOffset, 'minutes');
end_date = moment(end_date).add(endDateTimeZoneOffset, 'minutes');
const stepunit_and_multiplier = STEP_UNITS.find(step_unit => step_unit.qgis === units).moment.split(':');
let stepunit = stepunit_and_multiplier.length > 1 ? stepunit_and_multiplier[1]: stepunit_and_multiplier[0];
const stepunitmultiplier = stepunit_and_multiplier.length > 1 ? 1*stepunit_and_multiplier[0] : 1;
const id = layerConfig.id;
const projectLayer = this.project.getLayerById(id);
const name = projectLayer.getName();
const wmsname = projectLayer.getWMSLayerName();
this.config.layers.push({
id,
name,
wmsname,
start_date,
end_date,
options: {
range_max: moment(end_date).diff(moment(start_date), stepunit) - 1,
step, //added
stepunit,
stepunitmultiplier,
field
}
});
}
})
};
/**
* Get single
* @param layerId
* @param date
* @returns {Promise<unknown>}
*/
this.getTimeLayer = function({ layers, date, step, end_date, stepunit } = {} ){
return new Promise((resolve, reject) =>{
let findDate;
let endDate;
const ids = layers.map(layer => layer.id);
const projectLayers = ids.map(id => this.project.getLayerById(id));
projectLayers.forEach(projectLayer => projectLayer.setChecked(true));
const mapLayersToUpdate = ids.map(id => this.mapService.getMapLayerByLayerId(id));
const {multiplier, step_unit} = this.getMultiplierAndStepUnit(stepunit);
const findDateTimeZoneOffset = new Date(date).getTimezoneOffset();
findDate = moment(date).add(Math.abs(findDateTimeZoneOffset), 'minutes').toISOString();
endDate = moment(findDate).add(step * multiplier, step_unit).toISOString();
const layerEndDate = moment(end_date).add(Math.abs(findDateTimeZoneOffset), 'minutes').toISOString();
const isAfter = moment(endDate).isAfter(layerEndDate);
if (isAfter) endDate = layerEndDate;
const wmsParam = `${findDate}/${endDate}`;
let mapLayersToUpdateDone = mapLayersToUpdate.length;
mapLayersToUpdate.forEach(mapLayerToUpdate => {
mapLayerToUpdate.once('loadend', ()=> {
const info = endDate ? `${findDate} - ${endDate}` : findDate;
this.mapService.showMapInfo({
info,
style: {
fontSize: '1.2em',
color: 'grey',
border: '1px solid grey',
padding: '10px'
}
});
mapLayersToUpdateDone-=1;
mapLayersToUpdateDone === 0 && resolve();
});
mapLayerToUpdate.once('loaderror', () => {
const info = endDate ? `${findDate} - ${endDate}` : findDate;
this.mapService.showMapInfo({
info,
style: {
fontSize: '1.2em',
color: 'red',
border: '1px solid red',
padding: '10px'
}
});
mapLayersToUpdateDone-=1;
mapLayersToUpdateDone === 0 && reject();
});
this.mapService.updateMapLayer(mapLayerToUpdate, {
force: true,
[WMS_PARAMETER]: wmsParam
}, UPDATE_MAPLAYER_OPTIONS);
});
})
};
this.getMultiplierAndStepUnit = function(stepunit){
const multiplier_step_unit = stepunit.split(':');
return {
multiplier: multiplier_step_unit.length > 1 ? 1* multiplier_step_unit[0] : 1,
step_unit: multiplier_step_unit.length > 1 ? multiplier_step_unit[1] : stepunit
}
};
this.resetTimeLayer = function(layers, hideInfo=false){
return new Promise((resolve, reject) => {
let layersLength = layers.length;
layers.forEach(layer => {
if (layer.timed){
const mapLayerToUpdate = this.mapService.getMapLayerByLayerId(layer.id);
hideInfo && mapLayerToUpdate.once('loadend', () => {
this.mapService.showMapInfo();
layersLength-=1;
layersLength === 0 && resolve();
});
this.mapService.updateMapLayer(mapLayerToUpdate, {
force: true,
[WMS_PARAMETER]: undefined
});
} else resolve();
})
})
};
/**
* Method on open time series Panel
*/
this._open = function(){
this.state.panel.open = true;
};
/**
* Method on close time series Panel
*/
this._close = function(){
const layers = this.state.layers.filter(layer => layer.timed);
layers && this.resetTimeLayer(layers, true);
this.state.panel.open = false;
};
/**
* Clear time series
*/
this.clear = function(){
this.close();
};
}
inherit(PluginService, BasePluginService);
export default new PluginService;