Feature特征
# 概述
在 Openlayers 中,Feature
类是一个用于地理特征的矢量对象,具有几何形状和其他属性,类似于 GeoJSON 等矢量文件格式中的特征。它主要包含两个重要属性geometry
几何对象和style
样式,提供getGeometry
和setGeometry
方法用于获取和设置几何对象,以及getStyle
和setStyle
处理和样式相关的。
Feature
类继承于BaseObject
类,关于BaseObject
类,可以参考这篇文章
# 源码分析
# Feature
类的源码
Feature
类的源码实现如下:
class Feature extends BaseObject {
constructor(geometryOrProperties) {
super();
this.on;
this.once;
this.un;
this.id_ = undefined;
this.geometryName_ = "geometry";
this.style_ = null;
this.styleFunction_ = undefined;
this.geometryChangeKey_ = null;
this.addChangeListener(this.geometryName_, this.handleGeometryChanged_);
if (geometryOrProperties) {
if (typeof geometryOrProperties.getSimplifiedGeometry === "function") {
const geometry = geometryOrProperties;
this.setGeometry(geometry);
} else {
const properties = geometryOrProperties;
this.setProperties(properties);
}
}
}
clone() {
const clone = new Feature(
this.hasProperties() ? this.getProperties() : null
);
clone.setGeometryName(this.getGeometryName());
const geometry = this.getGeometry();
if (geometry) {
clone.setGeometry(geometry.clone());
}
const style = this.getStyle();
if (style) {
clone.setStyle(style);
}
return clone;
}
getGeometry() {
return this.get(this.geometryName_);
}
getId() {
return this.id_;
}
getGeometryName() {
return this.geometryName_;
}
getStyle() {
return this.style_;
}
getStyleFunction() {
return this.styleFunction_;
}
handleGeometryChange_() {
this.changed();
}
handleGeometryChanged_() {
if (this.geometryChangeKey_) {
unlistenByKey(this.geometryChangeKey_);
this.geometryChangeKey_ = null;
}
const geometry = this.getGeometry();
if (geometry) {
this.geometryChangeKey_ = listen(
geometry,
EventType.CHANGE,
this.handleGeometryChange_,
this
);
}
this.changed();
}
setGeometry(geometry) {
this.set(this.geometryName_, geometry);
}
setStyle(style) {
this.style_ = style;
this.styleFunction_ = !style ? undefined : createStyleFunction(style);
this.changed();
}
setId(id) {
this.id_ = id;
this.changed();
}
setGeometryName(name) {
this.removeChangeListener(this.geometryName_, this.handleGeometryChanged_);
this.geometryName_ = name;
this.addChangeListener(this.geometryName_, this.handleGeometryChanged_);
this.handleGeometryChanged_();
}
}
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
# Feature
类的构造函数
Feature
类构造函数接受一个参数,参数可以是直接传递的一个几何对象,或者是一个包含属性的对象字面量。如果传递的是对象字面量,可以包含一个与 geometry
键关联的几何对象。构造函数内部初始化了this.id_
为undefined
,this.geometryName_
为geometry
以及this.style_
、this.styleFunction_
和this.geometryChangeKey_
为null
,然后调用this.addChangeListener
方法监听this.geometryName_
,当它发生改变时,会触发this.handleGeometryChanged_
事件。判断,若参数geometryProperties
上存在getSimplifiedGeometry
方法,这说明参数是一个几何对象,然后会调用this.setGeometry
方法设置几何对象;否则说明参数是一个包含几何对象的对象字面量,则调用this.setProperties
方法设置属性。
# Feature
类的主要方法
Feature
类的主要方法如下:
clone
方法:该方法用于复制当前feature
,首先会实例化Feature
类,调用this.hasProperties
方法判断当前对象是否有属性,若存在,则调用getProperties
方法获取对象上的属性,否则取值是null
;hasProperties
和getProperties
方法都是在Observable
类中定义的,关于Observable
类可以参考这篇文章;然后调用this.getGeometryName
获取几何对象名称,然后通过setGeometryName
方法设置实例对象的几何对象名称;然后调用this.getGeometry
方法获取几何对象,若它存在,则设置实例对象clone
的几何对象;调用this.getStyle
方法获取feature
的样式,若它存在,则设置实例对象clone
的样式,最后返回实例对象。getGeometry
方法:通过this.get
方法和this.geometryName_
属性,获取几何对象getId
方法:获取当前feature
的this.id_
getGeometryName
方法:获取当前feature
的this.geometryName_
getStyle
方法:获取当前feature
的this.style_
getStyleFunction
方法:获取当前feature
的this.styleFunction_
handleGeometryChange_
方法:本质上就是调用this.changed
方法handleGeometryChanged_
方法:该方法就是用于监听几何对象名称的改变,会先判断,若this.geometryChanged_
存在,则先调用unlisten
方法移除监听,并将其置为null
;调用this.getGeometry
方法获取几何对象,若其存在,则调用listen
方法注册几何对象的监听事件this.handleGeometryChange_
;最后调用this.changed
方法。setGeometry
方法:设置几何对象,本质上是调用的this.set
方法setStyle
方法:设置feature
的样式,参数style
会赋值给this.style_
,若参数style
不存在,则this.styleFunction_
为undefined
;否则调用createStyleFunction
创建一个样式函数,其返回值也是一个函数,最后会调用this.changed
方法。setId
方法:设置this.id_
,最后会调用this.changed
方法。setGeometryName
方法:设置几何对象的名称,会先移除监听,然后设置this.geometryName_
为参数name
;然后注册几何对象名称的监听,最后调用this.handleGeometryChanged_
方法。
# 总结
Feature
类是一个很重要的知识,在矢量图层中绘制、编辑几何对象时都会用到,可以将其理解为几何对象在矢量图层上展示的一个载体,用于将几何对象和样式结合起来在地图上显示。