/
SerializerDeserializer.js
222 lines (201 loc) · 6.67 KB
/
SerializerDeserializer.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
/**
* @module ngeo.offline.SerializerDeserializer
*/
import olTilegridTileGrid from 'ol/tilegrid/TileGrid.js';
import olTilegridWMTS from 'ol/tilegrid/WMTS.js';
import * as olProj from 'ol/proj.js';
import olSourceTileWMS from 'ol/source/TileWMS.js';
import olSourceWMTS from 'ol/source/WMTS.js';
import olLayerTile from 'ol/layer/Tile.js';
const SerDes = class {
/**
* @param {Object} options The options
*/
constructor({gutter}) {
/**
* @private
*/
this.gutter_ = gutter;
}
/**
* @private
* @param {ol.Object} olObject An OL object
* @return {Object} The serializable properties of the object
*/
createBaseObject_(olObject) {
const properties = olObject.getProperties();
const obj = {};
for (const key in properties) {
const value = properties[key];
const typeOf = typeof value;
if (typeOf === 'string' || typeOf === 'number') {
obj[key] = value;
}
}
return obj;
}
/**
* @param {ol.tilegrid.TileGrid} tilegrid .
* @return {string} .
*/
serializeTilegrid(tilegrid) {
const obj = {};
obj['extent'] = tilegrid.getExtent();
obj['minZoom'] = tilegrid.getMinZoom();
obj['origin'] = tilegrid.getOrigin(0); // hack
obj['resolutions'] = tilegrid.getResolutions();
obj['tileSize'] = tilegrid.getTileSize(tilegrid.getMinZoom());
return JSON.stringify(obj);
}
/**
* @param {string} serialization .
* @return {ol.tilegrid.TileGrid} tilegrid
*/
deserializeTilegrid(serialization) {
const options = /** @type {olx.tilegrid.TileGridOptions} */ (JSON.parse(serialization));
return new olTilegridTileGrid(options);
}
/**
* @param {ol.tilegrid.WMTS} tilegrid .
* @return {string|undefined} .
*/
serializeTilegridWMTS(tilegrid) {
if (!tilegrid) {
return undefined;
}
const obj = {};
const resolutions = tilegrid.getResolutions();
obj['extent'] = tilegrid.getExtent();
obj['minZoom'] = tilegrid.getMinZoom();
obj['matrixIds'] = tilegrid.getMatrixIds();
obj['resolutions'] = resolutions;
obj['origins'] = [];
for (let z = 0; z < resolutions.length; ++z) {
obj['origins'].push(tilegrid.getOrigin(z));
}
return JSON.stringify(obj);
}
/**
* @param {string} serialization .
* @return {ol.tilegrid.WMTS} tilegrid .
*/
deserializeTilegridWMTS(serialization) {
const options = /** @type {olx.tilegrid.WMTSOptions} */ (JSON.parse(serialization));
return new olTilegridWMTS(options);
}
/**
* @param {ol.source.TileWMS} source .
* @return {string} .
*/
serializeSourceTileWMS(source) {
const obj = this.createBaseObject_(source);
obj['params'] = source.getParams();
obj['urls'] = source.getUrls();
obj['tileGrid'] = this.serializeTilegrid(source.getTileGrid());
const projection = source.getProjection();
if (projection) {
obj['projection'] = olProj.get(source.getProjection()).getCode();
}
return JSON.stringify(obj);
}
/**
* @param {string} serialization .
* @param {function(ol.ImageTile, string)=} tileLoadFunction .
* @return {ol.source.TileWMS} source .
*/
deserializeSourceTileWMS(serialization, tileLoadFunction) {
const options = /** @type {olx.source.TileWMSOptions} */ (JSON.parse(serialization));
options['tileLoadFunction'] = tileLoadFunction;
if (options['tileGrid']) {
options['tileGrid'] = this.deserializeTilegrid(options['tileGrid']);
}
options['gutter'] = this.gutter_;
return new olSourceTileWMS(options);
}
/**
* @param {ol.source.WMTS} source .
* @return {string} .
*/
serializeSourceWMTS(source) {
const obj = this.createBaseObject_(source);
obj['dimensions'] = source.getDimensions();
obj['format'] = source.getFormat();
obj['urls'] = source.getUrls();
obj['version'] = source.getVersion();
obj['layer'] = source.getLayer();
obj['style'] = source.getStyle();
obj['matrixSet'] = source.getMatrixSet();
// The OL getTileGrid method is expected to return a WMTS tilegrid so it is OK to cast here.
const tileGridWMTS = /** @type {ol.tilegrid.WMTS} */ (source.getTileGrid());
obj['tileGrid'] = this.serializeTilegridWMTS(tileGridWMTS);
obj['requestEncoding'] = source.getRequestEncoding();
const projection = source.getProjection();
if (projection) {
obj['projection'] = olProj.get(source.getProjection()).getCode();
}
return JSON.stringify(obj);
}
/**
* @param {string} serialization .
* @param {function(ol.ImageTile, string)=} tileLoadFunction .
* @return {ol.source.WMTS} .
*/
deserializeSourceWMTS(serialization, tileLoadFunction) {
const options = /** @type {olx.source.WMTSOptions} */ (JSON.parse(serialization));
options['tileLoadFunction'] = tileLoadFunction;
if (options['tileGrid']) {
options['tileGrid'] = this.deserializeTilegridWMTS(options['tileGrid']);
}
return new olSourceWMTS(options);
}
/**
* @private
* @param {number} number Some number which may be Infinity
* @return {number} The same number or an arbitrary big number instead of Infinity
*/
makeInfinitySerializable_(number) {
if (number === Infinity) {
return 1000;
}
return number;
}
/**
* @param {ol.layer.Tile|ol.layer.Image} layer .
* @param {ol.source.Source=} source .
* @return {string} .
*/
serializeTileLayer(layer, source) {
const obj = this.createBaseObject_(layer);
obj['opacity'] = layer.getOpacity();
obj['visible'] = layer.getVisible();
obj['minResolution'] = layer.getMinResolution();
obj['maxResolution'] = this.makeInfinitySerializable_(layer.getMaxResolution());
obj['zIndex'] = layer.getZIndex();
source = source || layer.getSource();
if (source instanceof olSourceTileWMS) {
obj['source'] = this.serializeSourceTileWMS(source);
obj['sourceType'] = 'tileWMS';
} else if (source instanceof olSourceWMTS) {
obj['source'] = this.serializeSourceWMTS(source);
obj['sourceType'] = 'WMTS';
}
return JSON.stringify(obj);
}
/**
* @param {string} serialization .
* @param {function(ol.ImageTile, string)=} tileLoadFunction .
* @return {ol.layer.Tile} .
*/
deserializeTileLayer(serialization, tileLoadFunction) {
const options = /** @type {olx.layer.TileOptions} */ (JSON.parse(serialization));
const sourceType = options['sourceType'];
if (sourceType === 'tileWMS') {
options['source'] = this.deserializeSourceTileWMS(options['source'], tileLoadFunction);
} else if (sourceType === 'WMTS') {
options['source'] = this.deserializeSourceWMTS(options['source'], tileLoadFunction);
}
return new olLayerTile(options);
}
};
const exports = SerDes;
export default exports;