-
Notifications
You must be signed in to change notification settings - Fork 288
Expand file tree
/
Copy pathThemeVector.js
More file actions
668 lines (541 loc) · 21.9 KB
/
ThemeVector.js
File metadata and controls
668 lines (541 loc) · 21.9 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
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
/* Copyright© 2000 - 2025 SuperMap Software Co.Ltd. All rights reserved.
* This program are made available under the terms of the Apache License, Version 2.0
* which accompanies this distribution and is available at http://www.apache.org/licenses/LICENSE-2.0.html.*/
import {Geometry} from '../commontypes/Geometry';
import {Util as CommonUtil} from '../commontypes/Util';
import {Theme} from './feature/Theme';
import {Rectangle} from '../commontypes/geometry/Rectangle';
import {Point} from '../commontypes/geometry/Point';
import {Collection} from '../commontypes/geometry/Collection';
import {MultiPoint} from '../commontypes/geometry/MultiPoint';
import {LineString} from '../commontypes/geometry/LineString';
import {MultiLineString} from '../commontypes/geometry/MultiLineString';
import {LinearRing} from '../commontypes/geometry/LinearRing';
import {Polygon} from '../commontypes/geometry/Polygon';
import {MultiPolygon} from '../commontypes/geometry/MultiPolygon';
import {Curve} from '../commontypes/geometry/Curve';
import {GeoText} from '../commontypes/geometry/GeoText';
import {SmicPoint} from './levelRenderer/SmicPoint';
import {SmicBrokenLine} from './levelRenderer/SmicBrokenLine';
import {SmicText} from './levelRenderer/SmicText';
import {SmicRectangle} from './levelRenderer/SmicRectangle';
import {SmicPolygon} from './levelRenderer/SmicPolygon';
import {LonLat} from '../commontypes/LonLat';
/**
* @class FeatureThemeVector
* @aliasclass Feature.Theme.ThemeVector
* @deprecatedclass SuperMap.Feature.Theme.ThemeVector
* @classdesc 矢量专题要素类。
* @category Visualization Theme
* @extends FeatureTheme
* @param {FeatureVector} data - 用户数据,的类型为矢量数据 feature。
* @param {SuperMap.Layer} layer - 此专题要素所在图层。
* @param {Object} style - 样式。
* @param {Object} options - 创建专题要素时的可选参数。
* @param {number} [options.nodesClipPixel=2] - 节点抽稀像素距离,单位:像素。
* @param {boolean} [options.isHoverAble=true] - 图形是否可 hover。
* @param {boolean} [options.isMultiHover=true] - 是否使用多图形高亮,isHoverAble 为 true 时生效。
* @param {boolean} [options.isClickAble=true] - 图形是否可点击。
* @param {Object} [options.highlightStyle] - 高亮样式。
* @usage
*/
export class ThemeVector extends Theme {
constructor(data, layer, style, options, shapeOptions) {
super(data, layer);
//数据的 geometry 属性必须存在且类型是 Geometry 或其子类的类型
if (!data.geometry) {
return;
}
if (!(data.geometry instanceof Geometry)) {
return;
}
/**
* @member {Bounds} [FeatureThemeVector.prototype.dataBounds]
* @description 用户数据的(feature.geometry)地理范围。
*/
this.dataBounds = data.geometry.getBounds();
/**
* @member {number} [FeatureThemeVector.prototype.nodesClipPixel=2]
* @description 节点抽稀像素距离。
*/
this.nodesClipPixel = 2;
/**
* @member {boolean} [FeatureThemeVector.prototype.isHoverAble=true]
* @description 图形是否可 hover。
*/
this.isHoverAble = true;
/**
* @member {boolean} [FeatureThemeVector.prototype.isMultiHover=true]
* @description 是否使用多图形高亮,isHoverAble 为 true 时生效。
*/
this.isMultiHover = true;
/**
* @member {boolean} [FeatureThemeVector.prototype.isClickAble=true]
* @description 图形是否可点击。
*/
this.isClickAble = true;
/**
* @member {Object} [FeatureThemeVector.prototype.highlightStyle]
* @description 高亮样式。
*/
this.highlightStyle = null;
/**
* @member {Object} [FeatureThemeVector.prototype.shapeOptions]
* @description 添加到渲染器前修改 shape 的一些属性,非特殊情况通常不允许这么做。
*/
this.shapeOptions = {};
/**
* @member {Object} [FeatureThemeVector.prototype.style]
* @description 可视化图形的 style。在子类中规定其对象结构和默认属性值。
*/
this.style = style || {};
this.CLASS_NAME = "SuperMap.Feature.Theme.Vector";
this.style = style ? style : {};
if (options) {
CommonUtil.copyAttributesWithClip(this, options, ["shapeOptions", "dataBounds"])
}
if (shapeOptions) {
CommonUtil.copyAttributesWithClip(this.shapeOptions, shapeOptions);
}
//设置基础参数 dataBounds、lonlat、location
var geometry = data.geometry;
this.lonlat = this.dataBounds.getCenterLonLat();
this.location = this.getLocalXY(this.lonlat);
//将地理要素转为专题要素
if (geometry instanceof LinearRing) {
this.lineToTF(geometry);
} else if (geometry instanceof LineString) {
this.lineToTF(geometry);
} else if (geometry instanceof Curve) {
//独立几何体
} else if (geometry instanceof MultiPoint) {
this.multiPointToTF(geometry);
} else if (geometry instanceof MultiLineString) {
this.multiLineStringToTF(geometry);
} else if (geometry instanceof MultiPolygon) {
this.multiPolygonToTF(geometry);
} else if (geometry instanceof Polygon) {
this.polygonToTF(geometry);
} else if (geometry instanceof Collection) {
//独立几何体
} else if (geometry instanceof Point) {
this.pointToTF(geometry);
} else if (geometry instanceof Rectangle) {
this.rectangleToTF(geometry);
} else if (geometry instanceof GeoText) {
this.geoTextToTF(geometry);
}
}
/**
* @function FeatureThemeVector.prototype.destroy
* @override
*/
destroy() {
this.style = null;
this.dataBounds = null;
this.nodesClipPixel = null;
this.isHoverAble = null;
this.isMultiHover = null;
this.isClickAble = null;
this.highlightStyle = null;
this.shapeOptions = null;
super.destroy();
}
/**
* @function FeatureThemeVector.prototype.lineToTF
* @description 转换线和线环要素。
* @param {Geometry} geometry - 用户数据几何地理信息,这里必须是 GeometryLineString 或 GeometryLineRing。
*/
lineToTF(geometry) {
var components = geometry.components;
//节点像素坐标
var localLX = [];
//参考位置,参考中心为
var refLocal = [];
var location = this.location;
var pointList = [];
//节点抽稀距离
var nCPx = this.nodesClipPixel;
for (var i = 0; i < components.length; i++) {
var components_i = components[i];
refLocal = [];
localLX = this.getLocalXY(components_i);
refLocal[0] = localLX[0] - location[0];
refLocal[1] = localLX[1] - location[1];
//抽稀 - 2 px
if (pointList.length > 0) {
var lastLocalXY = pointList[pointList.length - 1];
if ((Math.abs(lastLocalXY[0] - refLocal[0]) <= nCPx) && (Math.abs(lastLocalXY[1] - refLocal[1]) <= nCPx)) {
continue;
}
}
//使用参考点
pointList.push(refLocal);
}
if (pointList.length < 2) {
return null;
}
//赋 style
var style = new Object();
style = CommonUtil.copyAttributesWithClip(style, this.style, ['pointList']);
style.pointList = pointList;
//创建图形
var shape = new SmicBrokenLine({
style: style,
clickable: this.isClickAble,
hoverable: this.isHoverAble
});
//设置高亮样式
if (this.highlightStyle) {
shape.highlightStyle = this.highlightStyle;
}
//设置参考中心,指定图形位置
shape.refOriginalPosition = this.location;
//储存数据 id 属性,用于事件
shape.refDataID = this.data.id;
//储存数据 id 属性,用于事件-多图形同时高亮
shape.isHoverByRefDataID = this.isMultiHover;
//添加到渲染器前修改 shape 的一些属性,非特殊情况通常不允许这么做
if (this.shapeOptions) {
CommonUtil.copyAttributesWithClip(shape, this.shapeOptions);
}
this.shapes.push(shape);
}
/**
* @function FeatureThemeVector.prototype.multiPointToTF
* @description 转多点要素。
* @param {Geometry} geometry - 用户数据几何地理信息,这里必须是 MultiPoint。
*/
multiPointToTF(geometry) {
/* //-- 不抽稀
var components = geometry.components;
for(var i = 0; i < components.length; i++){
var components_i = components[i];
this.pointToTF(components_i);
}
*/
var components = geometry.components;
//节点像素坐标
var localLX = [];
//参考位置,参考中心为
var refLocal = [];
var location = this.location;
var pointList = [];
//节点抽稀距离
var nCPx = this.nodesClipPixel;
for (var i = 0; i < components.length; i++) {
var components_i = components[i];
refLocal = [];
localLX = this.getLocalXY(components_i);
refLocal[0] = localLX[0] - location[0];
refLocal[1] = localLX[1] - location[1];
//抽稀
if (pointList.length > 0) {
var lastLocalXY = pointList[pointList.length - 1];
if ((Math.abs(lastLocalXY[0] - refLocal[0]) <= nCPx) && (Math.abs(lastLocalXY[1] - refLocal[1]) <= nCPx)) {
continue;
}
}
//使用参考点
pointList.push(refLocal);
//赋 style
var style = new Object();
style.r = 6; //防止漏设此参数,默认 6 像素
style = CommonUtil.copyAttributesWithClip(style, this.style);
style.x = refLocal[0];
style.y = refLocal[1];
//创建图形
var shape = new SmicPoint({
style: style,
clickable: this.isClickAble,
hoverable: this.isHoverAble
});
//设置高亮样式
if (this.highlightStyle) {
shape.highlightStyle = this.highlightStyle;
}
//设置参考中心,指定图形位置
shape.refOriginalPosition = location;
//储存数据 id 属性,用于事件
shape.refDataID = this.data.id;
//储存数据 id 属性,用于事件-多图形同时高亮
shape.isHoverByRefDataID = this.isMultiHover;
//修改一些 shape 可选属性,通常不需要这么做
if (this.shapeOptions) {
CommonUtil.copyAttributesWithClip(shape, this.shapeOptions);
}
this.shapes.push(shape);
}
}
/**
* @function FeatureThemeVector.prototype.multiLineStringToTF
* @description 转换多线要素。
* @param {Geometry} geometry - 用户数据几何地理信息,这里必须是 MultiLineString。
*/
multiLineStringToTF(geometry) {
var components = geometry.components;
for (var i = 0; i < components.length; i++) {
var components_i = components[i];
this.lineToTF(components_i);
}
}
/**
* @function FeatureThemeVector.prototype.multiPolygonToTF
* @description 转换多面要素。
* @param {Geometry} geometry - 用户数据几何地理信息,这里必须是 MultiPolygon。
*/
multiPolygonToTF(geometry) {
var components = geometry.components;
for (var i = 0; i < components.length; i++) {
var components_i = components[i];
this.polygonToTF(components_i);
}
}
/**
* @function FeatureThemeVector.prototype.pointToTF
* @description 转换点要素。
* @param {Geometry} geometry - 用户数据几何地理信息,这里必须是 Point。
*/
pointToTF(geometry) {
//参考位置,参考中心为
var location = this.location;
//geometry 像素坐标
var localLX = this.getLocalXY(geometry);
//赋 style
var style = new Object();
style.r = 6; //防止漏设此参数,默认 6 像素
style = CommonUtil.copyAttributesWithClip(style, this.style);
style.x = localLX[0] - location[0];
style.y = localLX[1] - location[1];
//创建图形
var shape = new SmicPoint({
style: style,
clickable: this.isClickAble,
hoverable: this.isHoverAble
});
//设置高亮样式
if (this.highlightStyle) {
shape.highlightStyle = this.highlightStyle;
}
//设置参考中心,指定图形位置
shape.refOriginalPosition = location;
//储存数据 id 属性,用于事件
shape.refDataID = this.data.id;
//储存数据 id 属性,用于事件-多图形同时高亮
shape.isHoverByRefDataID = this.isMultiHover;
//修改一些 shape 可选属性,通常不需要这么做
if (this.shapeOptions) {
CommonUtil.copyAttributesWithClip(shape, this.shapeOptions);
}
this.shapes.push(shape);
}
/**
* @function FeatureThemeVector.prototype.polygonToThemeFeature
* @description 转换面要素。
* @param {Geometry} geometry - 用户数据几何地理信息,这里必须是 Polygon。
*/
polygonToTF(geometry) {
var components = geometry.components;
//节点像素坐标
var localLX = [];
//参考位置,参考中心为
var refLocal = [];
var location = this.location;
var pointList = [];
//岛洞
var holePolygonPointList = [];
var holePolygonPointLists = [];
//节点抽稀距离
var nCPx = this.nodesClipPixel;
for (var i = 0; i < components.length; i++) {
var components_i = components[i].components;
if (i === 0) {
// 第一个 component 正常绘制
pointList = [];
for (var j = 0; j < components_i.length; j++) {
refLocal = [];
localLX = this.getLocalXY(components_i[j]);
refLocal[0] = localLX[0] - location[0];
refLocal[1] = localLX[1] - location[1];
//抽稀 - 2 px
if (pointList.length > 0) {
var lastLocalXY = pointList[pointList.length - 1];
if ((Math.abs(lastLocalXY[0] - refLocal[0]) <= nCPx) && (Math.abs(lastLocalXY[1] - refLocal[1]) <= nCPx)) {
continue;
}
}
//使用参考点
pointList.push(refLocal);
}
} else {
// 其它 component 作为岛洞
holePolygonPointList = [];
for (var k = 0; k < components_i.length; k++) {
refLocal = [];
localLX = this.getLocalXY(components_i[k]);
refLocal[0] = localLX[0] - location[0];
refLocal[1] = localLX[1] - location[1];
//抽稀 - 2 px
if (holePolygonPointList.length > 0) {
var lastXY = holePolygonPointList[holePolygonPointList.length - 1];
if ((Math.abs(lastXY[0] - refLocal[0]) <= nCPx) && (Math.abs(lastXY[1] - refLocal[1]) <= nCPx)) {
continue;
}
}
//使用参考点
holePolygonPointList.push(refLocal);
}
}
if (holePolygonPointList.length < 2) {
continue;
}
holePolygonPointLists.push(holePolygonPointList);
}
if (pointList.length < 2) {
return;
}
//赋 style
var style = {};
style = CommonUtil.copyAttributesWithClip(style, this.style, ['pointList']);
style.pointList = pointList;
//创建图形
var shape = new SmicPolygon({
style: style,
clickable: this.isClickAble,
hoverable: this.isHoverAble
});
//设置高亮样式
if (this.highlightStyle) {
shape.highlightStyle = this.highlightStyle;
}
//设置参考中心,指定图形位置
shape.refOriginalPosition = this.location;
//储存数据 id 属性,用于事件
shape.refDataID = this.data.id;
//储存数据 id 属性,用于事件-多图形同时高亮
shape.isHoverByRefDataID = this.isMultiHover;
//岛洞面
if (holePolygonPointLists.length > 0) {
shape.holePolygonPointLists = holePolygonPointLists;
}
//修改一些 shape 可选属性,通常不需要这么做
if (this.shapeOptions) {
CommonUtil.copyAttributesWithClip(shape, this.shapeOptions);
}
this.shapes.push(shape);
}
/**
* @function FeatureThemeVector.prototype.rectangleToTF
* @description 转换矩形要素。
* @param {Geometry} geometry - 用户数据几何地理信息,这里必须是 Rectangle。
*/
rectangleToTF(geometry) {
//参考位置,参考中心为
var location = this.location;
var ll = new LonLat(geometry.x, geometry.y);
//地图分辨率
var res = this.layer.map.getResolution();
//geometry 像素坐标
var localLX = this.getLocalXY(ll);
//赋 style
var style = new Object();
style.r = 6; //防止漏设此参数,默认 6 像素
style = CommonUtil.copyAttributesWithClip(style, this.style);
style.x = localLX[0] - location[0];
// Rectangle 使用左下角定位, SmicRectangle 使用左上角定位,需要转换
style.y = (localLX[1] - location[1]) - 2 * geometry.width / res;
style.width = geometry.width / res;
style.height = geometry.height / res;
//创建图形
var shape = new SmicRectangle({
style: style,
clickable: this.isClickAble,
hoverable: this.isHoverAble
});
//设置高亮样式
if (this.highlightStyle) {
shape.highlightStyle = this.highlightStyle;
}
//设置参考中心,指定图形位置
shape.refOriginalPosition = location;
//储存数据 id 属性,用于事件
shape.refDataID = this.data.id;
//储存数据 id 属性,用于事件-多图形同时高亮
shape.isHoverByRefDataID = this.isMultiHover;
//修改一些 shape 可选属性,通常不需要这么做
if (this.shapeOptions) {
CommonUtil.copyAttributesWithClip(shape, this.shapeOptions);
}
this.shapes.push(shape);
}
/**
* @function FeatureThemeVector.prototype.geoTextToTF
* @description 转换文本要素。
* @param {Geometry} geometry - 用户数据几何地理信息,这里必须是 GeoText。
*/
geoTextToTF(geometry) {
//参考位置,参考中心为
var location = this.location;
//geometry 像素坐标
var localLX = this.getLocalXY(geometry);
//赋 style
var style = new Object();
style.r = 6; //防止漏设此参数,默认 6 像素
style = CommonUtil.copyAttributesWithClip(style, this.style, ["x", "y", "text"]);
style.x = localLX[0] - location[0];
style.y = localLX[1] - location[1];
style.text = geometry.text;
//创建图形
var shape = new SmicText({
style: style,
clickable: this.isClickAble,
hoverable: this.isHoverAble
});
//设置高亮样式
if (this.highlightStyle) {
shape.highlightStyle = this.highlightStyle;
}
//设置参考中心,指定图形位置
shape.refOriginalPosition = location;
//储存数据 id 属性,用于事件
shape.refDataID = this.data.id;
//储存数据 id 属性,用于事件-多图形同时高亮
shape.isHoverByRefDataID = this.isMultiHover;
//修改一些 shape 可选属性,通常不需要这么做
if (this.shapeOptions) {
CommonUtil.copyAttributesWithClip(shape, this.shapeOptions);
}
this.shapes.push(shape);
}
/**
* @function FeatureThemeVector.prototype.updateAndAddShapes
* @description 修改位置,针对地图平移操作,地图漫游操作后调用此函数。
*/
updateAndAddShapes() {
var newLocalLX = this.getLocalXY(this.lonlat);
this.location = newLocalLX;
var render = this.layer.renderer;
for (var i = 0, len = this.shapes.length; i < len; i++) {
var shape = this.shapes[i];
//设置参考中心,指定图形位置
shape.refOriginalPosition = newLocalLX;
render.addShape(shape);
}
}
/**
* @function FeatureThemeVector.prototype.getShapesCount
* @description 获得专题要素中可视化图形的数量。
* @returns {number} 可视化图形的数量。
*/
getShapesCount() {
return this.shapes.length;
}
/**
* @function FeatureThemeVector.prototype.getLocalXY
* @description 地理坐标转为像素坐标。
* @param {LonLat} lonlat - 专题要素地理位置。
*/
getLocalXY(lonlat) {
return this.layer.getLocalXY(lonlat);
}
}