-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathh3_hybrid.js
2957 lines (2811 loc) · 124 KB
/
h3_hybrid.js
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
//名称:Hexagon空间场js基础库
//版权:武汉大学 资源与环境科学学院 506室
//时间:2017/04/01
//命名规则:1 变量命名遵循camel命名法,如firstFunction 2 全局私有变量命名通过'_'开始,如_firstMember
//作者:倒霉蛋儿
(function (global, lib) {
'use strict';
let _version = '2.17.0602';
//选择器
//@param [string] 如'#ID'||'.className'||'tagName'
let _select = function (selector) {
let t = selector[0],
name = selector.slice(1, selector.length);
switch (t) {
case '#':
return document.getElementById(name);
break;
case '.':
var nodeList = document.getElementsByClassName(name),
eleAry = [];
for (var i = 0; i < nodeList.length; i++) {
eleAry.push(nodeList[i]);
}
return eleAry;
break;
default:
return document.getElementsByTagName(selector);
}
}
//------------------------------ 0 基础类 ------------------------------
let _pointPixel = function (x, y) { return { x: x, y: y, toString: function () { return '(' + this.x + ',' + this.y + ')' } }; }
let _pointCoord = function (x, z) { return { x: x, z: z, y: -x - z, toString: function () { return '(' + this.x + ',' + this.y + ',' + this.z + ')'; } } }
let _linePixel = function (pointPixelFrom, pointPixelTo) { return { from: pointPixelFrom, to: pointPixelTo, center: _pointPixel((pointPixelFrom.x + pointPixelTo.x) / 2, (pointPixelFrom.y + pointPixelTo.y) / 2) }; }
//范围
//@param [number] 最小x值
//@param [number] 最大x值
//@param [number] 最小y值
//@param [number] 最大y值
let _extent = function (minX, maxX, minY, maxY) {
if (arguments.length == 0) {
this.minX = this.minY = Number.MAX_VALUE;
this.maxX = this.maxY = -Number.MAX_VALUE;
return;
}
this.minX = minX; this.maxX = maxX;
this.minY = minY; this.maxY = maxY;
this.width = maxX - minX;
this.height = maxY - minY;
this.center = { x: this.minX + this.width / 2, y: this.minY + this.height / 2 };
}
_extent.prototype = {
//范围比较并更新
change: function (extent) {
this.minX = this.minX < extent.minX ? this.minX : extent.minX;
this.maxX = this.maxX > extent.maxX ? this.maxX : extent.maxX;
this.minY = this.minY < extent.minY ? this.minY : extent.minY;
this.maxY = this.maxY > extent.maxY ? this.maxY : extent.maxY;
this.width = this.maxX - this.minX;
this.height = this.maxY - this.minY;
this.center = { x: this.minX + this.width / 2, y: this.minY + this.height / 2 };
}
}
let window2canvas = function (canvas, x, y) {
let bbox = canvas.getBoundingClientRect();
return {
x: x - bbox.left * (canvas.width / bbox.width),
y: y - bbox.top * (canvas.height / bbox.height)
};
}
//变换矩阵
let _martix = function () {
this.matrix = [1, 0, 0, 1, 0, 0]; // normal matrix
this.inverseMatrix = [1, 0, 0, 1]; // inverse matrix
this.offset = { x: 0, y: 0 };
this.scale = 1;
this.rotate = 0;
}
_martix.prototype = {
createMatrix: function (x, y, scale, rotate) {
//this.offset.x += x;
//this.offset.y += y;
this.offset.x += (x + this.offset.x * (scale - 1)); // modified by yan.
this.offset.y += (y + this.offset.y * (scale - 1)); // modified by yan.
this.scale *= scale;
this.rotate += rotate;
let m = this.matrix; // just to make it easier to type and read
let im = this.inverseMatrix; // just to make it easier to type and read
// create the scale and rotation part of the matrix
m[3] = m[0] = Math.cos(this.rotate) * this.scale;
m[2] = -(m[1] = Math.sin(this.rotate) * this.scale);
// translation
m[4] = this.offset.x;
m[5] = this.offset.y;
// calculate the inverse transformation
// first get the cross product of x axis and y axis
let cross = m[0] * m[3] - m[1] * m[2];
// now get the inverted axies
im[0] = m[3] / cross;
im[1] = -m[1] / cross;
im[2] = -m[2] / cross;
im[3] = m[0] / cross;
this.matrix = m;
this.inverseMatrix = im;
},
// function to transform to world space
toWorld: function (x, y) {
let xx, yy, m;
m = this.inverseMatrix;
xx = x - this.matrix[4];
yy = y - this.matrix[5];
return {
x: xx * m[0] + yy * m[2],
y: xx * m[1] + yy * m[3]
}
}
}
let _colorRGBA = function (r, g, b, a) { return 'rgba(' + (r || 0) + ',' + (g || 0) + ',' + (b || 0) + ',' + (a || 1) + ')'; }
let _colors = { white: 'white', red: 'red', green: 'green', blue: 'blue', gray: 'gray', lightgray: 'lightgray', yellow: 'yellow', lightgreen: 'lightgreen', lightblue: 'ligntblue', black: 'black', purple: 'purple', dodgerblue: 'dodgerblue', forestgreen: 'forestgreen' };
let _sqrt3 = Math.sqrt(3);
//异步读取数据
//parma:method(GET或POST) url(文件路径或者.aspx文件路径/方法名) dataType:'txt'||'json' param(json{par1:val1,par2,val2...})
let _ajax = function (parma, fnSuccess, fnFaild) {
//1、创建ajax对象
let oAjax = null;
if (window.XMLHttpRequest) {
oAjax = new XMLHttpRequest();
}
else {
oAjax = new ActiveXObject('Miscrosoft.XMLHttpRequest');
}
//2、建立连接
oAjax.open(parma.method, parma.url, true);
//if (parma.contentType) {
// oAjax.setRequestHeader('Content-Type', parma.contentType);
//}
//3、发送请求
oAjax.send(parma.param);
//4.接收数据
oAjax.onreadystatechange = function () {
if (oAjax.readyState == 4) {
if (oAjax.status == 200) {//完成
if (parma.dataType.toLowerCase() == 'json')
fnSuccess(JSON.parse(oAjax.responseText));
else
fnSuccess(oAjax.responseText);
}
else {//出错
if (fnFaild)
fnFaild(oAjax.status);
}
}
};
}
//地图
//@param [string] 容器div标签的id
//@param [number] 坐标系单位,unit(实际分辨率)
let _map = function (idName, coordType, unit) {
let parent = document.getElementById(idName);
//parent.style.position = 'relative';
let canvas = document.createElement('canvas');
canvas.width = parent.clientWidth;
canvas.height = parent.clientHeight;
//canvas.style.border = '1px solid lightgray'
parent.appendChild(canvas);
window.onresize = (e) => {
parent = document.getElementById(idName);
canvas.width = parent.clientWidth;
canvas.height = parent.clientHeight;
this.render(true);
}
let labelContainer = document.createElement('div');
labelContainer.style.background = _colorRGBA(250, 250, 250, 0.8);
labelContainer.style.padding = '5px';
labelContainer.style.margin = '5px';
labelContainer.style.borderRadius = '5px';
labelContainer.style.position = 'absolute';
labelContainer.style.display = 'none';
labelContainer.style.top = labelContainer.style.right = '0px';
let labelState = document.createElement('label');
labelContainer.appendChild(labelState);
parent.appendChild(labelContainer);
//let btnSize = 20;
//let btnZoomIn = document.createElement('div');
//btnZoomIn.style.backgroundColor = _colorRGBA(100, 100, 100, 0.5);
//btnZoomIn.style.position = 'absolute';
//btnZoomIn.style.top = btnZoomIn.style.left = '0px';
//btnZoomIn.style.padding = '5px';
//btnZoomIn.style.margin = '5px';
//btnZoomIn.style.borderRadius = '5px';
//btnZoomIn.style.color = 'black';
//btnZoomIn.style.textAlign = 'center';
//btnZoomIn.style.lineHeight = btnSize;
//btnZoomIn.style.width = btnZoomIn.style.height = btnSize + 'px';
//parent.appendChild(btnZoomIn);
this.unit = unit;
//画布
this.canvas = canvas;
//绘图上下文
let context = this.canvas.getContext('2d');
context.lineWidth = 0.05;
//context.font = (unit / 20).toFixed(2) + 'px palatino';
this.labelState = labelState;
this.showLabelState = false;
//画笔
this.graphic = new _graphic(context);
//坐标系
this.coordSystem = _coordSystem(coordType, unit);
//范围
this.extent = new _extent(Number.MAX_VALUE, -Number.MAX_VALUE, Number.MAX_VALUE, -Number.MAX_VALUE);
//this.geometryArray = [];
this.layerArray = [];
this.layerCount = 0;
this.showCoord = false;
this.firstzRender = true;
this.updateType = { add: 2, remove: 4, sort: 6, visable: 8 }
//鼠标事件
//是否允许缩放平移变换
this.enableTransform = true;
//鼠标移动事件
this.onmousemove = undefined;
//鼠标点击事件
this.onmousedown = undefined;
//鼠标滚轮事件
this.onmousewhell = undefined;
//其他事件
//渲染之后执行事件
this.afterRender = undefined;
////伙伴map 同步操作
this.partnersAry = [];
//将map看做一张栅格图片 六边形为栅格像元
//记录map最上层的六边形 即呈现的栅格图片的像元
//优化渲染速度
//但是 这会根据图层的增删 层次的变化而改变
this.image = new Map();//key:pointCoord,value:geoPoint
this.imageData = new Map();//key:pointCoord,value:2d array
this.renderTimerId = 0;
//初始化
this.inite();
}
_map.prototype = {
//添加图层
addLayer: function (layer, name) {
if (!layer) return;
let self = this;
layer.name = name || layer.name;
self.extent.change(layer.extent);
self.layerArray.push(layer);
self.layerCount++;
this.update(this.updateType.add, layer);
},
//sortLayer: function () {
// this.layerArray.sort((ly1, ly2) => { return ly1.name - ly2.name; });
// this.async('sortLayer');
//},
//获取图层
//@param [fnFailed] 图层名字
getLayer: function (name) {
return this.layerArray.filter((ly) => { return ly.name == name; })
},
removeLayer(name) {
var layer = this.getLayer(name);
this.update(this.updateType.remove, layer);
var index = this.layerArray.indexOf(layer);
this.layerArray.splice(index, 1);
this.async('removeLayer', name);
},
//清空画布.重新绘制
render: function (fullExtent) {
let self = this;
//清空画布
self.clear();
if (self.firstzRender || fullExtent) {
self.fullExtent();
self.firstzRender = false;
}
//self.sortLayer();
self.layerArray.forEach(function (ly) {
if (ly.visable) {
//ly.geometryArray.forEach(geometry=> {
// if (geometry) {
// //针对线的补全和面的填充 不包括对点的操作
// switch (geometry.type) {
// case _geoType.geoPoint:
// //self.graphic.fillGeoPoint(geometry);
// self.graphic.fillGeoPoint(geometry);
// break;
// case _geoType.geoPolyline:
// //self.graphic.fillGeoPolyine(geometry);
// self.graphic.fillGeoPolyine(geometry);
// break;
// case _geoType.geoPolygon:
// //self.graphic.fillGeoPolygon(geometry);
// self.graphic.fillGeoPolygon(geometry);
// break;
// }
// }
// else {
// console.log(ly.name);
// console.log(geometry);
// }
//})
ly.image.forEach(geoPt=> {
self.graphic.fillGeoPoint(geoPt);
})
}
})
if (self.afterRender)
self.afterRender();
this.async('render');
},
//初始化Map状态
inite: function () {
let self = this;
self.extent = new _extent();
self.layerArray = [];
self.layerCount = 0;
self.firstzRender = true;
self.image = new Map();
self.imageData = new Map();
if (!self.canvas.onmousewheel) {
self.canvas.onmousewheel = function (e) {
if (!self.enableTransform)
return;
e.preventDefault();
//获取canvas pixelArray
var canvasImage = self.graphic.context.getImageData(0, 0, self.canvas.width, self.canvas.height);
let scale = e.wheelDelta < 0 ? 0.9 : 1.1;
e = window2canvas(self.canvas, e.x, e.y);
self.transform(-e.x * (scale - 1), -e.y * (scale - 1), scale, 0);
self.clear();
self.graphic.context.putImageData(canvasImage, 0, 0);
if (self.onmousewhell)
self.onmousewhell();
clearTimeout(self.renderTimerId);
self.renderTimerId = setTimeout(() => {
self.render();
}, 100);
}
}
if (!self.canvas.onmousemove) {
self.canvas.onmousemove = function (e) {
//self.render();
e = window2canvas(self.canvas, e.x, e.y);
let wp = self.graphic.canvas2GeoXY(self.canvas, self.graphic.matrix.toWorld(e.x, e.y)),//倒霉蛋儿 2017/10/16
hotCoord = self.coordSystem.getHotCoord(_pointPixel(wp.x, wp.y));
if (self.showLabelState) {
self.labelState.parentElement.style.display = '';
let tempPoint = self.getGeoPoint(hotCoord);
self.labelState.innerText = "canvas position:x:" + e.x.toFixed(2) + ',y:' + e.y.toFixed(2) +
"\nworld position:x:" + wp.x.toFixed(2) + ',y:' + wp.y.toFixed(2);
if (hotCoord.z)
self.labelState.innerText += '\ncoordSystem:x:' + hotCoord.x + ',z:' + hotCoord.z;
else
self.labelState.innerText += '\ncoordSystem:x:' + hotCoord.x + ',y:' + hotCoord.y;
self.labelState.innerText += '\ngeoXY:X:' + tempPoint.x.toFixed(2) + ',Y:' + tempPoint.y.toFixed(2) + '\n\n';
}
else {
self.labelState.innerText = '';
self.labelState.parentElement.style.display = 'none';
}
if (self.onmousemove)
self.onmousemove(hotCoord);
}
}
if (!self.canvas.onmousedown) {
self.canvas.onmousedown = function (e) {
let cp = window2canvas(self.canvas, e.x, e.y);
let wp = self.graphic.canvas2GeoXY(self.canvas, self.graphic.matrix.toWorld(cp.x, cp.y)),//倒霉蛋儿 2017/10/16
hotCoord = self.coordSystem.getHotCoord(_pointPixel(wp.x, wp.y));
if (self.enableTransform && e.button == 1) {
let offsetX = 0, offsetY = 0;
window.onmousemove = function (ee) {
ee.preventDefault();
offsetX = ee.clientX - e.clientX;
offsetY = ee.clientY - e.clientY;
}
window.onmouseup = function () {
if (offsetX != 0 || offsetY != 0) {
self.transform(offsetX, offsetY, 1, 0, true);
}
//注销 鼠标事件
window.onmousemove = null;
window.onmouseup = null;
}
}
if (self.onmousedown)
self.onmousedown(hotCoord, e);
}
}
},
//设置单位长度
setUnit: function (unit) {
unit = parseFloat(unit.toString());
this.coordSystem.unit = unit;
this.graphic.context.font = parseInt(unit / 15) + 'px palatino';
this.layerArray.forEach((ly) => {
ly.reInite(this.coordSystem);
})
this.async('setUnit', unit);
},
//图形变换
transform: function (tx, ty, scale, rotate, reRender) {
this.graphic.transform(tx, ty, scale, rotate);
if (reRender)
this.render();
this.async('transform', tx, ty, scale, rotate);
},
//清空所有绘制图形
//@param [bool] 是否删除之前加载的数据
clear: function (clearData) {
this.graphic.clear();
if (clearData)
this.inite();
this.async('clear', clearData);
},
fullExtent: function () {
this.graphic.fullExtent(this.extent);//倒霉蛋儿 2017/10/16
this.async('fullExtent');
},
getGeoPoint: function (pointCoord) {
let self = this;
let pointPixel = self.coordSystem.getCenterPixel(pointCoord);
return new _geoPoint(pointPixel.x, pointPixel.y, self.coordSystem);
},
//加载ESRI格式的json数据(通过ArcToolBox转Json工具生成的数据)
//@param [string] 文件路径
//@param [function] 数据加载成功待执行的方法;方法接收加载的layer和data作参数
loadEsriJSON: function (url, fnSucceed, fnFailed) {
let self = this;
h3.ajax({ method: 'GET', url: url, dataType: 'json' },//1 加载json数据
function (d) {
let ly = new _layer('layer1'),
index = 0;
let data = d;//2 加载成功后解析数据
data.features.forEach(function (feature) {
let geometry;//3 创建地理要素线
switch (data.geometryType) {
case 'esriGeometryPoint':
geometry = new _geoPoint(feature.geometry.x, feature.geometry.y, self.coordSystem);
break;
case 'esriGeometryPolyline':
geometry = new _geoPolyline(self.coordSystem);
feature.geometry.paths.forEach(function (path) {
path.forEach(function (pt) {
geometry.addPoint(pt[0], pt[1]);//4 添加点到线
});
});
break;
case 'esriGeometryPolygon':
geometry = new _geoPolygon(self.coordSystem);
feature.geometry.rings.forEach(function (ring) {
ring.forEach(function (pt) {
geometry.addPoint(pt[0], pt[1]);//4 添加点到线
});
});
break;
}
geometry.properties = feature.attributes;//绑定属性
ly.addGeometry(geometry);
index++;
});
//执行绑定的数据加载成功后要执行的方法
if (fnSucceed) {
fnSucceed(ly, data);
}
else {
self.addLayer(ly);
self.render();
}
},
function (e) {
if (fnFailed) {
fnFailed(e);
}
else {
alert('数据加载失败!');
}
});
},
//更改格网坐标
//@param [coordType] 坐标类型,h3.coordType
//@param [number] 坐标系单位长度
changeCoordSystem: function (coordType, unit) {
let self = this;
unit = unit || self.unit;
self.coordSystem = new _coordSystem(coordType, unit);
self.layerArray.forEach(function (ly) {
ly.reInite(self.coordSystem);
})
self.render();
},
//导出Esri格式json数据
//@param [Array] 几何数组(点或线或面,不可混合)
//return [string] json字符串
exportEsriJson: function (geoArray) {
let self = this;
let oEsriJson = new _esriJson(geoArray);
let jsonStr = JSON.stringify(oEsriJson);
let dlg = new h3.alert(jsonStr, '导出EsriJson');
dlg.show();
return jsonStr;
},
async: function (argunmets) {
//不含参数
if (arguments.length == 1) {
var fnName = arguments[0];
this.partnersAry.forEach((p) => {
p[fnName]();
})
}
//含参数
else if (arguments.length > 1) {
var fnName = arguments[0];
this.partnersAry.forEach((p) => {
p[fnName]();
})
}
},
//地图更新
//一般图层放生增删操作时使用变化type和变化ly(自动调用)
//当图层显示状态变化或者图层的几何数据发生变化 不需要参数(手动调用)
update(type, ly) {
var self = this;
if (type && ly && !ly.forceRender) {
switch (type) {
case self.updateType.add:
chargeLayerUpadta(chargeLayerAdd, ly);
break;
case self.updateType.remove:
chargeLayerUpadta(chargeLayerRemove, ly);
break;
case self.updateType.sort:
break;
case self.updateType.visable:
break;
}
}
else if (!type && !ly) {
this.image = new Map();
this.layerArray.forEach(ly=> {
if (!ly.forceRender)
chargeLayerUpadta(chargeLayerAdd, ly);
})
}
function chargeLayerUpadta(chargeFn, ly) {
ly.geometryArray.forEach(geometry=> {
switch (geometry.type) {
case _geoType.geoPoint:
geometry.vertexArray.forEach(ary=> {
ary.forEach(point=> {
point.symbolColor = geometry.symbolColor;
chargeFn(point);
})
})
break;
case _geoType.geoPolyline:
geometry.pointArray.forEach(function (line) {
line.forEach(function (point) {
point.symbolColor = geometry.symbolColor;
chargeFn(point);
});
});
break;
case _geoType.geoPolygon:
geometry.outerPointArray.forEach(function (line) {
line.forEach(function (point) {
point.symbolColor = geometry.innerSymbolColor;
chargeFn(point);
});
});
geometry.innerPointArray.forEach(function (line) {
line.forEach(function (point) {
point.symbolColor = geometry.outerSymbolColor;
chargeFn(point);
});
});
break;
}
})
}
function chargeLayerAdd(point) {
if (self.imageData.has(point.pointCoord.toString())) {
//如果key已经存在 将point插入数组第一位
self.imageData.get(point.pointCoord.toString()).splice(0, 0, point);
}
else {
//不存在 则初始化 并加入point
self.imageData.set(point.pointCoord.toString(), new Array(point));
}
self.image.set(point.pointCoord.toString(), point);
}
function chargeLayerRemove(point) {
if (self.imageData.has(point.pointCoord.toString())) {
//如果key已经存在 将point插入数组第一位
var ary = self.imageData.get(point.pointCoord.toString());
var index = ary.indexOf(point);
ary.splice(index, 1);
if (ary.length > 0)
self.image.set(point.pointCoord.toString(), ary[0]);
else
self.image.delete(point.pointCoord.toString());
}
}
//待完善
function chargeLayerSort(point) {
if (self.imageData.has(point.pointCoord)) {
//如果key已经存在 将point插入数组第一位
self.imageData.get(point.pointCoord).splice(0, 0, point);
}
else {
//不存在 则初始化 并加入point
self.imageData.set(point.pointCoord, new Array(point));
}
self.image.set(point.pointCoord, point);
}
//待完善
function chargeLayerVisable(point) {
if (self.imageData.has(point.pointCoord)) {
//如果key已经存在 将point插入数组第一位
self.imageData.get(point.pointCoord).splice(0, 0, point);
}
else {
//不存在 则初始化 并加入point
self.imageData.set(point.pointCoord, new Array(point));
}
self.image.set(point.pointCoord, point);
}
}
}
//esri格式的json文件
//@param [Array] 几何数组(点或线或面,不可混合)
let _esriJson = function (geoArray) {
//this.geoArray = geoArray;
this.create(geoArray);
}
_esriJson.prototype = {
displayFieldName: '',
fieldAliases: {
FID: "FID"
},
geometryType: "",
spatialReference: {
wkid: null
},
fields: [
{
name: "FID",
type: "esriFieldTypeOID",//esriFieldTypeInteger esriFieldTypeString esriFieldTypeDouble esriFieldTypeSmallInteger
alias: "FID"
}
],
features: [],
getFeatures: function (geoArray) {
if (!geoArray || geoArray.length == 0)
return;
let feas = [],
oID = 0;
geoArray.forEach(function (geo) {
feas.push({
attributes: getAttributes(geo, oID),
geometry: getEsriGeometry(geo)
})
oID++;
})
return feas;
function getAttributes(geometry, oid) {
let atr = {};
for (let pro in geometry.properties) {
if (pro == 'FID')
atr[pro] = oid;
else
atr[pro] = geometry.properties[pro];
}
return atr;
}
//根据几何类型 创建esri geometry
function getEsriGeometry(geometry) {
let esriGeo = {};
switch (geometry.type) {
case _geoType.geoPoint:
esriGeo = {
x: geometry.x,
y: geometry.y
}
break;
case _geoType.geoPolyline:
esriGeo = (function () {
let paths = [];
geometry.vertexArray.forEach(function (ary) {
let path = [];
ary.forEach(function (pt) {
path.push([pt.pointPixelOrg.x, pt.pointPixelOrg.y]);
})
paths.push(path);
})
return { paths: paths };
})();
break;
case _geoType.geoPolygon:
esriGeo = (function () {
let rings = [];
geometry.vertexArray.forEach(function (ary) {
let ring = [];
ary.forEach(function (pt) {
ring.push([pt.pointPixelOrg.x, pt.pointPixelOrg.y]);
})
rings.push(ring);
})
return { rings: rings };
})();
break;
}
return esriGeo;
}
},
create: function (geoArray) {
let self = this;
if (!geoArray || geoArray.length == 0)
return;
let geometry = geoArray[0];
//几何类型
switch (geometry.type) {
case _geoType.geoPoint:
self.geometryType = 'esriGeometryPoint';
break;
case _geoType.geoPolyline:
self.geometryType = 'esriGeometryPolyline';
break;
case _geoType.geoPolygon:
self.geometryType = 'esriGeometryPolygon';
break;
}
//遍历属性
let fieldAliases = {}, fields = [];
for (let pro in geometry.properties) {
fieldAliases[pro] = pro;
if (pro != 'FID') {
let property = geometry.properties[pro];
switch (typeof (property)) {
case 'string':
fields.push({
name: pro,
type: 'esriFieldTypeString',
alias: pro
})
break;
case 'number':
if (property.toString().split('.').length > 1) {
fields.push({
name: pro,
type: 'esriFieldTypeDouble',
alias: pro
})
}
else {
fields.push({
name: pro,
type: 'esriFieldTypeInteger',
alias: pro
})
}
break;
}
}
else {
fields.push({
name: pro,
type: 'esriFieldTypeOID',
alias: pro
})
}
}
self.fieldAliases = fieldAliases;
self.fields = fields;
self.features = self.getFeatures(geoArray);
}
}
//提示框
//@param [string] 内容
//@param [string] 标题
let _myAlert = function (txt, title) {
var self = this;
title = title || 'Alert';
this.alertDlg = `<div class="my-alert">
<div class="my-alert-con">
<div class="my-alert-con-title">${title}<span class="my-alert-close" title="关闭">×</span></div>
<div class="my-alert-con-p">${txt}</div>
</div>
</div>`;
//显示
this.show = function () {
_select('body')[0].style.position = 'relative';
_select('body')[0].innerHTML += this.alertDlg;
_select('.my-alert-close')[0].addEventListener('click', function () {
self.hide();
})
}
//隐藏
this.hide = function () {
_select('body')[0].removeChild(_select('.my-alert')[0]);
}
}
//图层
//@param [string] 图层名字
let _layer = function (name) {
this.name = name;
this.extent = new _extent();
this.geomtryCount = 0;
this.geometryArray = []
this.geoDictionary = {};
this.visable = true;
this.coordsMap = new Map();
this.imageData = new Map();
this.image = new Map();
this.symbolColor = _colorRGBA(200, 200, 200, 0.5);
}
_layer.prototype = {
//添加几何
addGeometry: function (geometry, key) {
let self = this;
self.extent.change(geometry.extent);
if (key) {
geometry.key = key;
self.geoDictionary[key] = geometry;
}
//针对线的补全和面的填充 不包括对点的操作
switch (geometry.type) {
case _geoType.geoPoint:
geometry.symbolColor = geometry.symbolColor || this.symbolColor;
self.geometryArray.push(geometry);
//给coordsMap赋值
geometry.vertexArray.forEach(ary=> {
ary.forEach(geoPoint=> {
initCoordsMap(geoPoint);
})
})
break;
case _geoType.geoPolyline:
geometry.symbolColor = this.symbolColor;
geometry.create();
self.geometryArray.push(geometry);
//给coordsMap赋值
geometry.pointArray.forEach(ary=> {
ary.forEach(geoPoint=> {
initCoordsMap(geoPoint);
})
})
break;
case _geoType.geoPolygon:
geometry.innerSymbolColor = geometry.innerSymbolColor || self.innerSymbolColor;
geometry.outerSymbolColor = geometry.outerSymbolColor || self.outerSymbolColor;
geometry.create();
self.geometryArray.push(geometry);
//给coordsMap赋值
geometry.innerPointArray.forEach(ary=> {
ary.forEach(geoPoint=> {
initCoordsMap(geoPoint);
})
})
geometry.outerPointArray.forEach(ary=> {
ary.forEach(geoPoint=> {
initCoordsMap(geoPoint);
})
})
break;
}
//self.geometryArray.push(geometry);
self.geomtryCount++;
return geometry;
function initCoordsMap(geoPoint) {
var key = geoPoint.pointCoord.toString()
self.coordsMap.set(key, geoPoint.pointCoord);
self.image.set(key, geoPoint);
if (!self.imageData.has(key))
self.imageData.set(key, []);
self.imageData.get(key).push(geoPoint);
}
},
//坐标系更改后重新初始化(数不变)
reInite: function (coordSystem) {
var geoAry = this.geometryArray;
this.removeAllGeometry();
for (var i = 0; i < geoAry.length; i++) {
var geo = geoAry[i].changeCoordSystem(coordSystem);
this.addGeometry(geo, geo.key);
}
},
getGeometry: function (key) {
return this.geoDictionary[key];
},
removeGeometry: function (geometry) {
if (!geometry)
return;
let self = this,
index = 0;
switch (geometry.type) {
case _geoType.geoPoint:
//删除 coordsMap值
geometry.vertexArray.forEach(ary=> {
ary.forEach(geoPoint=> {
self.coordsMap.delete(geoPoint.pointCoord.toString());
})
})
break;
case _geoType.geoPolyline:
//删除 coordsMap值
geometry.pointArray.forEach(ary=> {
ary.forEach(geoPoint=> {
self.coordsMap.delete(geoPoint.pointCoord.toString());
})
})
break;
case _geoType.geoPoint:
//删除 coordsMap值
geometry.innerPointArray.forEach(ary=> {
ary.forEach(geoPoint=> {
self.coordsMap.delete(geoPoint.pointCoord.toString());
})
})
geometry.outerPointArray.forEach(ary=> {
ary.forEach(geoPoint=> {
self.coordsMap.delete(geoPoint.pointCoord.toString());
})
})
break;
}
index = self.geometryArray.indexOf(geometry);
self.geometryArray.splice(index, 1);
self.geomtryCount--;
if (geometry.key)
delete self.geoDictionary[geometry.key];
return geometry;
},
removeAllGeometry: function () {
this.extent = new _extent();
this.geomtryCount = 0;
this.geometryArray = [];
this.geoDictionary = {};
this.coordsMap = new Map();
this.imageData = new Map();
this.image = new Map();
},
//如果是点和线只需要一个颜色
//如果是面 可以一个也可以两个(inner和outer)颜色