forked from maptalks/maptalks.js
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGeometry.ts
More file actions
2069 lines (1941 loc) · 63.1 KB
/
Geometry.ts
File metadata and controls
2069 lines (1941 loc) · 63.1 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
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
import { GEOMETRY_COLLECTION_TYPES, NUMERICAL_PROPERTIES } from '../core/Constants';
import Class from '../core/Class';
import Eventable, { BaseEventParamsType, HandlerFnResultType } from '../core/Eventable';
import JSONAble from '../core/JSONAble';
import Handlerable from '../handler/Handlerable';
import {
extend,
isNil,
isString,
isNumber,
isObject,
forEachCoord,
flash
} from '../core/util';
import { extendSymbol, getSymbolHash } from '../core/util/style';
import { loadGeoSymbol } from '../core/mapbox';
import { convertResourceUrl, getExternalResources } from '../core/util/resource';
import { replaceVariable, describeText } from '../core/util/strings';
import { isTextSymbol } from '../core/util/marker';
import Coordinate from '../geo/Coordinate';
import Point from '../geo/Point';
import Extent from '../geo/Extent';
import PointExtent from '../geo/PointExtent';
import Painter from '../renderer/geometry/Painter';
import CollectionPainter from '../renderer/geometry/CollectionPainter';
import SpatialReference from '../map/spatial-reference/SpatialReference';
import { isFunctionDefinition } from '../core/mapbox';
import { getDefaultBBOX, pointsBBOX } from '../core/util/bbox';
import { SizeLike } from '../geo/Size';
import type { ProjectionType } from '../geo/projection';
import OverlayLayer, { addGeometryFitViewOptions } from '../layer/OverlayLayer'
import GeometryCollection from './GeometryCollection'
import type { Map } from '../map';
import { WithNull } from '../types/typings';
import { InfoWindowOptionsType } from '../ui/InfoWindow';
import { getMinMaxAltitude } from '../core/util/path';
const TEMP_POINT0 = new Point(0, 0);
const TEMP_EXTENT = new PointExtent();
const TEMP_PROPERTIES = {};
function validateExtent(extent: Extent): boolean {
if (!extent) {
return false;
}
const { xmin, ymin, xmax, ymax } = extent;
return (xmax - xmin > 0 && ymax - ymin > 0);
}
/**
* @property {Object} options - geometry options
* @property {Boolean} [options.id=null] - id of the geometry
* @property {Boolean} [options.visible=true] - whether the geometry is visible.
* @property {Boolean} [options.editable=true] - whether the geometry can be edited.
* @property {Boolean} [options.interactive=true] - whether the geometry can be interactived.
* @property {String} [options.cursor=null] - cursor style when mouseover the geometry, same as the definition in CSS.
* @property {String} [options.measure=EPSG:4326] - the measure code for the geometry, defines {@tutorial measureGeometry how it can be measured}.
* @property {Boolean} [options.draggable=false] - whether the geometry can be dragged.
* @property {Boolean} [options.dragShadow=true] - if true, during geometry dragging, a shadow will be dragged before geometry was moved.
* @property {Boolean} [options.dragOnAxis=null] - if set, geometry can only be dragged along the specified axis, possible values: x, y
* @property {Number} [options.zIndex=undefined] - geometry's initial zIndex
* @property {Boolean} [options.antiMeridian=false] - geometry's antiMeridian
* @memberOf Geometry
* @instance
*/
const options: GeometryOptionsType = {
'id': null,
'visible': true,
'interactive': true,
'editable': true,
'cursor': null,
'antiMeridian': false,
'defaultProjection': 'EPSG:4326' // BAIDU, IDENTITY
};
/**
* 所有几何图形的基类。
* 它定义了所有几何图形类共享的通用方法。
* 它是抽象的,不打算被实例化而是被扩展。
* @english
* Base class for all the geometries. <br/>
* It defines common methods that all the geometry classes share. <br>
* It is abstract and not intended to be instantiated but extended.
*
* @category geometry
* @abstract
* @extends Class
* @mixes Eventable
* @mixes Handlerable
* @mixes JSONAble
* @mixes ui.Menuable
*/
export class Geometry extends JSONAble(Eventable(Handlerable(Class))) {
options: GeometryOptionsType;
type: string;
//@internal
_layer: OverlayLayer;
//@internal
_angle: number
//@internal
_pivot: Coordinate
//@internal
_id: string
properties: Record<string, any>;
//@internal
_symbol: any
//@internal
_symbolUpdated: any
//@internal
_compiledSymbol: any
//@internal
_symbolHash: any
//@internal
_textDesc: any
//@internal
_eventSymbolProperties: any
//@internal
_sizeSymbol: any
//@internal
_internalId: number
//@internal
_extent: Extent
//@internal
_fixedExtent: PointExtent
//@internal
_extent2d: PointExtent
//@internal
_externSymbol: any
//@internal
_parent: Geometry | GeometryCollection
//@internal
_silence: boolean
//@internal
_projCode: string
//@internal
_painter: Painter
//@internal
_maskPainter: CollectionPainter | Painter
//@internal
_dirtyCoords: boolean;
//@internal
_pcenter: Coordinate
//@internal
_coordinates: any;
//@internal
_infoWinOptions: InfoWindowOptionsType;
//@internal
_minAlt: number
//@internal
_maxAlt: number;
// 在 VectorLayerCanvasRenderer 附加的信息
//@internal
_isCheck?: boolean;
//@internal
_cPoint?: any;
//@internal
_inCurrentView?: boolean;
// 在 Marker 中附加的信息,Marker 和其子类都具有此属性
isPoint?: boolean;
//@internal
_savedVisible?: boolean;
//
//@internal
_paintAsPath?: () => any;
//@internal
_getPaintParams?: (disableSimplify?: boolean) => any[];
//@internal
_simplified?: boolean;
//@internal
_dirtyRotate?: boolean;
// 本身应该存于 Path 类,但是由于渲染层需要大量的特殊熟悉判断,定义在这里回减少很多麻烦
getHoles?(): Array<Array<Coordinate>>;
//@internal
__connectors: Array<Geometry>;
getShell?(): Array<Coordinate>;
getGeometries?(): Geometry[];
getCoordinates?(): Coordinate | Array<Coordinate> | Array<Array<Coordinate>> | Array<Array<Array<Coordinate>>>
setCoordinates?(coordinate: any): this;
//@internal
_computeCenter?(T: any): Coordinate;
//@internal
_computeExtent?(T: any): Extent;
onRemove?(): void;
//@internal
_computeGeodesicLength?(T: any): number;
//@internal
_computeGeodesicArea?(T: any): number;
getRotateOffsetAngle?(): number;
//@internal
_computePrjExtent?(T: null | ProjectionType): Extent;
//@internal
_updateCache?(): void;
onAdd?(): void;
constructor(options: GeometryOptionsType) {
const opts = extend({}, options);
const symbol = opts['symbol'];
const properties = opts['properties'];
const id = opts['id'];
delete opts['symbol'];
delete opts['id'];
delete opts['properties'];
super(opts);
if (symbol) {
this.setSymbol(symbol);
} else {
this._genSizeSymbol();
}
if (properties) {
this.setProperties(properties);
}
if (!isNil(id)) {
this.setId(id);
}
//record rotate
if (options && isNumber(options.rotateAngle)) {
this._dirtyRotate = true;
}
}
static fromJSON(json: { [key: string]: any } | Array<{ [key: string]: any }>): Geometry | Array<Geometry> {
return json as Geometry;
}
/**
* 获取几何图形第一个坐标点
* @english
* Returns the first coordinate of the geometry.
*
* @return {Coordinate} First Coordinate
*/
getFirstCoordinate(): Coordinate {
if (this.type === 'GeometryCollection') {
const geometries = this.getGeometries();
if (!geometries.length) {
return null;
}
return geometries[0].getFirstCoordinate();
}
let coordinates: any = this.getCoordinates();
if (!Array.isArray(coordinates)) {
return coordinates;
}
do {
coordinates = coordinates[0];
} while (Array.isArray(coordinates) && coordinates.length > 0);
return coordinates;
}
/**
* 获取几何图形最后一个坐标点
* @english
* Returns the last coordinate of the geometry.
*
* @return {Coordinate} Last Coordinate
*/
getLastCoordinate(): Coordinate {
if (this.type === 'GeometryCollection') {
const geometries = this.getGeometries();
if (!geometries.length) {
return null;
}
return geometries[geometries.length - 1].getLastCoordinate();
}
let coordinates: any = this.getCoordinates();
if (!Array.isArray(coordinates)) {
return coordinates;
}
do {
coordinates = coordinates[coordinates.length - 1];
} while (Array.isArray(coordinates) && coordinates.length > 0);
return coordinates;
}
/**
* 将几何图形添加到指定图层上
* @english
* Adds the geometry to a layer
* @param {Layer} layer - layer add to
* @param {Boolean} [fitview=false] - automatically set the map to a fit center and zoom for the geometry
* @return {Geometry} this
* @fires Geometry#add
*/
addTo(layer: OverlayLayer, fitview?: boolean | addGeometryFitViewOptions): this {
layer.addGeometry(this, fitview);
return this;
}
/**
* 获取几何图形所在的图层
* @english
* Get the layer which this geometry added to.
* @returns {Layer} - layer added to
*/
getLayer(): OverlayLayer {
if (!this._layer) {
return null;
}
return this._layer;
}
/**
* 获取几何图形所在的地图对象
* @english
* Get the map which this geometry added to
* @returns {Map} - map added to
*/
getMap(): Map | null {
if (!this._layer) {
return null;
}
return this._layer.getMap();
}
/**
* 获取几何图形的id
* @english
* Gets geometry's id. Id is set by setId or constructor options.
* @returns {String|Number} geometry的id
*/
getId(): string {
return this._id;
}
/**
* 给几何图形设置id
* @english
* Set geometry's id.
* @param {String} id - new id
* @returns {Geometry} this
* @fires Geometry#idchange
*/
setId(id: string): this {
const oldId = this.getId();
this._id = id;
/**
* idchange event.
*
* @event Geometry#idchange
* @type {Object}
* @property {String} type - idchange
* @property {Geometry} target - the geometry fires the event
* @property {String|Number} old - value of the old id
* @property {String|Number} new - value of the new id
*/
this._fireEvent('idchange', {
'old': oldId,
'new': id
});
return this;
}
/**
* 获取几何图形的属性
* @english
* Get geometry's properties. Defined by GeoJSON as [feature's properties]{@link http://geojson.org/geojson-spec.html#feature-objects}.
*
* @returns {Object} properties
*/
getProperties(): { [key: string]: any } | null {
if (!this.properties) {
if (this._getParent()) {
return this._getParent().getProperties();
}
return null;
}
return this.properties;
}
/**
* 给几何图形设置新的属性
* Set a new properties to geometry.
* @param {Object} properties - new properties
* @returns {Geometry} this
* @fires Geometry#propertieschange
*/
setProperties(properties: { [key: string]: any }): this {
const old = this.properties;
this.properties = isObject(properties) ? extend({}, properties) : properties;
//such as altitude update
this._clearAltitudeCache();
this._repaint();
/**
* propertieschange event, thrown when geometry's properties is changed.
*
* @event Geometry#propertieschange
* @type {Object}
* @property {String} type - propertieschange
* @property {Geometry} target - the geometry fires the event
* @property {String|Number} old - value of the old properties
* @property {String|Number} new - value of the new properties
*/
this._fireEvent('propertieschange', {
'old': old,
'new': properties
});
return this;
}
/**
* 获取几何图形的类型,例如“点”,"线"
* @english
* Get type of the geometry, e.g. "Point", "LineString"
* @returns {String} type of the geometry
*/
getType(): string {
return this.type;
}
/**
* 获取几何图形的样式
* @english
* Get symbol of the geometry
* @returns {Object} geometry's symbol
*/
getSymbol(): any {
const s = this._symbol;
if (s) {
if (!Array.isArray(s)) {
return extend({}, s);
} else {
return extendSymbol(s);
}
}
return null;
}
/**
* 给几何图形设置样式
* @english
* Set a new symbol to style the geometry.
* @param {Object} symbol - new symbol
* @see {@tutorial symbol Style a geometry with symbols}
* @return {Geometry} this
* @fires Geometry#symbolchange
*/
setSymbol(symbol: any): this {
this._symbolUpdated = symbol;
this._symbol = this._prepareSymbol(symbol);
this.onSymbolChanged();
delete this._compiledSymbol;
delete this._symbolHash;
return this;
}
/**
* 获取样式的哈希值
* @english
* Get symbol's hash code
* @return {String}
*/
getSymbolHash(): string {
if (!this._symbolHash) {
this._symbolHash = getSymbolHash(this._symbolUpdated);
}
return this._symbolHash;
}
/**
* 更新几何图形当前的样式
* @english
* Update geometry's current symbol.
*
* @param {Object | Array} props - symbol properties to update
* @return {Geometry} this
* @fires Geometry#symbolchange
* @example
* var marker = new Marker([0, 0], {
* // if has markerFile , the priority of the picture is greater than the vector and the path of svg
* // svg image type:'path';vector type:'cross','x','diamond','bar','square','rectangle','triangle','ellipse','pin','pie'
* symbol : {
* markerType : 'ellipse',
* markerWidth : 20,
* markerHeight : 30
* }
* });
* // update symbol's markerWidth to 40
* marker.updateSymbol({
* markerWidth : 40
* });
*/
updateSymbol(props: any): this {
if (!props) {
return this;
}
let s = this._getSymbol();
if (Array.isArray(s)) {
if (!Array.isArray(props)) {
throw new Error('Parameter of updateSymbol is not an array.');
}
for (let i = 0; i < props.length; i++) {
if (isTextSymbol(props[i])) {
delete this._textDesc;
}
if (s[i] && props[i]) {
s[i] = extendSymbol(s[i], props[i]);
}
}
} else if (Array.isArray(props)) {
throw new Error('Geometry\'s symbol is not an array to update.');
} else {
if (isTextSymbol(s)) {
delete this._textDesc;
}
if (s) {
s = extendSymbol(s, props);
} else {
s = extendSymbol(this._getInternalSymbol(), props);
}
}
this._eventSymbolProperties = props;
delete this._compiledSymbol;
return this.setSymbol(s);
}
/**
* 如果几何图形有文本内容,就获取它
* @english
* Get geometry's text content if it has
* @returns {String}
*/
getTextContent(): any {
const symbol = this._getInternalSymbol();
if (Array.isArray(symbol)) {
const contents = [];
let has = false;
for (let i = 0; i < symbol.length; i++) {
contents[i] = replaceVariable(symbol[i] && symbol[i]['textName'], this.getProperties());
if (!isNil(contents[i])) {
has = true;
}
}
return has ? contents : null;
}
return replaceVariable(symbol && symbol['textName'], this.getProperties());
}
getTextDesc(): any {
if (!this._textDesc) {
const textContent = this.getTextContent();
// if textName='',this is error
// if (!textContent) {
// return null;
// }
const symbol = this._sizeSymbol;
const isArray = Array.isArray(textContent);
if (Array.isArray(symbol)) {
this._textDesc = symbol.map((s, i) => {
return describeText(isArray ? textContent[i] : '', s);
});
} else {
this._textDesc = describeText(textContent, symbol);
}
}
return this._textDesc;
}
/**
* 获取几何图形中心点
* @english
* Get the geographical center of the geometry.
*
* @returns {Coordinate}
*/
getCenter(): Coordinate {
return this._computeCenter(this._getMeasurer());
}
/**
* 获取几何图形的包围盒范围
* @english
* Get the geometry's geographical extent
*
* @returns {Extent} geometry's extent
*/
getExtent(): Extent {
const prjExt = this._getPrjExtent();
const projection = this._getProjection();
if (prjExt && projection) {
const min = projection.unproject(new Coordinate(prjExt['xmin'], prjExt['ymin'])),
max = projection.unproject(new Coordinate(prjExt['xmax'], prjExt['ymax']));
return new Extent(min, max, projection);
} else {
return this._computeExtent(this._getMeasurer());
}
}
/**
* 获取几何图形的屏幕像素范围
* @english
* Get geometry's screen extent in pixel
*
* @returns {PointExtent}
*/
getContainerExtent(out?: PointExtent): PointExtent {
const extent2d = this.get2DExtent();
if (!extent2d || !extent2d.isValid()) {
return null;
}
const map = this.getMap();
// const center = this.getCenter();
const glRes = map.getGLRes();
const minAltitude = this.getMinAltitude();
const extent = extent2d.convertTo(c => map._pointAtResToContainerPoint(c, glRes, minAltitude, TEMP_POINT0), out);
const maxAltitude = this.getMaxAltitude();
if (maxAltitude !== minAltitude) {
const extent2 = extent2d.convertTo(c => map._pointAtResToContainerPoint(c, glRes, maxAltitude, TEMP_POINT0), TEMP_EXTENT);
extent._combine(extent2);
}
const layer = this.getLayer();
if (layer && this.type === 'LineString' && maxAltitude && layer.options['drawAltitude']) {
const groundExtent = extent2d.convertTo(c => map._pointAtResToContainerPoint(c, glRes, 0, TEMP_POINT0), TEMP_EXTENT);
extent._combine(groundExtent);
}
if (extent) {
const fixedExtent = this._getFixedExtent();
if (validateExtent(fixedExtent)) {
extent._add(fixedExtent);
}
}
const smoothness = this.options['smoothness'];
if (smoothness) {
extent._expand(extent.getWidth() * 0.15);
}
return extent;
}
//@internal
_getFixedExtent(): PointExtent {
// only for LineString and Polygon, Marker's will be overrided
if (!this._fixedExtent) {
this._fixedExtent = new PointExtent();
}
const symbol = this._sizeSymbol;
const t = (symbol && symbol['lineWidth'] || 1) / 2;
this._fixedExtent.set(-t, -t, t, t);
const dx = (symbol && symbol['lineDx']) || 0;
this._fixedExtent._add([dx, 0]);
const dy = (symbol && symbol['lineDy']) || 0;
this._fixedExtent._add([0, dy]);
return this._fixedExtent;
}
get2DExtent(): PointExtent {
const map = this.getMap();
if (!map) {
return null;
}
if (this._extent2d) {
return this._extent2d;
}
const extent = this._getPrjExtent();
if (!extent || !extent.isValid()) {
return null;
}
const min = extent.getMin();
const max = extent.getMax();
const glRes = map.getGLRes();
map._prjToPointAtRes(min, glRes, min);
map._prjToPointAtRes(max, glRes, max);
this._extent2d = new PointExtent(min, max);
(this._extent2d as any).z = map.getZoom();
return this._extent2d;
}
/**
* 获取几何体的像素大小,不同缩放级别的像素大小可能会有所不同。
* @english
* Get pixel size of the geometry, which may vary in different zoom levels.
*
* @returns {Size}
*/
getSize(): SizeLike {
const extent = this.getContainerExtent();
return extent ? extent.getSize() : null;
}
/**
* 几何体是否包含输入容器点
* @english
* Whehter the geometry contains the input container point.
*
* @param {Point|Coordinate} point - input container point or coordinate
* @param {Number} [t=undefined] - tolerance in pixel
* @return {Boolean}
* @example
* var circle = new Circle([0, 0], 1000)
* .addTo(layer);
* var contains = circle.containsPoint(new maptalks.Point(400, 300));
*/
containsPoint(containerPoint: Point, t?: number): boolean {
if (!this.getMap()) {
throw new Error('The geometry is required to be added on a map to perform "containsPoint".');
}
if (containerPoint instanceof Coordinate) {
containerPoint = this.getMap().coordToContainerPoint(containerPoint);
}
return this._containsPoint(containerPoint, t);
// return this._containsPoint(this.getMap()._containerPointToPoint(new Point(containerPoint)), t);
}
//@internal
_containsPoint(containerPoint: Point, t?: number): boolean {
const painter = this._getPainter();
if (!painter) {
return false;
}
t = t || 0;
if (this._hitTestTolerance) {
t += this._hitTestTolerance();
}
return painter.hitTest(containerPoint, t);
}
/**
* 显示几何图形
* @english
* Show the geometry.
*
* @return {Geometry} this
* @fires Geometry#show
*/
show(): this {
this.options['visible'] = true;
if (this.getMap()) {
const painter = this._getPainter();
if (painter) {
painter.show();
}
/**
* show event
*
* @event Geometry#show
* @type {Object}
* @property {String} type - show
* @property {Geometry} target - the geometry fires the event
*/
this._fireEvent('show');
}
return this;
}
/**
* 隐藏几何图形
* @english
* Hide the geometry
*
* @return {Geometry} this
* @fires Geometry#hide
*/
hide(): this {
this.options['visible'] = false;
if (this.getMap()) {
this.onHide();
const painter = this._getPainter();
if (painter) {
painter.hide();
}
/**
* hide event
*
* @event Geometry#hide
* @type {Object}
* @property {String} type - hide
* @property {Geometry} target - the geometry fires the event
*/
this._fireEvent('hide');
}
return this;
}
/**
* 几何图形是否可见
* @english
* Whether the geometry is visible
*
* @returns {Boolean}
*/
isVisible(): boolean {
if (!this.options['visible']) {
return false;
}
const symbol = this._getInternalSymbol();
if (!symbol) {
return true;
}
if (!this.symbolIsVisible()) {
return false;
}
if (Array.isArray(symbol)) {
if (!symbol.length) {
return true;
}
for (let i = 0, l = symbol.length; i < l; i++) {
if (isNil(symbol[i]['opacity']) || symbol[i]['opacity'] > 0) {
return true;
}
}
return false;
} else {
return (isNil(symbol['opacity']) || isObject(symbol['opacity']) || (isNumber(symbol['opacity']) && symbol['opacity'] > 0));
}
}
/**
* symbol是否可见
* @english
* Whether the geometry symbol is visible
*
* @returns {Boolean}
*/
symbolIsVisible(): boolean {
//function-type
let symbols = this._getCompiledSymbol();
if (!symbols) {
return true;
}
if (!Array.isArray(symbols)) {
symbols = [symbols];
}
for (let i = 0, len = symbols.length; i < len; i++) {
const symbol = symbols[i];
if (!symbol) {
continue;
}
const isVisible = symbol.visible;
if (isVisible !== false && isVisible !== 0) {
return true;
}
}
return false;
}
/**
* 获取几何图形所在层级,默认是0
* @english
* Get zIndex of the geometry, default is 0
* @return {Number} zIndex
*/
getZIndex(): number {
return this.options['zIndex'] || 0;
}
/**
* 给几何图形设置新的层级并触发zindexchange事件(将导致层对几何体进行排序并进行渲染)
* @english
* Set a new zIndex to Geometry and fire zindexchange event (will cause layer to sort geometries and render)
* @param {Number} zIndex - new zIndex
* @return {Geometry} this
* @fires Geometry#zindexchange
*/
setZIndex(zIndex: number): this {
const old = this.options['zIndex'];
this.options['zIndex'] = zIndex;
/**
* 层级改变事件,当几何图形层级发生改变将会触发
* @english
* zindexchange event, fired when geometry's zIndex is changed.
*
* @event Geometry#zindexchange
* @type {Object}
* @property {String} type - zindexchange
* @property {Geometry} target - the geometry fires the event
* @property {Number} old - old zIndex
* @property {Number} new - new zIndex
*/
this._fireEvent('zindexchange', {
'old': old,
'new': zIndex
});
return this;
}
/**
* 仅将新的zIndex设置为Geometry,而不触发zindexchange事件
* 当需要更新许多几何图形的zIndex时,可以用来提高性能
* 当更新了N个几何体时,可以将setZIndexSilently与(N-1)个几何体一起使用,并将setZIendex与要排序和渲染的层的最后一个几何体一同使用。
* @english
* Only set a new zIndex to Geometry without firing zindexchange event. <br>
* Can be useful to improve perf when a lot of geometries' zIndex need to be updated. <br>
* When updated N geometries, You can use setZIndexSilently with (N-1) geometries and use setZIndex with the last geometry for layer to sort and render.
* @param {Number} zIndex - new zIndex
* @return {Geometry} this
*/
setZIndexSilently(zIndex: number): this {
this.options['zIndex'] = zIndex;
return this;
}
/**
* 将几何图形至于顶层
* @english
* Bring the geometry on the top
* @return {Geometry} this
* @fires Geometry#zindexchange
*/
bringToFront(): this {
const layer = this.getLayer();
if (!layer || !layer.getGeoMaxZIndex) {
return this;
}
const topZ = layer.getGeoMaxZIndex();
this.setZIndex(topZ + 1);
return this;
}
/**
* 将几何图形置于底层
* @english
* Bring the geometry to the back
* @return {Geometry} this
* @fires Geometry#zindexchange
*/
bringToBack(): this {
const layer = this.getLayer();
if (!layer || !layer.getGeoMinZIndex) {
return this;
}
const bottomZ = layer.getGeoMinZIndex();
this.setZIndex(bottomZ - 1);
return this;
}
/**
* 按给定偏移平移或移动几何体
* @english
* Translate or move the geometry by the given offset.
*
* @param {Coordinate} offset - translate offset
* @return {Geometry} this
* @fires Geometry#positionchange
* @fires Geometry#shapechange
*/
/**
* Translate or move the geometry by the given offset.
*
* @param {Number} x - x offset
* @param {Number} y - y offset
* @return {Geometry} this
* @fires Geometry#positionchange
* @fires Geometry#shapechange
*/
translate(x: number | Coordinate, y?: number): this {
if (isNil(x)) {
return this;
}
const offset = new Coordinate(x as number, y);
if (offset.x === 0 && offset.y === 0) {
return this;
}
const coordinates: any = this.getCoordinates();
this._silence = true;
if (coordinates) {
if (Array.isArray(coordinates)) {
const translated = forEachCoord(coordinates, function (coord) {
return coord.add(offset);
});
this.setCoordinates(translated);
} else {
this.setCoordinates(coordinates.add(offset));
}
}
this._silence = false;
this._fireEvent('positionchange');
return this;
}
//translate rotate Pivot when coordinates change
//@interlal
_translateRotatePivot(newCoordinate: Coordinate) {
if (!this._pivot || !newCoordinate) {
return this;
}
if (this.options.rotatePivot) {
const oldCoordinate = this.getCoordinates();
if (!oldCoordinate) {
return this;
}
if (!(newCoordinate instanceof Coordinate)) {
newCoordinate = new Coordinate(newCoordinate);
}
const offset = newCoordinate.sub(oldCoordinate as Coordinate);
if (offset.x === 0 && offset.y === 0) {
return this;
}
this._pivot._add(offset);
this.options.rotatePivot = this._pivot.toArray();
}
return this;
}
/**
* 闪烁几何图形,按一定的内部显示和隐藏计数次数。
* @english