/
Base.js
405 lines (364 loc) · 11.8 KB
/
Base.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
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
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
/**
* @module ol/layer/Base
*/
import BaseObject from '../Object.js';
import LayerProperty from './Property.js';
import {abstract} from '../util.js';
import {assert} from '../asserts.js';
import {clamp} from '../math.js';
/**
* A css color, or a function called with a view resolution returning a css color.
*
* @typedef {string|function(number):string} BackgroundColor
* @api
*/
/**
* @typedef {import("../ObjectEventType").Types|'change:extent'|'change:maxResolution'|'change:maxZoom'|
* 'change:minResolution'|'change:minZoom'|'change:opacity'|'change:visible'|'change:zIndex'} BaseLayerObjectEventTypes
*/
/***
* @template Return
* @typedef {import("../Observable").OnSignature<import("../Observable").EventTypes, import("../events/Event.js").default, Return> &
* import("../Observable").OnSignature<BaseLayerObjectEventTypes, import("../Object").ObjectEvent, Return> &
* import("../Observable").CombinedOnSignature<import("../Observable").EventTypes|BaseLayerObjectEventTypes, Return>} BaseLayerOnSignature
*/
/**
* @typedef {Object} Options
* @property {string} [className='ol-layer'] A CSS class name to set to the layer element.
* @property {number} [opacity=1] Opacity (0, 1).
* @property {boolean} [visible=true] Visibility.
* @property {import("../extent.js").Extent} [extent] The bounding extent for layer rendering. The layer will not be
* rendered outside of this extent.
* @property {number | undefined} [zIndex] The z-index for layer rendering. At rendering time, the layers
* will be ordered, first by Z-index and then by position. When `undefined`, a `zIndex` of 0 is assumed
* for layers that are added to the map's `layers` collection, or `Infinity` when the layer's `setMap()`
* method was used.
* @property {number} [minResolution] The minimum resolution (inclusive) at which this layer will be
* visible.
* @property {number} [maxResolution] The maximum resolution (exclusive) below which this layer will
* be visible.
* @property {number} [minZoom] The minimum view zoom level (exclusive) above which this layer will be
* visible.
* @property {number} [maxZoom] The maximum view zoom level (inclusive) at which this layer will
* be visible.
* @property {BackgroundColor} [background] Background color for the layer. If not specified, no background
* will be rendered.
* @property {Object<string, *>} [properties] Arbitrary observable properties. Can be accessed with `#get()` and `#set()`.
*/
/**
* @classdesc
* Abstract base class; normally only used for creating subclasses and not
* instantiated in apps.
* Note that with {@link module:ol/layer/Base~BaseLayer} and all its subclasses, any property set in
* the options is set as a {@link module:ol/Object~BaseObject} property on the layer object, so
* is observable, and has get/set accessors.
*
* @api
*/
class BaseLayer extends BaseObject {
/**
* @param {Options} options Layer options.
*/
constructor(options) {
super();
/***
* @type {BaseLayerOnSignature<import("../events").EventsKey>}
*/
this.on;
/***
* @type {BaseLayerOnSignature<import("../events").EventsKey>}
*/
this.once;
/***
* @type {BaseLayerOnSignature<void>}
*/
this.un;
/**
* @type {BackgroundColor|false}
* @private
*/
this.background_ = options.background;
/**
* @type {Object<string, *>}
*/
const properties = Object.assign({}, options);
if (typeof options.properties === 'object') {
delete properties.properties;
Object.assign(properties, options.properties);
}
properties[LayerProperty.OPACITY] =
options.opacity !== undefined ? options.opacity : 1;
assert(
typeof properties[LayerProperty.OPACITY] === 'number',
'Layer opacity must be a number',
);
properties[LayerProperty.VISIBLE] =
options.visible !== undefined ? options.visible : true;
properties[LayerProperty.Z_INDEX] = options.zIndex;
properties[LayerProperty.MAX_RESOLUTION] =
options.maxResolution !== undefined ? options.maxResolution : Infinity;
properties[LayerProperty.MIN_RESOLUTION] =
options.minResolution !== undefined ? options.minResolution : 0;
properties[LayerProperty.MIN_ZOOM] =
options.minZoom !== undefined ? options.minZoom : -Infinity;
properties[LayerProperty.MAX_ZOOM] =
options.maxZoom !== undefined ? options.maxZoom : Infinity;
/**
* @type {string}
* @private
*/
this.className_ =
properties.className !== undefined ? properties.className : 'ol-layer';
delete properties.className;
this.setProperties(properties);
/**
* @type {import("./Layer.js").State}
* @private
*/
this.state_ = null;
}
/**
* Get the background for this layer.
* @return {BackgroundColor|false} Layer background.
*/
getBackground() {
return this.background_;
}
/**
* @return {string} CSS class name.
*/
getClassName() {
return this.className_;
}
/**
* This method is not meant to be called by layers or layer renderers because the state
* is incorrect if the layer is included in a layer group.
*
* @param {boolean} [managed] Layer is managed.
* @return {import("./Layer.js").State} Layer state.
*/
getLayerState(managed) {
/** @type {import("./Layer.js").State} */
const state =
this.state_ ||
/** @type {?} */ ({
layer: this,
managed: managed === undefined ? true : managed,
});
const zIndex = this.getZIndex();
state.opacity = clamp(Math.round(this.getOpacity() * 100) / 100, 0, 1);
state.visible = this.getVisible();
state.extent = this.getExtent();
state.zIndex = zIndex === undefined && !state.managed ? Infinity : zIndex;
state.maxResolution = this.getMaxResolution();
state.minResolution = Math.max(this.getMinResolution(), 0);
state.minZoom = this.getMinZoom();
state.maxZoom = this.getMaxZoom();
this.state_ = state;
return state;
}
/**
* @abstract
* @param {Array<import("./Layer.js").default>} [array] Array of layers (to be
* modified in place).
* @return {Array<import("./Layer.js").default>} Array of layers.
*/
getLayersArray(array) {
return abstract();
}
/**
* @abstract
* @param {Array<import("./Layer.js").State>} [states] Optional list of layer
* states (to be modified in place).
* @return {Array<import("./Layer.js").State>} List of layer states.
*/
getLayerStatesArray(states) {
return abstract();
}
/**
* Return the {@link module:ol/extent~Extent extent} of the layer or `undefined` if it
* will be visible regardless of extent.
* @return {import("../extent.js").Extent|undefined} The layer extent.
* @observable
* @api
*/
getExtent() {
return /** @type {import("../extent.js").Extent|undefined} */ (
this.get(LayerProperty.EXTENT)
);
}
/**
* Return the maximum resolution of the layer. Returns Infinity if
* the layer has no maximum resolution set.
* @return {number} The maximum resolution of the layer.
* @observable
* @api
*/
getMaxResolution() {
return /** @type {number} */ (this.get(LayerProperty.MAX_RESOLUTION));
}
/**
* Return the minimum resolution of the layer. Returns 0 if
* the layer has no minimum resolution set.
* @return {number} The minimum resolution of the layer.
* @observable
* @api
*/
getMinResolution() {
return /** @type {number} */ (this.get(LayerProperty.MIN_RESOLUTION));
}
/**
* Return the minimum zoom level of the layer. Returns -Infinity if
* the layer has no minimum zoom set.
* @return {number} The minimum zoom level of the layer.
* @observable
* @api
*/
getMinZoom() {
return /** @type {number} */ (this.get(LayerProperty.MIN_ZOOM));
}
/**
* Return the maximum zoom level of the layer. Returns Infinity if
* the layer has no maximum zoom set.
* @return {number} The maximum zoom level of the layer.
* @observable
* @api
*/
getMaxZoom() {
return /** @type {number} */ (this.get(LayerProperty.MAX_ZOOM));
}
/**
* Return the opacity of the layer (between 0 and 1).
* @return {number} The opacity of the layer.
* @observable
* @api
*/
getOpacity() {
return /** @type {number} */ (this.get(LayerProperty.OPACITY));
}
/**
* @abstract
* @return {import("../source/Source.js").State} Source state.
*/
getSourceState() {
return abstract();
}
/**
* Return the value of this layer's `visible` property. To find out whether the layer
* is visible on a map, use `isVisible()` instead.
* @return {boolean} The value of the `visible` property of the layer.
* @observable
* @api
*/
getVisible() {
return /** @type {boolean} */ (this.get(LayerProperty.VISIBLE));
}
/**
* Return the Z-index of the layer, which is used to order layers before
* rendering. Returns undefined if the layer is unmanaged.
* @return {number|undefined} The Z-index of the layer.
* @observable
* @api
*/
getZIndex() {
return /** @type {number|undefined} */ (this.get(LayerProperty.Z_INDEX));
}
/**
* Sets the background color.
* @param {BackgroundColor} [background] Background color.
*/
setBackground(background) {
this.background_ = background;
this.changed();
}
/**
* Set the extent at which the layer is visible. If `undefined`, the layer
* will be visible at all extents.
* @param {import("../extent.js").Extent|undefined} extent The extent of the layer.
* @observable
* @api
*/
setExtent(extent) {
this.set(LayerProperty.EXTENT, extent);
}
/**
* Set the maximum resolution at which the layer is visible.
* @param {number} maxResolution The maximum resolution of the layer.
* @observable
* @api
*/
setMaxResolution(maxResolution) {
this.set(LayerProperty.MAX_RESOLUTION, maxResolution);
}
/**
* Set the minimum resolution at which the layer is visible.
* @param {number} minResolution The minimum resolution of the layer.
* @observable
* @api
*/
setMinResolution(minResolution) {
this.set(LayerProperty.MIN_RESOLUTION, minResolution);
}
/**
* Set the maximum zoom (exclusive) at which the layer is visible.
* Note that the zoom levels for layer visibility are based on the
* view zoom level, which may be different from a tile source zoom level.
* @param {number} maxZoom The maximum zoom of the layer.
* @observable
* @api
*/
setMaxZoom(maxZoom) {
this.set(LayerProperty.MAX_ZOOM, maxZoom);
}
/**
* Set the minimum zoom (inclusive) at which the layer is visible.
* Note that the zoom levels for layer visibility are based on the
* view zoom level, which may be different from a tile source zoom level.
* @param {number} minZoom The minimum zoom of the layer.
* @observable
* @api
*/
setMinZoom(minZoom) {
this.set(LayerProperty.MIN_ZOOM, minZoom);
}
/**
* Set the opacity of the layer, allowed values range from 0 to 1.
* @param {number} opacity The opacity of the layer.
* @observable
* @api
*/
setOpacity(opacity) {
assert(typeof opacity === 'number', 'Layer opacity must be a number');
this.set(LayerProperty.OPACITY, opacity);
}
/**
* Set the visibility of the layer (`true` or `false`).
* @param {boolean} visible The visibility of the layer.
* @observable
* @api
*/
setVisible(visible) {
this.set(LayerProperty.VISIBLE, visible);
}
/**
* Set Z-index of the layer, which is used to order layers before rendering.
* The default Z-index is 0.
* @param {number} zindex The z-index of the layer.
* @observable
* @api
*/
setZIndex(zindex) {
this.set(LayerProperty.Z_INDEX, zindex);
}
/**
* Clean up.
*/
disposeInternal() {
if (this.state_) {
this.state_.layer = null;
this.state_ = null;
}
super.disposeInternal();
}
}
export default BaseLayer;