-
Notifications
You must be signed in to change notification settings - Fork 409
/
Copy pathLayersUtils.js
1173 lines (1096 loc) · 42.5 KB
/
LayersUtils.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
/*
* Copyright 2017, GeoSolutions Sas.
* All rights reserved.
*
* This source code is licensed under the BSD-style license found in the
* LICENSE file in the root directory of this source tree.
*/
import assign from 'object-assign';
import toBbox from 'turf-bbox';
import uuidv1 from 'uuid/v1';
import isString from 'lodash/isString';
import isObject from 'lodash/isObject';
import isArray from 'lodash/isArray';
import head from 'lodash/head';
import castArray from 'lodash/castArray';
import isEmpty from 'lodash/isEmpty';
import findIndex from 'lodash/findIndex';
import pick from 'lodash/pick';
import isNil from 'lodash/isNil';
import get from 'lodash/get';
import { addAuthenticationParameter } from './SecurityUtils';
import { getEPSGCode } from './CoordinatesUtils';
import { ANNOTATIONS, updateAnnotationsLayer, isAnnotationLayer } from '../plugins/Annotations/utils/AnnotationsUtils';
import { getLocale } from './LocaleUtils';
let LayersUtils;
let regGeoServerRule = /\/[\w- ]*geoserver[\w- ]*\//;
export const NodeTypes = {
LAYER: 'layers',
GROUP: 'groups'
};
export const DEFAULT_GROUP_ID = 'Default';
export const ROOT_GROUP_ID = 'root';
const getGroup = (groupId, groups) => {
return head(groups.filter((subGroup) => isObject(subGroup) && subGroup.id === groupId));
};
const getLayer = (layerName, allLayers) => {
return head(allLayers.filter((layer) => layer.id === layerName));
};
const getLayersId = (groupId, allLayers) => {
return allLayers.filter((layer) => (layer.group || DEFAULT_GROUP_ID) === groupId).map((layer) => layer.id).reverse();
};
/**
* utility to check
* @param {object} l layer data
* @returns wps url or fallback to other layer urls
*/
export const getWpsUrl = l => l && l.wpsUrl || (l.search && l.search.url) || l.url;
const initialReorderLayers = (groups, allLayers) => {
return groups.slice(0).reverse().reduce((previous, group) => {
return previous.concat(
group.nodes.slice(0).reverse().reduce((layers, node) => {
if (isObject(node)) {
return layers.concat(initialReorderLayers([node], allLayers));
}
return layers.concat(getLayer(node, allLayers));
}, [])
);
}, []);
};
const reorderLayers = (groups, allLayers) => {
return initialReorderLayers(groups, allLayers);
};
const createGroup = (groupId, groupTitle, groupName, layers, addLayers) => {
return assign({}, {
id: groupId,
title: groupTitle ?? (groupName || "").replace(/\${dot}/g, "."),
name: groupName,
nodes: addLayers ? getLayersId(groupId, layers) : [],
expanded: true
});
};
const getElevationDimension = (dimensions = []) => {
return dimensions.reduce((previous, dim) => {
return dim.name.toLowerCase() === 'elevation' || dim.name.toLowerCase() === 'depth' ?
assign({
positive: dim.name.toLowerCase() === 'elevation'
}, dim, {
name: dim.name.toLowerCase() === 'elevation' ? dim.name : 'DIM_' + dim.name
}) : previous;
}, null);
};
const addBaseParams = (url, params) => {
const query = Object.keys(params).map((key) => key + '=' + encodeURIComponent(params[key])).join('&');
return url.indexOf('?') === -1 ? (url + '?' + query) : (url + '&' + query);
};
const isSupportedLayerFunc = (layer, maptype) => {
const LayersUtil = require('./' + maptype + '/Layers');
const Layers = LayersUtil.default || LayersUtil;
if (layer.type === "mapquest" || layer.type === "bing") {
return Layers.isSupported(layer.type) && layer.apiKey && layer.apiKey !== "__API_KEY_MAPQUEST__" && !layer.invalid;
}
/*
* type 'empty' represents 'No background' layer
* previously was checking for types
*/
if (layer.type === 'empty') {
return true;
}
return Layers.isSupported(layer.type) && !layer.invalid;
};
const checkInvalidParam = (layer) => {
return layer && layer.invalid ? assign({}, layer, {invalid: false}) : layer;
};
export const getNode = (nodes, id) => {
if (nodes && isArray(nodes)) {
return nodes.reduce((previous, node) => {
if (previous) {
return previous;
}
if (node && (node.name === id || node.id === id || node === id)) {
return node;
}
if (node && node.nodes && node.nodes.length > 0) {
return getNode(node.nodes, id);
}
return previous;
}, null);
}
return null;
};
export const getGroupNodes = (node) => {
if (node && node.nodes) {
return node.nodes.reduce((a, b) => {
let nodes = [].concat(a);
if (b.nodes) {
nodes = a.concat(getGroupNodes(b));
}
if (isString(b)) {
return [...nodes, b];
}
return [...nodes, b.id];
}, []);
}
return [];
};
/**
* Gets title of nested groups from Default
* @param {string} id of group
* @param {array} groups groups of map
* @return {string} title of the group
*/
export const getNestedGroupTitle = (id, groups = []) => {
return isArray(groups) && head(groups.map(group => {
const groupObj = group.id === id ? group : null;
if (groupObj) {
return groupObj.title;
}
const nodeObj = getNode(group.nodes, id);
return nodeObj ? nodeObj.title : null;
}));
};
/**
* Flatten nested groupDetails to a one-level groupDetails
* @param {(Object[]|Object)} groupDetails of objects
* @returns {Object[]} flattened groupDetails
*/
export const flattenArrayOfObjects = (groupDetails) => {
let result = [];
groupDetails && castArray(groupDetails).forEach((a) => {
result.push(a);
if (a.nodes && Array.isArray(groupDetails) && Array.isArray(a.nodes)) {
result = result.concat(flattenArrayOfObjects(a.nodes));
}
});
return result;
};
/**
* Gets group title by id
* @param {string} id of group
* @param {object[]} groups groups of map
* @returns {string} title of the group
*/
export const displayTitle = (id, groups) => {
if (groups && Array.isArray(groups)) {
for (let group of groups) {
if (group?.id === id) {
return group.title;
}
}
}
return DEFAULT_GROUP_ID;
};
/**
* adds or update node property in a nested node
* if propName is an object it overrides a whole group of options instead of one
*/
export const deepChange = (nodes, findValue, propName, propValue) => {
if (nodes && isArray(nodes) && nodes.length > 0) {
return nodes.map((node) => {
if (isObject(node)) {
if (node.id === findValue) {
return {...node, ...(isObject(propName) ? propName : {[propName]: propValue})};
} else if (node.nodes) {
return {...node, nodes: deepChange(node.nodes, findValue, propName, propValue)};
}
}
return node;
});
}
return [];
};
export const updateAvailableTileMatrixSetsOptions = ({ tileMatrixSet, matrixIds, ...layer }) => {
if (!layer.availableTileMatrixSets && tileMatrixSet && matrixIds) {
const matrixIdsKeys = isArray(matrixIds) ? matrixIds : Object.keys(matrixIds);
const availableTileMatrixSets = matrixIdsKeys
.reduce((acc, key) => {
const tileMatrix = (tileMatrixSet || []).find((matrix) => matrix['ows:Identifier'] === key);
if (!tileMatrix) {
return acc;
}
const limits = isObject(matrixIds) ? matrixIds[key] : null;
const isLayerLimit = !!(limits || []).find(({ ranges }) => !!ranges);
const tileMatrixCRS = getEPSGCode(tileMatrix['ows:SupportedCRS'] || '');
return {
...acc,
[key]: {
crs: tileMatrixCRS,
...(isLayerLimit && { limits }),
tileMatrixSet: tileMatrix
}
};
}, {});
return { ...layer, availableTileMatrixSets };
}
return layer;
};
/**
* Extracts the sourceID of a layer.
* @param {object} layer the layer object
*/
export const getSourceId = (layer = {}) => layer.capabilitiesURL || head(castArray(layer.url));
export const getTileMatrixSetLink = (layer = {}, tileMatrixSetId) => `sources['${getSourceId(layer)}'].tileMatrixSet['${tileMatrixSetId}']`;
/**
* It extracts tile matrix set from sources and add them to the layer
*
* @param sources {object} sources object from state or configuration
* @param layer {object} layer to check
* @return {object} new layers with tileMatrixSet and matrixIds (if needed)
*/
export const extractTileMatrixFromSources = (sources, layer) => {
if (!sources || !layer) {
return {};
}
if (layer.availableTileMatrixSets) {
const availableTileMatrixSets = Object.keys(layer.availableTileMatrixSets)
.reduce((acc, tileMatrixSetId) => {
const tileMatrixSetLink = getTileMatrixSetLink(layer, tileMatrixSetId);
const tileMatrixSet = get({ sources }, tileMatrixSetLink);
if (tileMatrixSet) {
return {
...acc,
[tileMatrixSetId]: {
...layer.availableTileMatrixSets[tileMatrixSetId],
tileMatrixSet
}
};
}
return {
...acc,
[tileMatrixSetId]: layer.availableTileMatrixSets[tileMatrixSetId]
};
}, {});
return { availableTileMatrixSets };
}
if (!isArray(layer.matrixIds) && isObject(layer.matrixIds)) {
layer.matrixIds = [...Object.keys(layer.matrixIds)];
}
const sourceId = getSourceId(layer);
const matrixIds = layer.matrixIds && layer.matrixIds.reduce((acc, mI) => {
const ids = (sources?.[sourceId]?.tileMatrixSet?.[mI]?.TileMatrix || [])
.map(i => ({
identifier: i['ows:Identifier'],
ranges: i.ranges
}));
return ids.length === 0 ? acc : { ...acc, [mI]: [...ids] };
}, {}) || null;
const tileMatrixSet = layer.tileMatrixSet && layer.matrixIds.map(mI => sources[sourceId].tileMatrixSet[mI]).filter(v => v) || null;
const newTileMatrixOptions = updateAvailableTileMatrixSetsOptions((tileMatrixSet && matrixIds) ? { tileMatrixSet, matrixIds } : {});
return newTileMatrixOptions;
};
/**
* It extracts tile matrix set from layers and add them to sources map object
*
* @param {object} groupedLayersByUrl layers grouped by url
* @param {object} [sources] current sources map object
* @return {object} new sources object with data from layers
*/
export const extractTileMatrixSetFromLayers = (groupedLayersByUrl, sources = {}) => {
return Object.keys(groupedLayersByUrl || [])
.reduce((acc, url) => {
const layers = groupedLayersByUrl[url];
const tileMatrixSet = layers.reduce((layerAcc, layer) => {
const { availableTileMatrixSets } = updateAvailableTileMatrixSetsOptions(layer);
return {
...layerAcc,
...Object.keys(availableTileMatrixSets).reduce((tileMatrixSetAcc, tileMatrixSetId) => ({
...tileMatrixSetAcc,
[tileMatrixSetId]: availableTileMatrixSets[tileMatrixSetId].tileMatrixSet
}), {})
};
}, {});
return {
...acc,
[url]: {
...sources?.[url],
tileMatrixSet: {
...sources?.[url]?.tileMatrixSet,
...tileMatrixSet
}
}
};
}, { ...sources });
};
/**
* Creates a map of `sourceId: sourceObject` from the layers array.
* @param {object[]} layers array of layer objects
*/
export const extractSourcesFromLayers = layers => {
/* layers grouped by url to create the source object */
const groupByUrl = layers
.filter(layer => layer.tileMatrixSet || layer.availableTileMatrixSets)
.reduce((acc, layer) => {
const sourceId = getSourceId(layer);
return {
...acc,
[sourceId]: acc[sourceId]
? [...acc[sourceId], layer]
: [layer]
};
}, {});
/* extract and add tile matrix sets to sources object */
return extractTileMatrixSetFromLayers(groupByUrl);
};
/**
* It extracts data from configuration sources and add them to the layers
*
* @param mapState {object} state of map, must contains layers array
* @return {object} new sources object with data from layers
*/
export const extractDataFromSources = mapState => {
if (!mapState || !mapState.layers || !isArray(mapState.layers)) {
return null;
}
const sources = mapState.mapInitialConfig && mapState.mapInitialConfig.sources && assign({}, mapState.mapInitialConfig.sources) || {};
return !isEmpty(sources) ? mapState.layers.map(l => {
const tileMatrix = extractTileMatrixFromSources(sources, l);
return assign({}, l, tileMatrix);
}) : [...mapState.layers];
};
export const getURLs = (urls, queryParametersString = '') => {
return urls.map((url) => url.split("\?")[0] + queryParametersString);
};
const LayerCustomUtils = {};
/**
* Return a base url for the given layer.
* Supports multiple urls.
*/
export const getLayerUrl = (layer) => {
return isArray(layer.url) ? layer.url[0] : layer.url;
};
export const getGroupByName = (groupName, groups = []) => {
const result = head(groups.filter(g => g.name === groupName));
return result || groups.reduce((prev, g) => prev || !!g.nodes && LayersUtils.getGroupByName(groupName, g.nodes), undefined);
};
export const getDimension = (dimensions, dimension) => {
switch (dimension.toLowerCase()) {
case 'elevation':
return getElevationDimension(dimensions);
default:
return null;
}
};
/**
* Returns an id for the layer. If the layer has layer.id returns it, otherwise it will return a generated id.
* If the layer doesn't have any layer and if the 2nd argument is passed (it should be an array),
* the layer id will returned will be something like `layerName__2` when 2 is the layer size (for retro compatibility, it should be removed in the future).
* Otherwise a random string will be appended to the layer name.
* @param {object} layer the layer
* @returns {string} the id of the layer, or a generated one
*/
export const getLayerId = (layerObj) => {
return layerObj && layerObj.id || `${layerObj.name ? `${layerObj.name}__` : ''}${uuidv1()}`;
};
/**
* it creates an id of a feature if not existing
* @param {object} feature list of layers to check
* @return {string} the id
*/
export const createFeatureId = (feature = {}) => {
return {
...feature,
id: feature.id || feature.properties?.id || uuidv1()
};
};
/**
* Normalizes the layer to assign missing Ids and features for vector layers
* @param {object} layer the layer to normalize
* @returns {object} the normalized layer
*/
export const normalizeLayer = (layer) => {
// con uuid
let _layer = layer;
if (layer.type === "vector") {
_layer = _layer?.features?.length ? {
..._layer,
features: _layer?.features?.map(createFeatureId)
} : layer;
}
// regenerate geodesic lines as property since that info has not been saved
if (_layer.id === ANNOTATIONS) {
_layer = updateAnnotationsLayer(_layer)[0];
}
return {
..._layer,
id: _layer.id || LayersUtils.getLayerId(_layer)};
};
/**
* Normalizes the map adding missing ids, default groups.
* @param {object} map the map
* @param {object} the normalized map
*/
export const normalizeMap = (rawMap = {}) =>
[
(map) => (map.layers || []).filter(({ id } = {}) => !id).length > 0 ? {...map, layers: (map.layers || []).map(l => LayersUtils.normalizeLayer(l))} : map,
(map) => map.groups ? map : {...map, groups: {id: DEFAULT_GROUP_ID, expanded: true}}
// this is basically a compose
].reduce((f, g) => (...args) => f(g(...args)))(rawMap);
/**
* @param gid
* @return function that filter by group
*/
export const belongsToGroup = (gid) => l => (l.group || DEFAULT_GROUP_ID) === gid || (l.group || "").indexOf(`${gid}.`) === 0;
export const getLayersByGroup = (configLayers, configGroups) => {
let i = 0;
let mapLayers = configLayers.map((layer) => assign({}, layer, {storeIndex: i++}));
let groupNames = mapLayers.reduce((groups, layer) => {
return groups.indexOf(layer.group || DEFAULT_GROUP_ID) === -1 ? groups.concat([layer.group || DEFAULT_GROUP_ID]) : groups;
}, []).filter((group) => group !== 'background').reverse();
return groupNames.reduce((groups, names)=> {
let name = names || DEFAULT_GROUP_ID;
name.split('.').reduce((subGroups, groupName, idx, array)=> {
const groupId = name.split(".", idx + 1).join('.');
let group = getGroup(groupId, subGroups);
const addLayers = idx === array.length - 1;
if (!group) {
const flattenGroups = flattenArrayOfObjects(configGroups);
const groupTitle = displayTitle(groupId, flattenGroups);
group = createGroup(groupId, groupTitle || groupName, groupName, mapLayers, addLayers);
subGroups.push(group);
} else if (addLayers) {
group.nodes = getLayersId(groupId, mapLayers).concat(group.nodes)
.reduce((arr, cur) => {
isObject(cur)
? arr.push({node: cur, order: mapLayers.find((el) => el.group === cur.id)?.storeIndex})
: arr.push({node: cur, order: mapLayers.find((el) => el.id === cur)?.storeIndex});
return arr;
}, []).sort((a, b) => b.order - a.order).map(e => e.node);
}
return group.nodes;
}, groups);
return groups;
}, []);
};
export const removeEmptyGroups = (groups) => {
return groups.reduce((acc, group) => {
return acc.concat(LayersUtils.getNotEmptyGroup(group));
}, []);
};
export const getNotEmptyGroup = (group) => {
const nodes = group.nodes.reduce((gNodes, node) => {
return node.nodes ? gNodes.concat(LayersUtils.getNotEmptyGroup(node)) : gNodes.concat(node);
}, []);
return nodes.length > 0 ? assign({}, group, {nodes: nodes}) : [];
};
export const reorderFunc = (groups, allLayers) => {
return allLayers.filter((layer) => layer.group === 'background')
.concat(reorderLayers(groups, allLayers));
};
export const getInactiveNode = (groupId, groups, nodeId) => {
const groupIds = groupId
.split('.')
.reverse()
.map((val, idx, arr) => [val, ...arr.filter((v, jdx) => jdx > idx)].reverse().join('.'));
const inactive = !!groups
.find((group) => nodeId !== group?.id && groupIds.includes(group?.id) && group.visibility === false);
return inactive;
};
export const getDerivedLayersVisibility = (layers = [], groups = []) => {
const flattenGroups = flattenArrayOfObjects(groups).filter(isObject);
return layers.map((layer) => {
const inactive = getInactiveNode(layer?.group || DEFAULT_GROUP_ID, flattenGroups);
return inactive ? { ...layer, visibility: false } : layer;
});
};
export const denormalizeGroups = (allLayers, groups) => {
const flattenGroups = flattenArrayOfObjects(groups).filter(isObject);
let getNormalizedGroup = (group, layers) => {
const nodes = group.nodes.map((node) => {
if (isObject(node)) {
return getNormalizedGroup(node, layers);
}
return layers.find((layer) => layer.id === node);
});
return {
...group,
nodes,
inactive: getInactiveNode(group?.id || '', flattenGroups, group?.id),
visibility: group?.visibility === undefined ? true : group.visibility
};
};
let normalizedLayers = allLayers.map((layer) => ({
...layer,
inactive: getInactiveNode(layer?.group || DEFAULT_GROUP_ID, flattenGroups),
expanded: layer.expanded || false
}));
return {
flat: normalizedLayers,
groups: groups.map((group) => getNormalizedGroup(group, normalizedLayers))
};
};
export const sortLayers = (groups, allLayers) => {
return allLayers.filter((layer) => layer.group === 'background')
.concat(reorderLayers(groups, allLayers));
};
export const toggleByType = (type, toggleFun) => {
return (node, status) => {
return toggleFun(node, type, status);
};
};
export const sortUsing = (sortFun, action) => {
return (node, reorder) => {
return action(node, reorder, sortFun);
};
};
export const splitMapAndLayers = (mapState) => {
if (mapState && isArray(mapState.layers)) {
let groups = LayersUtils.getLayersByGroup(mapState.layers, mapState.groups);
// additional params from saved configuration
if (isArray(mapState.groups)) {
groups = mapState.groups.reduce((g, group) => {
let newGroups = g;
let groupMetadata = {
expanded: group.expanded,
visibility: group.visibility,
nodesMutuallyExclusive: group.nodesMutuallyExclusive
};
if (group.title) {
groupMetadata = {
...groupMetadata,
title: group.title,
description: group.description,
tooltipOptions: group.tooltipOptions,
tooltipPlacement: group.tooltipPlacement
};
}
newGroups = LayersUtils.deepChange(newGroups, group.id, groupMetadata);
return newGroups;
}, [].concat(groups));
}
let layers = extractDataFromSources(mapState);
return assign({}, mapState, {
layers: {
flat: LayersUtils.reorder(groups, layers),
groups: groups
}
});
}
return mapState;
};
/**
* used for converting a geojson file with fileName into a vector layer
* it supports FeatureCollection or Feature
* @param {object} geoJSON object to put into features
* @param {string} id layer id
* @return {object} vector layer containing the geojson in features array
*/
export const geoJSONToLayer = (geoJSON, id) => {
const bbox = toBbox(geoJSON);
let features = [];
if (geoJSON.type === "FeatureCollection") {
features = geoJSON.features.map((feature, idx) => {
if (!feature.id) {
feature.id = idx;
}
if (feature.geometry && feature.geometry.bbox && isNaN(feature.geometry.bbox[0])) {
feature.geometry.bbox = [null, null, null, null];
}
return feature;
});
} else {
features = [pick({...geoJSON, id: isNil(geoJSON.id) ? uuidv1() : geoJSON.id}, ["geometry", "type", "style", "id"])];
}
return {
type: 'vector',
visibility: true,
id,
name: geoJSON.fileName,
hideLoading: true,
bbox: {
bounds: {
minx: bbox[0],
miny: bbox[1],
maxx: bbox[2],
maxy: bbox[3]
},
crs: "EPSG:4326"
},
features,
...(['geostyler'].includes(geoJSON?.style?.format) && geoJSON?.style?.body && {
style: geoJSON.style
})
};
};
export const saveLayer = (layer) => {
return assign({
id: layer.id,
features: layer.features,
format: layer.format,
thumbURL: layer.thumbURL && layer.thumbURL.split(':')[0] === 'blob' ? undefined : layer.thumbURL,
group: layer.group,
search: layer.search,
fields: layer.fields,
source: layer.source,
name: layer.name,
opacity: layer.opacity,
provider: layer.provider,
description: layer.description,
styles: layer.styles,
style: layer.style,
styleName: layer.styleName,
layerFilter: layer.layerFilter,
title: layer.title,
transparent: layer.transparent,
tiled: layer.tiled,
type: layer.type,
url: layer.url,
bbox: layer.bbox,
visibility: layer.visibility,
singleTile: layer.singleTile || false,
allowedSRS: layer.allowedSRS,
matrixIds: layer.matrixIds,
tileMatrixSet: layer.tileMatrixSet,
availableTileMatrixSets: layer.availableTileMatrixSets,
requestEncoding: layer.requestEncoding,
dimensions: layer.dimensions || [],
maxZoom: layer.maxZoom,
maxNativeZoom: layer.maxNativeZoom,
maxResolution: layer.maxResolution,
minResolution: layer.minResolution,
disableResolutionLimits: layer.disableResolutionLimits,
hideLoading: layer.hideLoading || false,
handleClickOnLayer: layer.handleClickOnLayer || false,
queryable: layer.queryable,
featureInfo: layer.featureInfo,
catalogURL: layer.catalogURL,
capabilitiesURL: layer.capabilitiesURL,
serverType: layer.serverType,
useForElevation: layer.useForElevation || false,
hidden: layer.hidden || false,
origin: layer.origin,
thematic: layer.thematic,
tooltipOptions: layer.tooltipOptions,
tooltipPlacement: layer.tooltipPlacement,
legendOptions: layer.legendOptions,
tileSize: layer.tileSize,
version: layer.version,
expanded: layer.expanded || false
},
layer?.enableInteractiveLegend !== undefined ? { enableInteractiveLegend: layer?.enableInteractiveLegend } : {},
layer.sources ? { sources: layer.sources } : {},
layer.heightOffset ? { heightOffset: layer.heightOffset } : {},
layer.params ? { params: layer.params } : {},
layer.extendedParams ? { extendedParams: layer.extendedParams } : {},
layer.localizedLayerStyles ? { localizedLayerStyles: layer.localizedLayerStyles } : {},
layer.options ? { options: layer.options } : {},
layer.credits ? { credits: layer.credits } : {},
layer.tileGrids ? { tileGrids: layer.tileGrids } : {},
layer.tileGridStrategy ? { tileGridStrategy: layer.tileGridStrategy } : {},
layer.tileGridCacheSupport ? { tileGridCacheSupport: layer.tileGridCacheSupport } : {},
isString(layer.rowViewer) ? { rowViewer: layer.rowViewer } : {},
!isNil(layer.forceProxy) ? { forceProxy: layer.forceProxy } : {},
!isNil(layer.disableFeaturesEditing) ? { disableFeaturesEditing: layer.disableFeaturesEditing } : {},
layer.pointCloudShading ? { pointCloudShading: layer.pointCloudShading } : {},
!isNil(layer.sourceMetadata) ? { sourceMetadata: layer.sourceMetadata } : {});
};
/**
* constants to specify whether we can use some geoserver vendor extensions of if they
* should rather be avoided
*/
export const ServerTypes = {
GEOSERVER: 'geoserver',
NO_VENDOR: 'no-vendor'
};
/**
* default initial constant regex rule for searching for a /geoserver/ string in a url
* useful for a reset to an initial state of the rule
*/
export const REG_GEOSERVER_RULE = regGeoServerRule;
/**
* Override default REG_GEOSERVER_RULE variable
* @param {regex} regex custom regex to override
*/
export const setRegGeoserverRule = (regex) => {
regGeoServerRule = regex;
};
/**
* Get REG_GEOSERVER_RULE regex variable
*/
export const getRegGeoserverRule = () => regGeoServerRule;
/**
* it tests if a url is matched by a regex,
* if so it returns the matched string
* otherwise returns null
* @param object.regex the regex to use for parsing the url
* @param object.url the url to test
*/
export const findGeoServerName = ({url, regexRule}) => {
const regex = regexRule || LayersUtils.getRegGeoserverRule();
const location = isArray(url) ? url[0] : url;
return regex.test(location) && location.match(regex)[0] || null;
};
/**
* This method search for a /geoserver/ string inside the url
* if it finds it returns a getCapabilitiesUrl to a single layer if it has a name like WORKSPACE:layerName
* otherwise it returns the default getCapabilitiesUrl
*/
export const getCapabilitiesUrl = (layer) => {
const matchedGeoServerName = LayersUtils.findGeoServerName({url: layer.url});
let reqUrl = getLayerUrl(layer);
if (!!matchedGeoServerName) {
let urlParts = reqUrl.split(matchedGeoServerName);
if (urlParts.length === 2) {
let layerParts = layer.name.split(":");
if (layerParts.length === 2) {
reqUrl = urlParts[0] + matchedGeoServerName + layerParts [0] + "/" + layerParts[1] + "/" + urlParts[1];
}
}
}
const params = {
...layer.baseParams,
...layer.params
};
return addBaseParams(reqUrl, params);
};
/**
* Gets the layer search url or the current url
*
* @memberof utils.LayerUtils
* @param {Object} layer
* @returns {string} layer url
*/
export const getSearchUrl = (l = {}) => l.search && l.search.url || l.url;
export const invalidateUnsupportedLayer = (layer, maptype) => {
return isSupportedLayerFunc(layer, maptype) ? checkInvalidParam(layer) : assign({}, layer, {invalid: true});
};
/**
* Establish if a layer is supported or not
* @return {boolean} value
*/
export const isSupportedLayer = (layer, maptype) => {
return !!isSupportedLayerFunc(layer, maptype);
};
export const getLayerTitleTranslations = (capabilities) => {
return !!LayerCustomUtils.getLayerTitleTranslations ? LayerCustomUtils.getLayerTitleTranslations(capabilities) : capabilities.Title;
};
export const setCustomUtils = (type, fun) => {
LayerCustomUtils[type] = fun;
};
export const getAuthenticationParam = options => {
const urls = getURLs(isArray(options.url) ? options.url : [options.url]);
let authenticationParam = {};
urls.forEach(url => {
addAuthenticationParameter(url, authenticationParam, options.securityToken);
});
return authenticationParam;
};
/**
* Removes google backgrounds and select an alternative one as visible
* returns a new list of layers modified accordingly
*/
export const excludeGoogleBackground = ll => {
const hasVisibleGoogleBackground = ll.filter(({ type, group, visibility } = {}) => group === 'background' && type === 'google' && visibility).length > 0;
const layers = ll.filter(({ type } = {}) => type !== 'google');
const backgrounds = layers.filter(({ group } = {}) => group === 'background');
// check if the selection of a new background is required
if (hasVisibleGoogleBackground && backgrounds.filter(({ visibility } = {}) => visibility).length === 0) {
// select the first available
if (backgrounds.length > 0) {
const candidate = findIndex(layers, {group: 'background'});
return layers.map((l, i) => i === candidate ? {...l, visibility: true} : l);
}
// add osm if any other background is missing
return [{
"type": "osm",
"title": "Open Street Map",
"name": "mapnik",
"source": "osm",
"group": "background",
"visibility": true
}, ...layers];
}
return layers;
};
export const creditsToAttribution = ({ imageUrl, link, title, text }) => {
// TODO: check if format is valid for an img (svg, for instance, may not work)
const html = imageUrl ? `<img src="${imageUrl}" ${title ? `title="${title}"` : ``}>` : title || text || "credits";
return link && html ? `<a href="${link}" target="_blank">${html}</a>` : html;
};
export const getLayerTitle = ({title, name}, currentLocale = 'default') => title?.[currentLocale] || title?.default || title || name;
/**
* Check if a resolution is inside of the min and max resolution limits of a layer
* @param {object} layer layer object
* @param {number} resolution resolutions of the current view
*/
export const isInsideResolutionsLimits = (layer, resolution) => {
if (layer.disableResolutionLimits) {
return true;
}
const minResolution = layer.minResolution || -Infinity;
const maxResolution = layer.maxResolution || Infinity;
return resolution !== undefined
? resolution < maxResolution && resolution >= minResolution
: true;
};
/**
* Filter array of layers to return layers with visibility key set to true
* @param {Array} layers
* @param {Array} timelineLayers
* @returns {Array}
*/
export const visibleTimelineLayers = (layers, timelineLayers) => {
return layers.filter(layer => {
let timelineLayer = timelineLayers?.find(item => item.id === layer.id);
return timelineLayer?.visibility ? layer : null;
});
};
/**
* Loop through array of timeline layers to determine if any of the layers is visible
* @param {Array} layers
* @returns {boolean}
*/
export const isTimelineVisible = (layers)=>{
for (let layer of layers) {
if (layer?.visibility) {
return true;
}
}
return false;
};
/**
* Remove the workspace prefix from a geoserver layer name
* @param {string} full layer name with workspace
* @returns {string} layer name without workspace prefix
*/
export const removeWorkspace = (layer) => {
if (layer.indexOf(':') !== -1) {
return layer.split(':')[1];
}
return layer;
};
/**
* Returns vendor params that can be used when calling wms server for display requests
* @param {layer} the layer object
*/
export const getWMSVendorParams = (layer) => {
if (layer?.serverType === ServerTypes.NO_VENDOR) {
return {};
}
return { TILED: layer.singleTile ? false : (!isNil(layer.tiled) ? layer.tiled : true)};
};
/**
* Utility function to check if the node allows to show fields tab
* @param {object} node the node of the TOC (including layer properties)
* @returns {boolean} true if the node allows to show fields
*/
export const hasWFSService = ({type, search = {}} = {}) =>
type === 'wfs' // pure WFS layer
|| (type === 'wms' && search.type === 'wfs'); // WMS backed by WFS (search)
export const getLayerTypeGlyph = (layer) => {
if (isAnnotationLayer(layer)) {
return 'comment';
}
return '1-layer';
};
/**
Removes a group even if it is nested
It works for layers too
**/
export const deepRemove = (nodes, findValue) => {
if (nodes && isArray(nodes) && nodes.length > 0) {
return nodes.filter((node) => (node.id && node.id !== findValue) || (isString(node) && node !== findValue )).map((node) => isObject(node) ? assign({}, node, node.nodes ? {
nodes: deepRemove(node.nodes, findValue)
} : {}) : node);
}
return nodes;
};
const updateGroupIds = (node, parentGroupId, newLayers) => {
if (node) {
if (isString(node.id)) {
const lastDot = node.id.lastIndexOf('.');
const newId = lastDot !== -1 ?
parentGroupId + node.id.slice(lastDot + (parentGroupId === '' ? 1 : 0)) :
parentGroupId + (parentGroupId === '' ? '' : '.') + node.id;
return assign({}, node, {id: newId, nodes: node.nodes.map(x => updateGroupIds(x, newId, newLayers))});
} else if (isString(node)) {
// if it's just a string it means it is a layer id
for (let layer of newLayers) {
if (layer.id === node) {
layer.group = parentGroupId;
}
}
return node;
}
}
return node;
};
export const sortGroups = (
{
groups: _groups,
layers: _layers
},
{
node: _node,
index: _index,
groupId: _groupId
}
) => {
const node = getNode(_groups || [], _node);
const layerNode = getNode(_layers, _node);
if (node && _index >= 0 && node.id !== ROOT_GROUP_ID && node.id !== DEFAULT_GROUP_ID && !(!!layerNode && _groupId === ROOT_GROUP_ID)) {
const groupId = _groupId || DEFAULT_GROUP_ID;
const curGroupId = layerNode ? (layerNode.group || DEFAULT_GROUP_ID) : (() => {
const groups = node.id.split('.');
return groups[groups.length - 2] || ROOT_GROUP_ID;
})();
if (groupId === curGroupId) {
const curGroupNode = curGroupId === ROOT_GROUP_ID ? {nodes: _groups} : getNode(_groups, curGroupId);
let nodes = (curGroupNode && curGroupNode.nodes || []).slice();