UNPKG

10.8 kBJavaScriptView Raw
1var __extends = (this && this.__extends) || (function () {
2 var extendStatics = function (d, b) {
3 extendStatics = Object.setPrototypeOf ||
4 ({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
5 function (d, b) { for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p]; };
6 return extendStatics(d, b);
7 };
8 return function (d, b) {
9 extendStatics(d, b);
10 function __() { this.constructor = d; }
11 d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
12 };
13})();
14/**
15 * @module ol/Feature
16 */
17import { assert } from './asserts.js';
18import { listen, unlistenByKey } from './events.js';
19import EventType from './events/EventType.js';
20import BaseObject, { getChangeEventType } from './Object.js';
21/**
22 * @typedef {typeof Feature|typeof import("./render/Feature.js").default} FeatureClass
23 */
24/**
25 * @typedef {Feature|import("./render/Feature.js").default} FeatureLike
26 */
27/**
28 * @classdesc
29 * A vector object for geographic features with a geometry and other
30 * attribute properties, similar to the features in vector file formats like
31 * GeoJSON.
32 *
33 * Features can be styled individually with `setStyle`; otherwise they use the
34 * style of their vector layer.
35 *
36 * Note that attribute properties are set as {@link module:ol/Object} properties on
37 * the feature object, so they are observable, and have get/set accessors.
38 *
39 * Typically, a feature has a single geometry property. You can set the
40 * geometry using the `setGeometry` method and get it with `getGeometry`.
41 * It is possible to store more than one geometry on a feature using attribute
42 * properties. By default, the geometry used for rendering is identified by
43 * the property name `geometry`. If you want to use another geometry property
44 * for rendering, use the `setGeometryName` method to change the attribute
45 * property associated with the geometry for the feature. For example:
46 *
47 * ```js
48 *
49 * import Feature from 'ol/Feature';
50 * import Polygon from 'ol/geom/Polygon';
51 * import Point from 'ol/geom/Point';
52 *
53 * var feature = new Feature({
54 * geometry: new Polygon(polyCoords),
55 * labelPoint: new Point(labelCoords),
56 * name: 'My Polygon'
57 * });
58 *
59 * // get the polygon geometry
60 * var poly = feature.getGeometry();
61 *
62 * // Render the feature as a point using the coordinates from labelPoint
63 * feature.setGeometryName('labelPoint');
64 *
65 * // get the point geometry
66 * var point = feature.getGeometry();
67 * ```
68 *
69 * @api
70 * @template {import("./geom/Geometry.js").default} Geometry
71 */
72var Feature = /** @class */ (function (_super) {
73 __extends(Feature, _super);
74 /**
75 * @param {Geometry|Object<string, *>=} opt_geometryOrProperties
76 * You may pass a Geometry object directly, or an object literal containing
77 * properties. If you pass an object literal, you may include a Geometry
78 * associated with a `geometry` key.
79 */
80 function Feature(opt_geometryOrProperties) {
81 var _this = _super.call(this) || this;
82 /**
83 * @private
84 * @type {number|string|undefined}
85 */
86 _this.id_ = undefined;
87 /**
88 * @type {string}
89 * @private
90 */
91 _this.geometryName_ = 'geometry';
92 /**
93 * User provided style.
94 * @private
95 * @type {import("./style/Style.js").StyleLike}
96 */
97 _this.style_ = null;
98 /**
99 * @private
100 * @type {import("./style/Style.js").StyleFunction|undefined}
101 */
102 _this.styleFunction_ = undefined;
103 /**
104 * @private
105 * @type {?import("./events.js").EventsKey}
106 */
107 _this.geometryChangeKey_ = null;
108 _this.addEventListener(getChangeEventType(_this.geometryName_), _this.handleGeometryChanged_);
109 if (opt_geometryOrProperties) {
110 if (typeof /** @type {?} */ (opt_geometryOrProperties).getSimplifiedGeometry === 'function') {
111 var geometry = /** @type {Geometry} */ (opt_geometryOrProperties);
112 _this.setGeometry(geometry);
113 }
114 else {
115 /** @type {Object<string, *>} */
116 var properties = opt_geometryOrProperties;
117 _this.setProperties(properties);
118 }
119 }
120 return _this;
121 }
122 /**
123 * Clone this feature. If the original feature has a geometry it
124 * is also cloned. The feature id is not set in the clone.
125 * @return {Feature} The clone.
126 * @api
127 */
128 Feature.prototype.clone = function () {
129 var clone = new Feature(this.getProperties());
130 clone.setGeometryName(this.getGeometryName());
131 var geometry = this.getGeometry();
132 if (geometry) {
133 clone.setGeometry(geometry.clone());
134 }
135 var style = this.getStyle();
136 if (style) {
137 clone.setStyle(style);
138 }
139 return clone;
140 };
141 /**
142 * Get the feature's default geometry. A feature may have any number of named
143 * geometries. The "default" geometry (the one that is rendered by default) is
144 * set when calling {@link module:ol/Feature~Feature#setGeometry}.
145 * @return {Geometry|undefined} The default geometry for the feature.
146 * @api
147 * @observable
148 */
149 Feature.prototype.getGeometry = function () {
150 return (
151 /** @type {Geometry|undefined} */ (this.get(this.geometryName_)));
152 };
153 /**
154 * Get the feature identifier. This is a stable identifier for the feature and
155 * is either set when reading data from a remote source or set explicitly by
156 * calling {@link module:ol/Feature~Feature#setId}.
157 * @return {number|string|undefined} Id.
158 * @api
159 */
160 Feature.prototype.getId = function () {
161 return this.id_;
162 };
163 /**
164 * Get the name of the feature's default geometry. By default, the default
165 * geometry is named `geometry`.
166 * @return {string} Get the property name associated with the default geometry
167 * for this feature.
168 * @api
169 */
170 Feature.prototype.getGeometryName = function () {
171 return this.geometryName_;
172 };
173 /**
174 * Get the feature's style. Will return what was provided to the
175 * {@link module:ol/Feature~Feature#setStyle} method.
176 * @return {import("./style/Style.js").StyleLike} The feature style.
177 * @api
178 */
179 Feature.prototype.getStyle = function () {
180 return this.style_;
181 };
182 /**
183 * Get the feature's style function.
184 * @return {import("./style/Style.js").StyleFunction|undefined} Return a function
185 * representing the current style of this feature.
186 * @api
187 */
188 Feature.prototype.getStyleFunction = function () {
189 return this.styleFunction_;
190 };
191 /**
192 * @private
193 */
194 Feature.prototype.handleGeometryChange_ = function () {
195 this.changed();
196 };
197 /**
198 * @private
199 */
200 Feature.prototype.handleGeometryChanged_ = function () {
201 if (this.geometryChangeKey_) {
202 unlistenByKey(this.geometryChangeKey_);
203 this.geometryChangeKey_ = null;
204 }
205 var geometry = this.getGeometry();
206 if (geometry) {
207 this.geometryChangeKey_ = listen(geometry, EventType.CHANGE, this.handleGeometryChange_, this);
208 }
209 this.changed();
210 };
211 /**
212 * Set the default geometry for the feature. This will update the property
213 * with the name returned by {@link module:ol/Feature~Feature#getGeometryName}.
214 * @param {Geometry|undefined} geometry The new geometry.
215 * @api
216 * @observable
217 */
218 Feature.prototype.setGeometry = function (geometry) {
219 this.set(this.geometryName_, geometry);
220 };
221 /**
222 * Set the style for the feature. This can be a single style object, an array
223 * of styles, or a function that takes a resolution and returns an array of
224 * styles. If it is `null` the feature has no style (a `null` style).
225 * @param {import("./style/Style.js").StyleLike} style Style for this feature.
226 * @api
227 * @fires module:ol/events/Event~BaseEvent#event:change
228 */
229 Feature.prototype.setStyle = function (style) {
230 this.style_ = style;
231 this.styleFunction_ = !style ? undefined : createStyleFunction(style);
232 this.changed();
233 };
234 /**
235 * Set the feature id. The feature id is considered stable and may be used when
236 * requesting features or comparing identifiers returned from a remote source.
237 * The feature id can be used with the
238 * {@link module:ol/source/Vector~VectorSource#getFeatureById} method.
239 * @param {number|string|undefined} id The feature id.
240 * @api
241 * @fires module:ol/events/Event~BaseEvent#event:change
242 */
243 Feature.prototype.setId = function (id) {
244 this.id_ = id;
245 this.changed();
246 };
247 /**
248 * Set the property name to be used when getting the feature's default geometry.
249 * When calling {@link module:ol/Feature~Feature#getGeometry}, the value of the property with
250 * this name will be returned.
251 * @param {string} name The property name of the default geometry.
252 * @api
253 */
254 Feature.prototype.setGeometryName = function (name) {
255 this.removeEventListener(getChangeEventType(this.geometryName_), this.handleGeometryChanged_);
256 this.geometryName_ = name;
257 this.addEventListener(getChangeEventType(this.geometryName_), this.handleGeometryChanged_);
258 this.handleGeometryChanged_();
259 };
260 return Feature;
261}(BaseObject));
262/**
263 * Convert the provided object into a feature style function. Functions passed
264 * through unchanged. Arrays of Style or single style objects wrapped
265 * in a new feature style function.
266 * @param {!import("./style/Style.js").StyleFunction|!Array<import("./style/Style.js").default>|!import("./style/Style.js").default} obj
267 * A feature style function, a single style, or an array of styles.
268 * @return {import("./style/Style.js").StyleFunction} A style function.
269 */
270export function createStyleFunction(obj) {
271 if (typeof obj === 'function') {
272 return obj;
273 }
274 else {
275 /**
276 * @type {Array<import("./style/Style.js").default>}
277 */
278 var styles_1;
279 if (Array.isArray(obj)) {
280 styles_1 = obj;
281 }
282 else {
283 assert(typeof /** @type {?} */ (obj).getZIndex === 'function', 41); // Expected an `import("./style/Style.js").Style` or an array of `import("./style/Style.js").Style`
284 var style = /** @type {import("./style/Style.js").default} */ (obj);
285 styles_1 = [style];
286 }
287 return function () {
288 return styles_1;
289 };
290 }
291}
292export default Feature;
293//# sourceMappingURL=Feature.js.map
\No newline at end of file