-
Notifications
You must be signed in to change notification settings - Fork 88
/
FeatureOverlay.ts
149 lines (133 loc) · 5.05 KB
/
FeatureOverlay.ts
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
// The MIT License (MIT)
//
// Copyright (c) 2015-2022 Camptocamp SA
//
// Permission is hereby granted, free of charge, to any person obtaining a copy of
// this software and associated documentation files (the "Software"), to deal in
// the Software without restriction, including without limitation the rights to
// use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
// the Software, and to permit persons to whom the Software is furnished to do so,
// subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all
// copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
// FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
// COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
// IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
// CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
import {listen, unlistenByKey} from 'ol/events';
import {CollectionEvent} from 'ol/Collection';
import {FeatureOverlayMgr as NgeoMapFeatureOverlayMgrFeatureOverlayMgr} from 'ngeo/map/FeatureOverlayMgr';
import {EventsKey as OlEventsEventsKey} from 'ol/events';
import OlFeature from 'ol/Feature';
import OlGeomGeometry from 'ol/geom/Geometry';
import OlCollection from 'ol/Collection';
import OlStyleStyle from 'ol/style/Style';
import {StyleFunction as OlStyleStyleStyleFunction} from 'ol/style/Style';
import OlEventsEvent from 'ol/events/Event';
/**
* Provides a featureOverlay used by the FeatureOverlayManager.
*
* Example of initialization:
* const featureOverlay = new FeatureOverlay(manager, index);
*
* @param {NgeoMapFeatureOverlayMgrFeatureOverlayMgr} manager The feature overlay manager.
* @param {number} index This feature overlay's index.
*/
export class FeatureOverlay {
manager_: NgeoMapFeatureOverlayMgrFeatureOverlayMgr;
index_: number;
listenerKeys_: OlEventsEventsKey[];
constructor(manager: NgeoMapFeatureOverlayMgrFeatureOverlayMgr, index: number) {
this.manager_ = manager;
this.index_ = index;
this.listenerKeys_ = [];
}
/**
* Add a feature to the feature overlay.
*
* @param {OlFeature<OlGeomGeometry>} feature The feature to add.
*/
addFeature(feature: OlFeature<OlGeomGeometry>): void {
this.manager_.addFeature(feature, this.index_);
}
/**
* Remove a feature from the feature overlay.
*
* @param {OlFeature<OlGeomGeometry>} feature The feature to remove.
*/
removeFeature(feature: OlFeature<OlGeomGeometry>): void {
this.manager_.removeFeature(feature, this.index_);
}
/**
* Check if featureOverlay has no features.
*
* @returns {boolean} True if there is no features. False otherwise.
*/
isEmpty(): boolean {
return this.manager_.isEmpty(this.index_);
}
/**
* Remove all the features from the feature overlay.
*/
clear(): void {
this.manager_.clear(this.index_);
}
/**
* Configure this feature overlay with a feature collection. Features added
* to the collection are also added to the overlay. Same for removal. If you
* configure the feature overlay with a feature collection you will use the
* collection to add and remove features instead of using the overlay's
* `addFeature`, `removeFeature` and `clear` functions.
*
* @param {OlCollection<OlFeature<OlGeomGeometry>>} features Feature collection.
*/
setFeatures(features: OlCollection<OlFeature<OlGeomGeometry>>): void {
// Remove old features collection.
this.clear();
this.listenerKeys_.forEach(unlistenByKey);
// Add new feature collection.
if (features !== null) {
features.forEach((feature) => {
this.addFeature(feature);
});
// Listen collection to sync features in the manager.
this.listenerKeys_.push(
listen(features, 'add', (evt: OlEventsEvent | Event) => this.handleFeatureAdd_(evt))
);
this.listenerKeys_.push(
listen(features, 'remove', (evt: OlEventsEvent | Event) => this.handleFeatureRemove_(evt))
);
}
}
/**
* Set a style for the feature overlay.
*
* @param {OlStyleStyle | OlStyleStyle[] | OlStyleStyleStyleFunction} style Style.
*/
setStyle(style: OlStyleStyle | OlStyleStyle[] | OlStyleStyleStyleFunction): void {
this.manager_.setStyle(style, this.index_);
}
/**
* @param {Event|OlEventsEvent} evt Feature collection event.
*/
handleFeatureAdd_(evt: Event | OlEventsEvent): void {
if (evt instanceof CollectionEvent) {
const feature = evt.element as OlFeature<OlGeomGeometry>;
this.addFeature(feature);
}
}
/**
* @param {Event|OlEventsEvent} evt Feature collection event.
*/
handleFeatureRemove_(evt: Event | OlEventsEvent): void {
if (evt instanceof CollectionEvent) {
const feature = evt.element as OlFeature<OlGeomGeometry>;
this.removeFeature(feature);
}
}
}
export default FeatureOverlay;