-
Notifications
You must be signed in to change notification settings - Fork 288
Expand file tree
/
Copy pathL7LayerUtil.js
More file actions
2099 lines (1989 loc) · 60.7 KB
/
L7LayerUtil.js
File metadata and controls
2099 lines (1989 loc) · 60.7 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 { FetchRequest } from '../../util/FetchRequest';
import center from '@turf/center';
import * as G2 from '@antv/g2';
import { isNumber } from './util';
import Color from './Color';
import debounce from 'lodash.debounce';
import { getEpsgCodeInfo, getProjection, registerProjection, transformCoodinates } from './epsg-define';
const SelectStyleTypes = {
basic: 'base',
heat: 'heat',
heat3D: 'heat3D',
cluster: 'cluster',
heatGrid: 'heatGrid',
heatHexagon: 'heatHexagon',
heat3DHexagon: 'heat3DHexagon',
heat3DGrid: 'heat3DGrid',
od: 'od',
animateLine: 'animateLine',
column: 'column', // 3D 柱状图
animatePoint: 'animatePoint',
radarPoint: 'radarPoint',
staticChart: 'staticChart',
bar: 'bar',
line: 'line',
pie: 'pie',
line3D: 'line3D',
isoline3D: 'isoline3D',
fillExtrusion: 'fillExtrusion'
};
const MSLayerType = {
point: 'point',
line: 'line',
polygon: 'polygon',
cluster: 'cluster',
heat3DHexagon: 'heat3DHexagon',
heat3DGrid: 'heat3DGrid',
heatmap: 'heatmap',
heatGrid: 'heatGrid',
heatHexagon: 'heatHexagon',
raster: 'raster',
image: 'image',
radar: 'radar'
};
const ANTVL7_LINE_EXTRUSION_STYLE_KEY = {
'line-extrusion-dasharray': 'dashArray',
'line-extrusion-opacity': 'opacity',
'line-extrusion-pattern-interval': 'iconStep',
'line-extrusion-pattern-blend': 'textureBlend',
'line-extrusion-pattern-opacity': 'opacity',
'line-extrusion-base-fixed': 'heightfixed'
};
const ANTVL7_ANIMATE_LINE_EXTRUSION_STYLE_KEY = {
'line-extrusion-animate-duration': 'duration',
'line-extrusion-animate-interval': 'interval',
'line-extrusion-animate-trailLength': 'trailLength'
};
const AntvL7LayerType = {
MarkerLayer: 'MarkerLayer'
};
const TEXT_MAPBOXGL_ANTVL7_KEY = {
'text-color': 'color',
'text-size': 'size',
'text-opacity': 'opacity',
'text-translate': 'textOffset',
'text-z-offset': 'raisingHeight',
'text-allow-overlap': 'textAllowOverlap',
'text-anchor': 'textAnchor',
'text-letter-spacing': 'spacing',
'text-halo-color': 'stroke',
'text-halo-width': 'strokeWidth',
'text-halo-blur': 'halo',
'text-font': 'fontFamily',
'text-field': 'shape'
};
const LineLayoutKey = ['line-join', 'line-cap', 'line-miter-limit', 'line-round-limit'];
const PointLayoutKey = [
'icon-allow-overlap',
'icon-image',
'icon-size',
'fill-sort-key',
'visibility',
'icon-anchor',
'icon-ignore-placement',
'icon-rotate',
'symbol-placement',
'text-justify',
'text-transform',
'text-field',
'text-allow-overlap',
'text-size',
'text-font',
'text-line-height',
'text-max-width',
'text-rotate',
'text-anchor',
'text-ignore-placement',
'text-letter-spacing',
'text-z-offset'
];
const layoutStyleNames = [...PointLayoutKey, ...LineLayoutKey];
const IPortalDataTypes = {
STRING: 'STRING',
INT: 'INT',
LONG: 'LONG',
DOUBLE: 'DOUBLE',
BOOLEAN: 'BOOLEAN',
TEXT: 'TEXT',
LONGTEXT: 'LONGTEXT',
POINT: 'POINT',
LINESTRING: 'LINESTRING',
POLYGON: 'POLYGON',
MULTIPOINT: 'MULTIPOINT',
MULTILINESTRING: 'MULTILINESTRING',
MULTIPOLYGON: 'MULTIPOLYGON'
};
// rest data iServer返回的字段类型
const RestDataTypes = {
BOOLEAN: 'BOOLEAN',
BYTE: 'BYTE',
CHAR: 'CHAR',
DATETIME: 'DATETIME',
DOUBLE: 'DOUBLE',
INT16: 'INT16',
INT32: 'INT32',
INT64: 'INT64',
LONGBINARY: 'LONGBINARY',
SINGLE: 'SINGLE',
WTEXT: 'WTEXT',
TEXT: 'TEXT'
};
// 字段属性
const propertyType = {
NUMBER: 'NUMBER',
STRING: 'STRING',
BOOLEAN: 'BOOLEAN'
};
const StyleRenderType = {
base: 'base',
heat: 'heat',
heat3D: 'heat3D',
cluster: 'cluster',
heatGrid: 'heatGrid',
heatHexagon: 'heatHexagon',
heat3DGrid: 'heat3DGrid',
heat3DHexagon: 'heat3DHexagon',
od: 'od',
animateLine: 'animateLine',
column: 'column',
animatePoint: 'animatePoint',
radarPoint: 'radarPoint',
staticChart: 'staticChart',
bar: 'bar',
line: 'line',
pie: 'pie',
isoline3D: 'isoline3D',
line3D: 'line3D',
fillExtrusion: 'fillExtrusion'
};
const L7_WIDTH_MULTIPLE = 0.5;
export function isL7Layer(layer) {
const layout = layer.layout || {};
return (
(layer.type === 'circle' && layout['circle-animate-rings']) || // 动画点
layer.type === 'radar' || // 雷达图
layer.type === 'point-extrusion' || // 3D柱状图
layer.type === 'line-curve' || // OD
layer.type === 'line-curve-extrusion' || // OD-3D
layer.type === 'line-extrusion' || // 3D线
layer.type === 'chart' || // 统计专题图
(layer.type === 'heatmap' && layout['heatmap-shape']) || // L7-2D热力图
layer.type === 'heatmap-extrusion' || // L7-3D热力图
(layer.type === 'symbol' && layout['text-z-offset'] > 0)
);
}
export function getL7Filter(filter, featureFilter) {
if (!filter) {
return;
}
const [condition, ...expressions] = filter;
const newExpressions = expressions.filter((exp) => {
const [, f] = exp;
if (['$type', '$id'].includes(f)) {
return false;
}
return true;
});
const field = Array.from(new Set(getFilterFields(newExpressions)));
const fFilter = featureFilter([condition, ...newExpressions]);
const filterFunc = fFilter.filter.bind(fFilter);
return {
field,
values: (...args) => {
const properties = {};
field.forEach((f, idx) => {
properties[f] = args[idx];
});
const result = filterFunc(
{},
{
properties
}
);
return result;
}
};
}
export function L7LayerUtil(config) {
const { featureFilter, expression, spec, L7Layer, L7, proj4 } = config;
/**
* @param {string} url
* @param {string} [token]
* @returns {string}
*/
function addCredentialToUrl(url, credential) {
if (!credential) {
return url;
}
const tokenInfo = `${credential.key || credential.name}=${credential.value}`;
const newUrl = url.includes('?') ? `${url}&${tokenInfo}` : `${url}?${tokenInfo}`;
return newUrl;
}
/**
* @description 获取数据集表头信息
* @param {string} datasetUrl 数据集地址
* @param {Object} credential
* @param {Object} options
*/
function getRestDataFields(datasetUrl, credential, options) {
const url = addCredentialToUrl(`${datasetUrl}/fields.json?returnAll=true`, credential);
return FetchRequest.get(url, null, options)
.then((res) => res.json())
.then((result) => {
return result.map((item) => {
const { caption, name, isSystemField, type, maxLength, isZeroLengthAllowed } = item;
return {
name,
type,
title: caption,
visible: true,
isSystemField,
maxLength, // MaxLength字节最大长度
isZeroLengthAllowed // 是否允许零长度
};
});
});
}
/**
* @description 获取值域
* @param {string} datasetUrl
* @param {Object} credential
* @param {Object} options
*/
function getRestDataDomains(datasetUrl, credential, options) {
const url = addCredentialToUrl(`${datasetUrl}/domain.json`, credential);
return FetchRequest.get(url, null, options).then((result) => {
return result.json();
});
}
/**
* restdata字段排序,系统字段放在最前面
* @param fieldInfos 需要排序的
*/
function sortRestdataField(fieldInfos) {
const systemFields = [];
const nonsystemFields = [];
fieldInfos.forEach((fieldInfo) => {
if (fieldInfo.isSystemField) {
systemFields.push(fieldInfo);
} else {
nonsystemFields.push(fieldInfo);
}
});
return systemFields.concat(nonsystemFields);
}
async function requestRestDataFieldsInfo(datasetUrl, credential, options) {
const [fields, domains] = await Promise.all([
getRestDataFields(datasetUrl, credential, options),
getRestDataDomains(datasetUrl, credential, options)
]);
domains.forEach((domain) => {
const { fieldName, type, rangeInfos, codeInfos } = domain;
const field = fields.find((item) => item.name === fieldName);
if (!field) {
return;
}
field.domain = {
type,
infos:
type === 'RANGE'
? rangeInfos.map((rangeInfo) => pickAttrs(rangeInfo, ['max', 'min', 'type']))
: codeInfos.map(({ value }) => value)
};
});
return sortRestdataField(fields);
}
/**
* 获取restDatafield
* @param datasetUrl
* @param credential
* @param options
*/
async function getRestDataFieldInfo(datasetUrl, credential, options) {
const fieldsInfos = await requestRestDataFieldsInfo(datasetUrl, credential, options);
return {
fieldNames: fieldsInfos.map((el) => el.name),
fieldTypes: fieldsInfos.map((el) => el.type)
};
}
/**
* featureResults中的字段名称为全部大写
* field中的字段名称为首字母大写
* @param properties
* @param fields
*/
function transformFeatureField(properties, fields) {
return Object.keys(properties).reduce((result, k) => {
const newKey = fields.find((f) => f.toUpperCase() === k.toUpperCase());
newKey && (result[newKey] = properties[k]);
return result;
}, {});
}
/**
* 将字段和字段类型组装成对象
* @param fields
* @param fieldTypes
*/
function getFieldTypeInfo(fields, fieldTypes) {
return (
fields &&
fields.reduce((pre, cur, idx) => {
pre[cur] = fieldTypes[idx];
return pre;
}, {})
);
}
/**
* 是否Iportal数值字段
* @param type
*/
function isPortalNumberData(type) {
return [IPortalDataTypes.INT, IPortalDataTypes.LONG, IPortalDataTypes.DOUBLE].includes(type);
}
/**
* 是否Iportal数值字段
* @param type
*/
function isRestDataNumberData(type) {
return [
RestDataTypes.DOUBLE,
RestDataTypes.INT16,
RestDataTypes.INT32,
RestDataTypes.INT64,
RestDataTypes.SINGLE
].includes(type);
}
function isNumberType(type) {
return isPortalNumberData(type) || isRestDataNumberData(type);
}
function transformDataType(type, value) {
const isNumber = isNumberType(type);
if (isNumber) {
return Number(value);
}
const isBoolean = type === propertyType.BOOLEAN;
if (isBoolean) {
return Boolean(value);
}
return value;
}
/**
* iServer返回的feature属性值都是字符串,这里按照字段类型,将属性值转换成类型正确的值
* @param features
* @param fieldTypesInfo
* @returns
*/
function formatFeatures(features, fieldTypesInfo) {
return features.map((feature) => {
if (feature) {
for (const key in feature.properties) {
feature.properties[key] = transformDataType((fieldTypesInfo || {})[key], (feature.properties || {})[key]);
}
}
return feature;
});
}
/**
* 将featureResult的feature按feild中的名称和类型进行转换
* @param originFeatures
* @param fieldNames
* @param fieldTypes
*/
function transformFeaturesNameAndType(originFeatures, fieldNames, fieldTypes) {
const features = originFeatures.map((f) => {
return { ...f, properties: transformFeatureField(f.properties || {}, fieldNames) };
});
const fieldInfo = getFieldTypeInfo(fieldNames, fieldTypes);
return formatFeatures(features, fieldInfo);
}
function isIportalProxyServiceUrl(url, options) {
return options.iportalServiceProxyUrlPrefix && url.indexOf(options.iportalServiceProxyUrlPrefix) >= 0;
}
function handleWithRequestOptions(url, options) {
if (isIportalProxyServiceUrl(url, options)) {
return { ...options, withCredentials: true };
}
return { ...options, withCredentials: undefined };
}
/**
* 通过webMapsource获取restData-geojson
* @param data
* @param options
*/
async function getRestDataGeojsonByWebMap(data, options) {
const { url, dataSourceName, datasetName, credential } = data;
const SQLParams = {
datasetNames: [dataSourceName + ':' + datasetName],
getFeatureMode: 'SQL',
targetEpsgCode: '4326',
queryParameter: { name: datasetName + '@' + dataSourceName }
};
const datasetUrl = `${url.split('featureResults')[0]}datasources/${dataSourceName}/datasets/${datasetName}`;
const nextOptions = handleWithRequestOptions(datasetUrl, options);
const { fieldNames, fieldTypes } = await getRestDataFieldInfo(datasetUrl, credential, nextOptions);
const nextUrl = addCredentialToUrl(url, credential);
const attrDataInfo = await FetchRequest.post(nextUrl, JSON.stringify(SQLParams), nextOptions);
const featuresRes = await attrDataInfo.json();
return {
type: 'FeatureCollection',
features: transformFeaturesNameAndType((featuresRes || {}).features || [], fieldNames, fieldTypes)
};
}
/**
* 获取单条item
* @param href
* @param option
*/
function getStructDataItemJson(href, option) {
return FetchRequest.get(href, null, option)
.then((res) => res.json())
.then((data) => {
if (data.succeed === false) {
throw data.error.errorMsg;
}
return data;
});
}
/**
* 通过返回的links递归请求所有item
* @param href
* @param options
*/
async function getStructDataItem(href, options) {
const data = await getStructDataItemJson(href, options);
const { features, links = [] } = data || {};
const nextInfo = links.find((l) => l.rel === 'next');
if (nextInfo) {
return features.concat(await getStructDataItem(nextInfo.href, options));
} else {
return features;
}
}
/**
* 请求结构化数据的所有Feature
* @param datasetId iportal结构化数据id
* @param options
*/
async function getStructDataGeojson(datasetId, options) {
const href = `${options.server}web/datas/${datasetId}/structureddata/ogc-features/collections/all/items.json?limit=10000&offset=0`;
const allFeature = await getStructDataItem(href, options);
return {
type: 'FeatureCollection',
features: allFeature
};
}
/**
* 坐标转换坐标点
* @param fromProjection 源坐标系
* @param toProjection 目标坐标系
* @param coordinates 坐标点
*/
function transformCoordinate(fromProjection, toProjection, coordinates) {
if (fromProjection === toProjection) {
return coordinates;
}
// proj4缺陷,EPSG:4214的坐标x为180,转换后变成-179.
if (fromProjection === 'EPSG:4214' && toProjection === 'EPSG:4326' && coordinates[0] === 180) {
const newCoordinate = transformCoodinates({
coordinates,
sourceProjection: fromProjection,
destProjection: toProjection,
proj4
});
newCoordinate[0] = 180;
return newCoordinate;
}
return transformCoodinates({
coordinates,
sourceProjection: fromProjection,
destProjection: toProjection,
proj4
});
}
/**
* 坐标转换坐标---点线面,多点,多线,多面
* @param fromProjection 源坐标系
* @param toProjection 目标坐标系
* @param geometry 坐标点
*/
function transformGeometryCoordinatesUtil(fromProjection, toProjection, geometry) {
if (geometry.type === 'MultiPolygon') {
const coordinates = geometry.coordinates;
const newGeometry = {
type: geometry.type,
coordinates: coordinates.map((items) => {
return items.map((item) => {
return item.map((c) => {
return transformCoordinate(fromProjection, toProjection, c);
});
});
})
};
return newGeometry;
}
if (geometry.type === 'Polygon' || geometry.type === 'MultiLineString') {
const coordinates = geometry.coordinates;
const newGeometry = {
type: geometry.type,
coordinates: coordinates.map((items) => {
return items.map((item) => {
return transformCoordinate(fromProjection, toProjection, item);
});
})
};
return newGeometry;
}
if (geometry.type === 'MultiPoint' || geometry.type === 'LineString') {
const coordinates = geometry.coordinates;
const newGeometry = {
type: geometry.type,
coordinates: coordinates.map((item) => {
return transformCoordinate(fromProjection, toProjection, item);
})
};
return newGeometry;
}
if (geometry.type === 'Point') {
const newGeometry = {
type: geometry.type,
coordinates: transformCoordinate(fromProjection, toProjection, geometry.coordinates)
};
return newGeometry;
}
}
/**
* @description 由于绘制插件只支持4326,所以需要将feature的坐标进行转换
* @param datasetId 选择要素
* @param featureIds 选择要素对应的图层(从reducer中获取的)
*/
function transformGeometryCoordinates(features, fromProjection, toProjection = 'EPSG:4326') {
if (!features || !features.length || !fromProjection) {
return;
}
if (fromProjection === toProjection) {
return features;
}
return features.map((f) => {
const geometry = transformGeometryCoordinatesUtil(fromProjection, toProjection, f.geometry);
const newFeature = { ...f };
geometry && Object.assign(newFeature, { geometry });
return newFeature;
});
}
/**
* 通过webMapsource获取StructuredData-geojson
* @param data
* @param options
*/
async function getStructuredDataGeojsonByWebMap(data, options) {
const allFeature = await getStructDataGeojson(data.dataId, options);
const resultRes = await FetchRequest.get(
`${options.server}web/datas/${data.dataId}/structureddata.json`,
null,
options
);
const result = await resultRes.json();
const projection = `EPSG:${result.epsgCode}`;
if (projection !== 'EPSG:4326') {
if (!getProjection(projection, proj4)) {
const epsgWKT = await getEpsgCodeInfo(projection, options.server);
registerProjection(projection, epsgWKT, proj4);
}
const newFeatures = transformGeometryCoordinates(allFeature.features, projection);
newFeatures && (allFeature.features = newFeatures);
}
return allFeature;
}
function webmapODToParser(data) {
const { originX, originY, destinationX, destinationY } = data;
return {
type: 'json',
x: originX,
y: originY,
x1: destinationX,
y1: destinationY
};
}
function webmapClusterToTransform(data) {
const { clusterField, clusterMethod, clusterRadius, clusterType } = data;
const methodRules = {
avg: 'mean'
};
return [
{
type: clusterType,
field: clusterField,
method: (clusterMethod && methodRules[clusterMethod]) || clusterMethod,
size: clusterRadius
}
];
}
/**
* 构造L7 geojsonsource
* @param source
* @param sourceLayer
* @param options
*/
async function geoJSONSourceToL7Source(source, sourceLayer, options) {
const { data, od, cluster } = source;
if (data && data.type === 'FeatureCollection') {
return {
data
};
}
const rules = {
'supermap-rest-data': getRestDataGeojsonByWebMap,
'supermap-structured-data': getStructuredDataGeojsonByWebMap
};
const parser = od && webmapODToParser(source);
const transforms = cluster && webmapClusterToTransform(source);
return {
data: rules[data.type] && (await rules[data.type](source.data, options)),
parser,
transforms
};
}
/**
* 构造L7mvt-source
* @param source
* @returns {Object} L7 mvt source
*/
function vectorSourceToL7Source(source, sourceLayer, options) {
const result = {
data: ((source || {}).tiles || [])[0],
parser: {
type: 'mvt',
extent: source.bounds,
sourceLayer
}
};
const requestParameters = options.transformRequest(result.data, 'Tile');
if (requestParameters) {
if (requestParameters.credentials) {
result.parser.requestParameters = { credentials: requestParameters.credentials };
}
if (requestParameters.headers) {
result.parser.requestParameters = { ...result.parser.requestParameters, headers: requestParameters.headers };
}
}
return result;
}
/**
* WebMapsource TO L7 source
* @param source
* @param sourceLayer mvt才有此参数
* @param options
*/
function WebMapSourceToL7Source(source, sourceLayer, options) {
const type = source.type;
const rules = {
vector: vectorSourceToL7Source,
geojson: geoJSONSourceToL7Source
};
return rules[type] && rules[type](source, sourceLayer, options);
}
/**
* 获取L7图层所需要的共有的图层信息
* @param layer
*/
function getL7LayerCommonInfo(layer) {
const { type, id, minzoom, maxzoom, layout, paint, filter, source } = layer;
return {
id,
options: {
type,
name: layer.id,
sourceLayer: layer['source-layer'],
source,
layout,
paint,
filter,
minZoom: minzoom,
maxZoom: maxzoom,
visible: layout.visibility === 'none' ? false : true
},
filter: getL7Filter(filter, featureFilter)
};
}
function expressionToFunction({ value, property, multiple, map }) {
if (!expression.isExpression(value)) {
return {
values: multiple ? value * multiple : value
};
}
const typeRules = {
size: 'paint_circle',
color: 'paint_circle',
shape: 'layout_symbol'
};
const rules = {
size: 'circle-radius',
color: 'circle-color',
shape: 'icon-image'
};
const fieldRules = {
interpolate: (compiledExpression) => {
return compiledExpression.value._styleExpression.expression.input.args[0].args[0].evaluate(
compiledExpression.value._styleExpression._evaluator
);
},
match: (compiledExpression) => {
return compiledExpression.value._styleExpression.expression.input.args[0].evaluate(
compiledExpression.value._styleExpression._evaluator
);
},
case: (compiledExpression) => {
const branches = compiledExpression.value._styleExpression.expression.branches[0][0];
// args[0]适用于分段,arg.lhs适用于单值转换成case表达式
if (branches && branches.args) {
return branches.args[0].lhs.args[0].args[0].evaluate(compiledExpression.value._styleExpression._evaluator);
}
return branches.lhs.args[0].evaluate();
}
};
const propertySpec = spec[typeRules[property]][rules[property]];
const compiledExpression = expression.createPropertyExpression(value, propertySpec);
const field = fieldRules[value[0]] && fieldRules[value[0]](compiledExpression);
const newFunc = compiledExpression.value.evaluate.bind(compiledExpression.value);
const callback = (v) => {
const f = {
properties: { [field]: v },
type: 'point'
};
const result = newFunc({ zoom: map.getZoom() }, f);
return property === 'shape' ? result.toString() : multiple ? result * multiple : result;
};
return {
field,
values: callback
};
}
function getLineCurveStyleKey(type, key) {
const ANTVL7_LINE_CURVE_STYLE_KEY = {
[`${type}-dasharray`]: 'dashArray',
[`${type}-opacity`]: 'opacity',
[`${type}-pattern-color`]: 'textureColor',
[`${type}-pattern-opacity`]: 'opacity',
[`${type}-pattern-interval`]: 'iconStep',
[`${type}-pattern-blend`]: 'textureBlend',
[`${type}-segment`]: 'segmentNumber',
[`${type}-pattern-rotate`]: 'textureRotate'
};
return ANTVL7_LINE_CURVE_STYLE_KEY[key];
}
function getLineCurveAnimateKey(type, key) {
const ANTVL7_ANIMATE_LINE_CURVE_STYLE_KEY = {
[`${type}-animate-duration`]: 'duration',
[`${type}-animate-interval`]: 'interval',
[`${type}-animate-trailLength`]: 'trailLength'
};
return ANTVL7_ANIMATE_LINE_CURVE_STYLE_KEY[key];
}
/**
* 返回L7函数需要的字段
* 动画线:宽高
* 3D柱状图:长宽高
* @param args
*/
function getCompositeField(...args) {
return args
.filter((a) => a.field)
.map((a) => {
return a.field;
})
.join('*');
}
/**
* 返回L7函数
* args为
* 动画线:宽高
* 3D柱状图:长宽高
* @param valueArgs
* @param args
*/
function getCompositeCallback(valueArgs, args) {
const newValueArgs = [...valueArgs];
return args.map((attr) => {
const multiple = attr.multiple;
if (attr.field) {
const value = newValueArgs[0];
newValueArgs.unshift();
const result = attr && attr.values ? attr.values(value) : value;
return multiple && isNumber(result) ? result * multiple : result;
}
return multiple && isNumber(attr.values) ? attr.values * multiple : attr.values;
});
}
/**
* 将表达式转换成L7支持的function
* @param value
* @param property
*/
function expressionMultiToFunction(map, ...size) {
const functions = size.map(({ value, multiple }) => expressionToFunction({ value, property: 'size', multiple, map }));
const field = getCompositeField(...functions);
let values;
if (field !== '') {
values = (...valueArgs) => {
// valueArgs的个数由field来决定
return getCompositeCallback(valueArgs, functions);
};
} else {
values = size.map((v, idx) => {
const multiple = size[idx].multiple || 1;
return v.value * multiple;
});
}
return {
field: field !== '' ? field : undefined,
values
};
}
function webmapAttibutesToChartBarFields(attributes) {
return attributes.reduce((result, [field, color]) => {
result[field] = color;
return result;
}, {});
}
function webmapAttibutesToChartLineFields(attributes) {
return attributes.map(([field]) => field);
}
/**
* 表达式转换为rampColors
* @param color
*/
function colorExpressionToRampColors(color) {
const propertySpec = spec[`paint_heatmap`]['heatmap-color'];
const compiledExpression = expression.createPropertyExpression(color, propertySpec);
const positions = compiledExpression.value._styleExpression.expression.labels;
const colors = compiledExpression.value._styleExpression.expression.outputs.map((v) => {
return v.value;
});
return {
positions,
colors
};
}
function getSelectType(currentLayer) {
const layout = currentLayer.layout || {};
if (
currentLayer.type === 'heatmap' &&
layout['heatmap-shape'] &&
['square', 'hexagon'].includes(layout['heatmap-shape'])
) {
return SelectStyleTypes.heatGrid;
}
if (
currentLayer.type === 'heatmap-extrusion' &&
layout['heatmap-extrusion-shape'] &&
['squareColumn', 'hexagonColumn'].includes(layout['heatmap-extrusion-shape'])
) {
return SelectStyleTypes.heat3DGrid;
}
if (currentLayer.type === 'heatmap-extrusion') {
return SelectStyleTypes.heat3D;
}
if (currentLayer.type === 'chart' && ['bar', 'line'].includes(layout['chart-type'])) {
return SelectStyleTypes.bar;
}
if (currentLayer.type === 'chart' && layout['chart-type'] === 'pie') {
return SelectStyleTypes.pie;
}
if (currentLayer.type === 'radar') {
return SelectStyleTypes.radarPoint;
}
if (currentLayer.type === 'point-extrusion') {
return SelectStyleTypes.column;
}
if (['line-curve', 'line-curve-extrusion'].includes(currentLayer.type)) {
return SelectStyleTypes.od;
}
if (currentLayer.type === 'line-extrusion') {
return SelectStyleTypes.line;
}
if (layout['circle-animate-rings'] !== undefined) {
return SelectStyleTypes.animatePoint;
}
return '';
}
function getPaintOrLayutByStyleName(styleName) {
return layoutStyleNames.includes(styleName) ? 'layout' : 'paint';
}
function omitAttrs(obj, excludeKeys) {
const nextObj = {};
for (const key in obj) {
if (!excludeKeys.includes(key)) {
nextObj[key] = obj[key];
}
}
return obj;
}
function pickAttrs(obj, includeKeys) {
const nextObj = {};
for (const key in obj) {
if (includeKeys.includes(key)) {
nextObj[key] = obj[key];
}
}
return obj;
}
function isSolidDasharray(dasharray) {
return dasharray.length === 2 && dasharray[0] === 1 && dasharray[1] === 0;
}
/**
* 根据dasharray获取线型
* @param dasharray
*/
function getLineTypeByDashArray(dasharray) {
if (dasharray && dasharray.length > 1 && !isSolidDasharray(dasharray)) {