-
Notifications
You must be signed in to change notification settings - Fork 0
/
Feature.js
296 lines (296 loc) · 10.8 KB
/
Feature.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
var __extends = (this && this.__extends) || (function () {
var extendStatics = function (d, b) {
extendStatics = Object.setPrototypeOf ||
({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
function (d, b) { for (var p in b) if (Object.prototype.hasOwnProperty.call(b, p)) d[p] = b[p]; };
return extendStatics(d, b);
};
return function (d, b) {
extendStatics(d, b);
function __() { this.constructor = d; }
d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
};
})();
/**
* @module ol/Feature
*/
import BaseObject, { getChangeEventType } from './Object.js';
import EventType from './events/EventType.js';
import { assert } from './asserts.js';
import { listen, unlistenByKey } from './events.js';
/**
* @typedef {typeof Feature|typeof import("./render/Feature.js").default} FeatureClass
*/
/**
* @typedef {Feature|import("./render/Feature.js").default} FeatureLike
*/
/**
* @classdesc
* A vector object for geographic features with a geometry and other
* attribute properties, similar to the features in vector file formats like
* GeoJSON.
*
* Features can be styled individually with `setStyle`; otherwise they use the
* style of their vector layer.
*
* Note that attribute properties are set as {@link module:ol/Object} properties on
* the feature object, so they are observable, and have get/set accessors.
*
* Typically, a feature has a single geometry property. You can set the
* geometry using the `setGeometry` method and get it with `getGeometry`.
* It is possible to store more than one geometry on a feature using attribute
* properties. By default, the geometry used for rendering is identified by
* the property name `geometry`. If you want to use another geometry property
* for rendering, use the `setGeometryName` method to change the attribute
* property associated with the geometry for the feature. For example:
*
* ```js
*
* import Feature from 'ol/Feature';
* import Polygon from 'ol/geom/Polygon';
* import Point from 'ol/geom/Point';
*
* var feature = new Feature({
* geometry: new Polygon(polyCoords),
* labelPoint: new Point(labelCoords),
* name: 'My Polygon'
* });
*
* // get the polygon geometry
* var poly = feature.getGeometry();
*
* // Render the feature as a point using the coordinates from labelPoint
* feature.setGeometryName('labelPoint');
*
* // get the point geometry
* var point = feature.getGeometry();
* ```
*
* @api
* @template {import("./geom/Geometry.js").default} Geometry
*/
var Feature = /** @class */ (function (_super) {
__extends(Feature, _super);
/**
* @param {Geometry|Object<string, *>=} opt_geometryOrProperties
* You may pass a Geometry object directly, or an object literal containing
* properties. If you pass an object literal, you may include a Geometry
* associated with a `geometry` key.
*/
function Feature(opt_geometryOrProperties) {
var _this = _super.call(this) || this;
/**
* @private
* @type {number|string|undefined}
*/
_this.id_ = undefined;
/**
* @type {string}
* @private
*/
_this.geometryName_ = 'geometry';
/**
* User provided style.
* @private
* @type {import("./style/Style.js").StyleLike}
*/
_this.style_ = null;
/**
* @private
* @type {import("./style/Style.js").StyleFunction|undefined}
*/
_this.styleFunction_ = undefined;
/**
* @private
* @type {?import("./events.js").EventsKey}
*/
_this.geometryChangeKey_ = null;
_this.addEventListener(getChangeEventType(_this.geometryName_), _this.handleGeometryChanged_);
if (opt_geometryOrProperties) {
if (typeof (
/** @type {?} */ (opt_geometryOrProperties).getSimplifiedGeometry) === 'function') {
var geometry = /** @type {Geometry} */ (opt_geometryOrProperties);
_this.setGeometry(geometry);
}
else {
/** @type {Object<string, *>} */
var properties = opt_geometryOrProperties;
_this.setProperties(properties);
}
}
return _this;
}
/**
* Clone this feature. If the original feature has a geometry it
* is also cloned. The feature id is not set in the clone.
* @return {Feature} The clone.
* @api
*/
Feature.prototype.clone = function () {
var clone = new Feature(this.hasProperties() ? this.getProperties() : null);
clone.setGeometryName(this.getGeometryName());
var geometry = this.getGeometry();
if (geometry) {
clone.setGeometry(geometry.clone());
}
var style = this.getStyle();
if (style) {
clone.setStyle(style);
}
return clone;
};
/**
* Get the feature's default geometry. A feature may have any number of named
* geometries. The "default" geometry (the one that is rendered by default) is
* set when calling {@link module:ol/Feature~Feature#setGeometry}.
* @return {Geometry|undefined} The default geometry for the feature.
* @api
* @observable
*/
Feature.prototype.getGeometry = function () {
return /** @type {Geometry|undefined} */ (this.get(this.geometryName_));
};
/**
* Get the feature identifier. This is a stable identifier for the feature and
* is either set when reading data from a remote source or set explicitly by
* calling {@link module:ol/Feature~Feature#setId}.
* @return {number|string|undefined} Id.
* @api
*/
Feature.prototype.getId = function () {
return this.id_;
};
/**
* Get the name of the feature's default geometry. By default, the default
* geometry is named `geometry`.
* @return {string} Get the property name associated with the default geometry
* for this feature.
* @api
*/
Feature.prototype.getGeometryName = function () {
return this.geometryName_;
};
/**
* Get the feature's style. Will return what was provided to the
* {@link module:ol/Feature~Feature#setStyle} method.
* @return {import("./style/Style.js").StyleLike|undefined} The feature style.
* @api
*/
Feature.prototype.getStyle = function () {
return this.style_;
};
/**
* Get the feature's style function.
* @return {import("./style/Style.js").StyleFunction|undefined} Return a function
* representing the current style of this feature.
* @api
*/
Feature.prototype.getStyleFunction = function () {
return this.styleFunction_;
};
/**
* @private
*/
Feature.prototype.handleGeometryChange_ = function () {
this.changed();
};
/**
* @private
*/
Feature.prototype.handleGeometryChanged_ = function () {
if (this.geometryChangeKey_) {
unlistenByKey(this.geometryChangeKey_);
this.geometryChangeKey_ = null;
}
var geometry = this.getGeometry();
if (geometry) {
this.geometryChangeKey_ = listen(geometry, EventType.CHANGE, this.handleGeometryChange_, this);
}
this.changed();
};
/**
* Set the default geometry for the feature. This will update the property
* with the name returned by {@link module:ol/Feature~Feature#getGeometryName}.
* @param {Geometry|undefined} geometry The new geometry.
* @api
* @observable
*/
Feature.prototype.setGeometry = function (geometry) {
this.set(this.geometryName_, geometry);
};
/**
* Set the style for the feature to override the layer style. This can be a
* single style object, an array of styles, or a function that takes a
* resolution and returns an array of styles. To unset the feature style, call
* `setStyle()` without arguments or a falsey value.
* @param {import("./style/Style.js").StyleLike=} opt_style Style for this feature.
* @api
* @fires module:ol/events/Event~BaseEvent#event:change
*/
Feature.prototype.setStyle = function (opt_style) {
this.style_ = opt_style;
this.styleFunction_ = !opt_style
? undefined
: createStyleFunction(opt_style);
this.changed();
};
/**
* Set the feature id. The feature id is considered stable and may be used when
* requesting features or comparing identifiers returned from a remote source.
* The feature id can be used with the
* {@link module:ol/source/Vector~VectorSource#getFeatureById} method.
* @param {number|string|undefined} id The feature id.
* @api
* @fires module:ol/events/Event~BaseEvent#event:change
*/
Feature.prototype.setId = function (id) {
this.id_ = id;
this.changed();
};
/**
* Set the property name to be used when getting the feature's default geometry.
* When calling {@link module:ol/Feature~Feature#getGeometry}, the value of the property with
* this name will be returned.
* @param {string} name The property name of the default geometry.
* @api
*/
Feature.prototype.setGeometryName = function (name) {
this.removeEventListener(getChangeEventType(this.geometryName_), this.handleGeometryChanged_);
this.geometryName_ = name;
this.addEventListener(getChangeEventType(this.geometryName_), this.handleGeometryChanged_);
this.handleGeometryChanged_();
};
return Feature;
}(BaseObject));
/**
* Convert the provided object into a feature style function. Functions passed
* through unchanged. Arrays of Style or single style objects wrapped
* in a new feature style function.
* @param {!import("./style/Style.js").StyleFunction|!Array<import("./style/Style.js").default>|!import("./style/Style.js").default} obj
* A feature style function, a single style, or an array of styles.
* @return {import("./style/Style.js").StyleFunction} A style function.
*/
export function createStyleFunction(obj) {
if (typeof obj === 'function') {
return obj;
}
else {
/**
* @type {Array<import("./style/Style.js").default>}
*/
var styles_1;
if (Array.isArray(obj)) {
styles_1 = obj;
}
else {
assert(typeof ( /** @type {?} */(obj).getZIndex) === 'function', 41); // Expected an `import("./style/Style.js").Style` or an array of `import("./style/Style.js").Style`
var style = /** @type {import("./style/Style.js").default} */ (obj);
styles_1 = [style];
}
return function () {
return styles_1;
};
}
}
export default Feature;
//# sourceMappingURL=Feature.js.map