forked from MapGIS/WebClient-JavaScript
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGeoFeatureThemeLayer.js
More file actions
301 lines (272 loc) · 11.8 KB
/
GeoFeatureThemeLayer.js
File metadata and controls
301 lines (272 loc) · 11.8 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
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
import mapboxgl from '@mapgis/mapbox-gl';
import { Zondy } from '../../service/common/Base';
import { ThemeLayer } from './ThemeLayer';
import { extend } from '../../service/common/Util';
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';
/**
* @class Zondy.Map.GeoFeatureThemeLayer
* @classdesc 地理几何专题要素型专题图层。
* @param {string} name - 图层名。
* @param {Object} options - 参数。
* @param {mapboxgl.Map} options.map - 当前 mapboxgl map 对象。
* @param {string} [options.id] - 专题图层 ID。
* @param {boolean} [options.loadWhileAnimating=true] - 是否实时重绘。
* @param {number} [options.nodesClipPixel=2] - 节点抽稀像素距离。
* @param {boolean} [options.isHoverAble=false] - 图形是否在 hover 时高亮。
* @param {boolean} [options.isMultiHover=false] - 是否多图形同时高亮,用于高亮同一个数据对应的所有图形(如:多面)。
* @param {boolean} [options.isClickAble=true] - 图形是否可点击。
* @param {boolean} [options.isAllowFeatureStyle=false] - 是否允许 feature 样式(style) 中的有效属性应用到专题图层。禁止对专题要素使用数据(feature)的 style。此属性可强制将数据 feature 的 style 中有效属性应用到专题要素上,且拥有比图层 style 和 styleGroups 更高的优先级,使专题要素的样式脱离专题图层的控制。可以通过此方式实现对特殊数据(feature) 对应专题要素赋予独立 style。
* @param {number} [options.opacity=1] - 图层透明度。
* @extends {Zondy.Map.ThemeLayer}
* @fires Zondy.Map.GeoFeatureThemeLayer#beforefeaturesadded
*/
class GeoFeatureThemeLayer extends ThemeLayer {
constructor(name, options) {
super(name, options);
/**
* @member {string} Zondy.Map.GeoFeatureThemeLayer.prototype.name
* @description 专题图图层名称。
*/
this.name = null;
/**
* @member {Zondy.ThemeStyle} Zondy.Map.GeoFeatureThemeLayer.prototype.style
* @description 专题图图层全局样式。
*/
this.style = null;
/**
* @member {Zondy.ThemeStyle} Zondy.Map.GeoFeatureThemeLayer.prototype.highlightStyle
* @description 专题图图层高亮样式。
*/
this.highlightStyle = null;
/**
* @member {number} [Zondy.Map.GeoFeatureThemeLayer.prototype.nodesClipPixel=2]
* @description 节点抽稀像素距离。
*/
this.nodesClipPixel = 2;
/**
* @member {boolean} [Zondy.Map.GeoFeatureThemeLayer.prototype.isHoverAble=false]
* @description 图形是否在 hover 时高亮。
*/
this.isHoverAble = false;
/**
* @member {boolean} [Zondy.Map.GeoFeatureThemeLayer.prototype.isMultiHover=false]
* @description 是否多图形同时高亮,用于高亮同一个数据对应的所有图形(如:多面)。
*/
this.isMultiHover = false;
/**
* @member {boolean} [Zondy.Map.GeoFeatureThemeLayer.prototype.isClickAble=true]
* @description 图形是否可点击。
*/
this.isClickAble = true;
/**
* @member {boolean} [Zondy.Map.GeoFeatureThemeLayer.prototype.isAllowFeatureStyle=false]
* @description 是否允许 feature 样式(style) 中的有效属性应用到专题图层。</br>禁止对专题要素使用数据(feature)的 style。此属性可强制将数据 feature 的 style 中有效属性应用到专题要素上,且拥有比图层 style 和 styleGroups 更高的优先级,使专题要素的样式脱离专题图层的控制。可以通过此方式实现对特殊数据(feature) 对应专题要素赋予独立 style。
*/
this.isAllowFeatureStyle = false;
extend(this, options);
this.cache = options.cache || {};
this.cacheFields = options.cacheFields || [];
this.maxCacheCount = options.maxCacheCount || 0;
this.isCustomSetMaxCacheCount = options.isCustomSetMaxCacheCount === undefined ? false : options.isCustomSetMaxCacheCount;
}
/**
* @function addFeatures
* @description 添加要素
* @param features - {Object} 只支持FeatureSet
*/
addFeatures(features) {
var me = this;
mapboxgl.Evented.prototype.fire('beforefeaturesadded', {
features: features
});
if (features && (true || features instanceof FeatureSet)) {
var attrs = null;
var LabelDots = features.LabelDots;
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;
LabelDots && LabelDots[j] && (feature.LabelDot = LabelDots[j]);
me.features.push(feature);
}
}
}
if (!this.isCustomSetMaxCacheCount) {
this.maxCacheCount = this.features.length * 5;
}
//绘制专题要素
if (this.renderer) {
this.redrawThematicFeatures(this.map.getBounds());
}
}
/**
* @function Zondy.Map.GeoFeatureThemeLayer.prototype.removeFeatures
* @description 从专题图中删除 feature。这个函数删除所有传递进来的矢量要素。
* @param {Zondy.Feature.Vector} features - 要删除的要素对象。
*/
removeFeatures(features) {
// eslint-disable-line no-unused-vars
this.clearCache();
ThemeLayer.prototype.removeFeatures.apply(this, arguments);
}
/**
* @function Zondy.Map.GeoFeatureThemeLayer.prototype.removeAllFeatures
* @description 清除当前图层所有的矢量要素。
*/
removeAllFeatures() {
this.clearCache();
ThemeLayer.prototype.removeAllFeatures.apply(this, arguments);
}
/**
* @function Zondy.Map.GeoFeatureThemeLayer.prototype.redrawThematicFeatures
* @description 重绘所有专题要素。
* @param {mapboxgl.LngLatBounds} extent - 重绘的范围。
*/
redrawThematicFeatures(extent) {
// eslint-disable-line no-unused-vars
this.clearCache();
//获取高亮专题要素对应的用户 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.getZoom();
var maxCC = this.maxCacheCount;
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.Map.GeoFeatureThemeLayer.prototype.createThematicFeature
* @description 创建专题要素。
* @param {Zondy.Feature.Vector} 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;
}
/**
* @function Zondy.Map.GeoFeatureThemeLayer.prototype.clearCache
* @description 清除缓存。
*/
clearCache() {
this.cache = {};
this.cacheFields = [];
}
/**
* @function Zondy.Map.GeoFeatureThemeLayer.prototype.clear
* @description 清除的内容包括数据(features)、专题要素和缓存。
*/
clear() {
this.renderer.clearAll();
this.renderer.refresh();
this.removeAllFeatures();
this.clearCache();
}
/**
* @function Zondy.Map.GeoFeatureThemeLayer.prototype.getCacheCount
* @description 获取当前缓存数量。
* @returns {number} 当前缓存数量。
*/
getCacheCount() {
return this.cacheFields.length;
}
/**
* @function Zondy.Map.GeoFeatureThemeLayer.prototype.setMaxCacheCount
* @param {number} [cacheCount] - 缓存总数。
* @description 设置最大缓存条数。
*/
setMaxCacheCount(cacheCount) {
if (!isNaN(cacheCount)) {
this.maxCacheCount = cacheCount;
this.isCustomSetMaxCacheCount = true;
}
}
/**
* @function Zondy.Map.GeoFeatureThemeLayer.prototype.setMaxCacheCount
* @param {number} [featureID=si.refDataID] - 要素 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 { GeoFeatureThemeLayer };
Zondy.Map.GeoFeatureThemeLayer = GeoFeatureThemeLayer;