forked from MapGIS/WebClient-JavaScript
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGeoFeatureSource.js
More file actions
284 lines (254 loc) · 10.3 KB
/
GeoFeatureSource.js
File metadata and controls
284 lines (254 loc) · 10.3 KB
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
import {Zondy} from '../../service/common/Base';
import {ThemeSource} from './ThemeSource';
import {copyAttributesWithClip} from '../../service/common/Util';
import {ShapeFactory} from '../../common/overlay/feature/ShapeFactory';
import {ThemeVector} from '../../common/overlay/ThemeVector';
import {FeatureSet} from '../../service/common/FeatureSet';
import {Rectangle} from '../../service/common/Rectangle';
/**
* @class Zondy.Source.GeoFeatureSource
* @classdesc 地理几何专题要素型专题图层基类。
* @param {string} name - 图层名称。
* @param {Object} options - 参数。
* @param {ol.Map} options.map - 当前 OpenLayers Map 对象。
* @param {string} [options.id] - 专题图层 ID
* @param {number} [options.opacity=1] - 图层透明度。
* @param {ol.proj.Projection} [options.projection] - 投影信息。
* @param {number} [options.ratio=1.5] - 视图比,1 表示画布是地图视口的大小,2 表示地图视口的宽度和高度的两倍,依此类推。 必须是 1 或更高。
* @param {Array} [options.resolutions] - 分辨率数组。
* @param {ol.source.State} [option.state] - 资源状态。
* @param {Object} [options.style] - 专题图样式。
* @param {Object} [options.styleGroups] - 各专题类型样式组。
* @param {boolean} [options.isHoverAble=false] - 是否开启 hover 事件。
* @param {Object} [options.highlightStyle] - 开启 hover 事件后,触发的样式风格。
* @extends {Zondy.Source.ThemeSource}
*/
class GeoFeatureSource extends ThemeSource {
constructor(name, options) {
super(name, options);
this.cache = options.cache || {};
this.cacheFields = options.cacheFields || [];
this.style = options.style || {};
this.maxCacheCount = options.maxCacheCount || 0;
this.isCustomSetMaxCacheCount = options.isCustomSetMaxCacheCount || false;
this.nodesClipPixel = options.nodesClipPixel || 2;
this.isHoverAble = options.isHoverAble || false;
this.isMultiHover = options.isMultiHover || false;
this.isClickAble = options.isClickAble || true;
this.highlightStyle = options.highlightStyle || null;
this.isAllowFeatureStyle = options.isAllowFeatureStyle || false;
}
/**
* @function Zondy.Source.GeoFeatureSource.prototype.destroy
* @description 释放资源,将引用资源的属性置空。
*/
destroy() {
this.maxCacheCount = null;
this.isCustomSetMaxCacheCount = null;
this.nodesClipPixel = null;
this.isHoverAble = null;
this.isMultiHover = null;
this.isClickAble = null;
this.cache = null;
this.cacheFields = null;
this.style = null;
this.highlightStyle = null;
this.isAllowFeatureStyle = null;
}
/**
* @function Zondy.Source.GeoFeatureSource.prototype.addFeatures
* @description 添加要素。
* @param {Object} features - 要素对象。
*/
addFeatures(features) {
var me = this;
this.dispatchEvent({
type: 'beforefeaturesadded',
value: {features: features}
});
if (features instanceof FeatureSet) {
var attrs = null;
var attstruct = features.AttStruct;
var feaArr = features.SFEleArray;
if (feaArr != null && feaArr.length > 0) {
for (var j = 0; j < feaArr.length; j++) {
var feature = feaArr[j];
if (feature.AttValue != null && feature.AttValue.length > 0) {
var attrs = {};
for (var i = 0; i < feature.AttValue.length; i++) {
attrs[(attstruct.FldName)[i]] = (feature.AttValue)[i];
}
attrs['FID'] = feature.FID;
}
feature.attributes = attrs;
me.features.push(feature);
}
}
}
if (!this.isCustomSetMaxCacheCount) {
this.maxCacheCount = this.features.length * 5;
}
//绘制专题要素
if (this.renderer) {
this.changed();
}
}
/**
* @function Zondy.Source.GeoFeatureSource.prototype.removeFeatures
* @description 从专题图中删除 feature。这个函数删除所有传递进来的矢量要素。
* @param {Zondy.Feature.Vector} features - 要删除的要素对象。
*/
removeFeatures(features) { // eslint-disable-line no-unused-vars
this.clearCache();
ThemeSource.prototype.removeFeatures.apply(this, arguments);
}
/**
* @function Zondy.Source.GeoFeatureSource.prototype.removeAllFeatures
* @description 清除当前图层所有的矢量要素。
*/
removeAllFeatures() {
this.clearCache();
ThemeSource.prototype.removeAllFeatures.apply(this, arguments);
}
/**
* @function Zondy.Source.GeoFeatureSource.prototype.redrawThematicFeatures
* @description 重绘所有专题要素。
* @param {Object} extent - 视图范围数据。
*/
redrawThematicFeatures(extent) {
//获取高亮专题要素对应的用户 id
var hoverone = this.renderer.getHoverOne();
var hoverFid = null;
if (hoverone && hoverone.refDataID) {
hoverFid = hoverone.refDataID;
}
//清除当前所有可视元素
this.renderer.clearAll();
var features = this.features;
var cache = this.cache;
var cacheFields = this.cacheFields;
var cmZoom = this.map.getView().getZoom();
var maxCC = this.maxCacheCount;
var bounds = new Rectangle(extent[0], extent[1], extent[2], extent[3]);
for (var i = 0, len = features.length; i < len; i++) {
var feature = features[i];
var feaBounds = feature.bound;
//剔除当前视图(地理)范围以外的数据
if (bounds && !bounds.intersectsBounds(feaBounds)) {
continue;
}
//缓存字段
var fields = feature.FID + "_zoom_" + cmZoom.toString();
var thematicFeature;
//判断专题要素缓存是否存在
if (cache[fields]) {
cache[fields].updateAndAddShapes();
} else {
//如果专题要素缓存不存在,创建专题要素
thematicFeature = this.createThematicFeature(features[i]);
//检查 thematicFeature 是否有可视化图形
if (thematicFeature.getShapesCount() < 1) {
continue;
}
//加入缓存
cache[fields] = thematicFeature;
cacheFields.push(fields);
//缓存数量限制
if (cacheFields.length > maxCC) {
var fieldsTemp = cacheFields[0];
cacheFields.splice(0, 1);
delete cache[fieldsTemp];
}
}
}
this.renderer.render();
//地图漫游后,重新高亮图形
if (hoverFid && this.isHoverAble && this.isMultiHover) {
var hShapes = this.getShapesByFeatureID(hoverFid);
this.renderer.updateHoverShapes(hShapes);
}
}
/**
* @function Zondy.Source.GeoFeatureSource.prototype.createThematicFeature
* @description 创建专题要素。
* @param {Object} feature - 要素对象。
*/
createThematicFeature(feature) {
var style = copyAttributesWithClip(this.style);
//创建专题要素时的可选参数
var options = {};
options.nodesClipPixel = this.nodesClipPixel;
options.isHoverAble = this.isHoverAble;
options.isMultiHover = this.isMultiHover;
options.isClickAble = this.isClickAble;
options.highlightStyle = ShapeFactory.transformStyle(this.highlightStyle);
//将数据转为专题要素(Vector)
var thematicFeature = new ThemeVector(feature, this, ShapeFactory.transformStyle(style), options);
//直接添加图形到渲染器
for (var m = 0; m < thematicFeature.shapes.length; m++) {
this.renderer.addShape(thematicFeature.shapes[m]);
}
return thematicFeature;
}
canvasFunctionInternal_(extent, resolution, pixelRatio, size, projection) { // eslint-disable-line no-unused-vars
return ThemeSource.prototype.canvasFunctionInternal_.apply(this, arguments);
}
/**
* @function Zondy.Source.GeoFeatureSource.prototype.clearCache
* @description 清除缓存。
*/
clearCache() {
this.cache = {};
this.cacheFields = [];
}
/**
* @function Zondy.Source.GeoFeatureSource.prototype.clear
* @description 清除的内容包括数据(features)、专题要素、缓存。
*/
clear() {
this.renderer.clearAll();
this.renderer.refresh();
this.removeAllFeatures();
this.clearCache();
}
/**
* @function Zondy.Source.GeoFeatureSource.prototype.getCacheCount
* @description 获取当前缓存数量。
* @returns {number} 返回当前缓存数量。
*/
getCacheCount() {
return this.cacheFields.length;
}
/**
* @function Zondy.Source.GeoFeatureSource.prototype.setMaxCacheCount
* @param {number} cacheCount - 缓存总数。
* @description 设置最大缓存条数。
*/
setMaxCacheCount(cacheCount) {
if (!isNaN(cacheCount)) {
this.maxCacheCount = cacheCount;
this.isCustomSetMaxCacheCount = true;
}
}
/**
* @function Zondy.Source.GeoFeatureSource.prototype.setMaxCacheCount
* @param {number} featureID - 要素 ID。
* @description 通过 FeatureID 获取 feature 关联的所有图形。如果不传入此参数,函数将返回所有图形。
*/
getShapesByFeatureID(featureID) {
var list = [];
var shapeList = this.renderer.getAllShapes();
if (!featureID) {
return shapeList
}
for (var i = 0, len = shapeList.length; i < len; i++) {
var si = shapeList[i];
if (si.refDataID && featureID === si.refDataID) {
list.push(si);
}
}
return list;
}
}
export {GeoFeatureSource};
Zondy.Source.GeoFeatureSource = GeoFeatureSource;