-
Notifications
You must be signed in to change notification settings - Fork 88
/
Permalink.js
1930 lines (1754 loc) · 62.9 KB
/
Permalink.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
// The MIT License (MIT)
//
// Copyright (c) 2016-2021 Camptocamp SA
//
// Permission is hereby granted, free of charge, to any person obtaining a copy of
// this software and associated documentation files (the "Software"), to deal in
// the Software without restriction, including without limitation the rights to
// use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
// the Software, and to permit persons to whom the Software is furnished to do so,
// subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all
// copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
// FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
// COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
// IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
// CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
import angular from 'angular';
import {PermalinkParam} from 'gmf/index.js';
import gmfAuthenticationService from 'gmf/authentication/Service.js';
import gmfLayerBeingSwipe from 'gmf/datasource/LayerBeingSwipe.js';
import gmfThemeManager, {ThemeEventType} from 'gmf/theme/Manager.js';
import gmfThemeThemes, {findThemeByName, findGroupByName} from 'gmf/theme/Themes.js';
import ngeoPopover from 'ngeo/Popover.js';
import ngeoDrawFeatures from 'ngeo/draw/features.js';
import gmfDataSourcesManager from 'gmf/datasource/Manager.js';
import ngeoDatasourceGroup from 'ngeo/datasource/Group.js';
import ngeoDatasourceOGC, {guessServiceTypeByUrl, Type} from 'ngeo/datasource/OGC.js';
import {Permalink3dParam} from 'ngeo/olcs/constants.js';
import ngeoFormatFeatureHash from 'ngeo/format/FeatureHash.js';
import ngeoFormatFeatureProperties from 'ngeo/format/FeatureProperties.js';
import {LAYER_NODE_NAME_KEY} from 'ngeo/map/LayerHelper.js';
import ngeoMiscDebounce from 'ngeo/misc/debounce.js';
import ngeoMiscEventHelper from 'ngeo/misc/EventHelper.js';
import ngeoStatemanagerModule from 'ngeo/statemanager/module.js';
import ngeoStatemanagerService from 'ngeo/statemanager/Service.js';
import ngeoLayertreeController, {LayertreeVisitorDecision} from 'ngeo/layertree/Controller.js';
import {getUid as olUtilGetUid} from 'ol/util.js';
import {listen, unlistenByKey} from 'ol/events.js';
import olFeature from 'ol/Feature.js';
import olGeomMultiPoint from 'ol/geom/MultiPoint.js';
import olGeomPoint from 'ol/geom/Point.js';
import olLayerGroup from 'ol/layer/Group.js';
import {CollectionEvent} from 'ol/Collection.js';
import {buildStyle} from 'ngeo/options.js';
/**
* @enum {string}
* @hidden
*/
export const OpenLayersLayerProperties = {
OPACITY: 'opacity',
};
/**
* External data source separators
* @enum {string}
* @hidden
*/
const ExtDSSeparator = {
LIST: ',',
NAMES: ';',
};
/**
* @enum {string}
* @hidden
*/
const ParamPrefix = {
DIMENSIONS: 'dim_',
TREE_ENABLE: 'tree_enable_',
TREE_GROUP_LAYERS: 'tree_group_layers_',
TREE_GROUP_OPACITY: 'tree_group_opacity_',
TREE_OPACITY: 'tree_opacity_',
TREE_TIME: 'tree_time_',
WFS: 'wfs_',
};
/**
* The Permalink service for GMF, which uses the `ngeo.statemanager.Service` to
* manage the GMF application state. Here's the list of states are are managed:
*
* - the map center and zoom level
* - the current background layer selected
* - whether to add a crosshair feature in the map or not
* - the dimensions value
*
* For time layers the time range or time value is added
*
* It can also be used to add different types of things in the map,
* such as features, external data sources, etc.
*
* This is made using parameters in the url, which can be static
* (i.e. parameters that always have the same name) or dynamic
* (i.e. parameters that have a static prefix but with a full name
* being dynamic).
*
* Here's a complete list of possible parameters and what they do. But
* first, a legend
*
* === Parameters [type] ===
*
* - `[paramameter_name]` (type)
* [parameter documentation]
*
*
* === Parameters (static) ===
*
* A static parameter always have the same name.
*
* - `baselayer_ref` - "string"
* The name of the base layer that should be set visible as
* default in the map. If none is set, then the default one in the
* loaded theme is set instead.
*
* - `eds_n` (string), `eds_u` (string)
* These parameters stand for:
* - eds_n: "External Data Sources Names"
* - eds_u: "External Data Sources URLs"
* These parameters define external WMS/WMTS data sources to add
* to the map upon initialization. Both values are comma-separated
* lists, `eds_u` containing the urls to the services and `eds_n`
* the layer names (separated by `;`). Here's an example:
* &eds_n=a;b;c,d,e&eds_u=host1.com,host2.com,host3.com, which reads as:
* - host1.com - layers: a, b and c
* - host2.com - layers: d
* - host3.com - layers: e
* For these parameters to work properly, they must define the
* same number of elements, i.e. same number of names and urls.
*
* - `map_crosshair` (boolean)
* If this parameter set to `true`, then a crosshair marker will be
* added to the center of the map upon initialization. The marker
* will stay at this location.
*
* - `map_tooltip` (string)
* If set, then the text defined in this parameter will be added
* as a tooltip at center of the map upon initialization.
*
* - `map_x` (number), `map_y` (number)
* These two parameters define a coordinate the map view should be
* centered to upon initialization. The value must be in the map
* view projection.
*
* - `map_zoom` (number)
* Defines the zoom level the map view should be zoomed to upon
* initialitation.
*
* - `rl_features` (string)
* This parameter defines vector features to add to the map upon
* initializaton. In addition, if the application includes the
* draw tool, the features added can be modified. The draw tool
* can also be used to add new features, which are automatically
* added in the url.
*
* - `tree_groups` (string)
* Defines the layer groups that should be added to the layer tree
* upon application initialization as a comma-separated list. For
* example: Layers,Filters,Externals
*
* - `wfs_layer` (string)
* If set, this parameter defines the name of a layer that
* supports WFS to use to fetch features and add them to the map
* upon initialization. The dynamic parameter `wfs_[]` is
* required to identify the features to fetch.
*
* - `wfs_ngroups` (number)
* If set, then `wfs_layer` represents the name of a group and
* this property defines how many layers are in it. Requires
* `wfs_layer` to be set.
*
* - `wfs_show_features` (boolean)
* If set to `false` or `0`, then the features returned by the
* `wfs_layer` parameter will not be shown in the map.
*
*
* === Parameters (dynamic) ===
*
* Dynamic parameters have variable names, which is always composed of:
* - a static prefix
* - a variable suffix
*
* The same dynamic parameter can be set multiple times, with
* different suffix values as name.
*
* For example: `&wfs_a=&wfs_b=`.
* - `wfs_` is the static prefix
* - the name used as reference for this dynamic parameter is `wfs_[]`
* - therefore, this example has 2 `wfs_[]` parameters set, with `a`
* and `b` being the variable suffix
*
*
* - `dim_[]` (string)
* Variable suffix: the name of a dimension
* Value: *
* Defines the value of a specific dimension to set upon loading
* the application. For example: `&dim_time=2019-01-25T14:45:51.986Z`.
* WMS data sources that support the dimension set will be
* initialized with its value.
*
* - `tree_enable_[]` (boolean)
* Variable suffix: the name of a layer group
* Value: whether the group should be enabled or not in the layer tree
* For example: `&tree_enable_polygon=true&tree_enable_point=false`
* means that the group `polygon` will be enabled in the layer
* tree, but not `point`.
*
* - `tree_group_layers_[]` (string)
* Variable suffix: the name of a layer group
* Value: a comma-separated list of layers within the group that
* should be enabled upon initialization.
* For example: `&tree_group_layers_polygon=forest,lake` means
* that only the layers `forest` and `lake` within the group
* `polygon` would be enabled upon initialization.
*
* - `tree_opacity_[]` (number)
* Variable suffix: the name of a layer group
* Value: Number between 0 (transparent) and 1 (opaque)
* Defines the opacity of a layer group upon initialization.
*
* - `tree_time_[]` (date)
* Variable suffix: the name of a layer group or layer
* Value: a date or date interval with the resolution of the time
* of the layer or group
* Defines the time or time interval for a time layer or a group.
*
* - `wfs_[]` (string)
* Variable suffix: the name of an attribute
* Value: A comma-separated list of values for the attribute
* This parameter requires `wfs_layer` in order to work
* properly. If set, it defines the filters to build to fetch the
* features to add to the map. For example:
* `&wfs_layer=fuel&wfs_osm_id=1420918679,441134960` the layer
* `fuel` will be fetched features with the attribute `osm_id`
* equal to `1420918679` or `441134960`.
* If `wfs_ngroups` is set, then an index is added to after the
* prefix of the `wfs_[]` parameter, for example:
* `wfs_ngroups=2&wfs_0_[]=&wfs_1_[]=`
*
*
* === More documentation ===
*
* To have the whole possibilities offer by the permalink, these services
* should be instantiated: ngeoBackgroundLayerMgr, ngeoFeatureOverlayMgr,
* ngeoFeatureHelper, gmfPermalinkOptions, gmfThemes, gmfObjectEditingManager,
* gmfThemeManager, defaultTheme, gmfTreeManager, ngeoWfsPermalink,
* ngeoAutoProjection and ngeoFeatures.
*
* Used functionalities:
*
* * `default_theme`: Theme to use by default.
*
* @constructor
* @param {angular.IQService} $q The Angular $q service.
* @param {angular.ITimeoutService} $timeout Angular timeout service.
* @param {angular.IScope} $rootScope Angular rootScope.
* @param {angular.auto.IInjectorService} $injector Main injector.
* @param {import('gmf/datasource/LayerBeingSwipe.js').LayerBeingSwipe} gmfLayerBeingSwipe
* @param {import("ngeo/misc/debounce.js").miscDebounce<function(): void>} ngeoDebounce ngeo Debounce factory.
* @param {angular.gettext.gettextCatalog} gettextCatalog Gettext service.
* @param {import("ngeo/misc/EventHelper.js").EventHelper} ngeoEventHelper Ngeo event helper service
* @param {import("ngeo/statemanager/Service.js").StatemanagerService} ngeoStateManager The ngeo statemanager
* service.
* @param {import("gmf/datasource/Manager.js").DatasourceManager} gmfDataSourcesManager The gmf datasourcemanager
* service.
* @param {import("ngeo/misc/WMSTime.js").WMSTime} ngeoWMSTime The ngeo wmstime service
* @param {import("ngeo/statemanager/Location.js").StatemanagerLocation} ngeoLocation ngeo location service.
* @param {import('gmf/options.js').gmfPermalinkOptions} gmfPermalinkOptions The options.
* @ngInject
* @ngdoc service
* @ngname gmfPermalink
*/
export function PermalinkService(
$q,
$timeout,
$rootScope,
$injector,
ngeoDebounce,
gettextCatalog,
ngeoEventHelper,
ngeoStateManager,
ngeoLocation,
gmfLayerBeingSwipe,
gmfPermalinkOptions,
gmfDataSourcesManager,
ngeoWMSTime
) {
/**
* @type {angular.IQService}
*/
this.q_ = $q;
/**
* @type {angular.IScope}
*/
this.rootScope_ = $rootScope;
/**
* @type {angular.ITimeoutService}
*/
this.$timeout_ = $timeout;
// == listener keys ==
/**
* The key for map view 'propertychange' event.
* @type {?import("ol/events.js").EventsKey}
*/
this.mapViewPropertyChangeEventKey_ = null;
// == properties from params ==
/**
* @type {import("ngeo/misc/debounce.js").miscDebounce<function(): void>}
*/
this.ngeoDebounce_ = ngeoDebounce;
/**
* @type {import("ngeo/misc/EventHelper.js").EventHelper}
*/
this.ngeoEventHelper_ = ngeoEventHelper;
/**
* @type {import("ngeo/statemanager/Service.js").StatemanagerService}
*/
this.ngeoStateManager_ = ngeoStateManager;
/**
* @type {import("gmf/datasource/Manager.js").DatasourceManager}
*/
this.gmfDataSourcesManager_ = gmfDataSourcesManager;
/**
* @type {import("ngeo/misc/WMSTime.js").WMSTime}
*/
this.ngeoWMSTime_ = ngeoWMSTime;
/**
* @type {?import("ol/Collection.js").default<olFeature<import("ol/geom/Geometry.js").default>>}
*/
this.ngeoFeatures_ = $injector.has('ngeoFeatures') ? $injector.get('ngeoFeatures') : null;
/**
* @type {?import("ngeo/map/BackgroundLayerMgr.js").MapBackgroundLayerManager}
*/
this.ngeoBackgroundLayerMgr_ = $injector.has('ngeoBackgroundLayerMgr')
? $injector.get('ngeoBackgroundLayerMgr')
: null;
/**
* @type {import('gmf/datasource/LayerBeingSwipe.js').LayerBeingSwipe}
*/
this.gmfLayerBeingSwipe_ = gmfLayerBeingSwipe;
/**
* @type {?import("ngeo/map/FeatureOverlayMgr.js").FeatureOverlayMgr}
*/
const ngeoFeatureOverlayMgr = $injector.has('ngeoFeatureOverlayMgr')
? $injector.get('ngeoFeatureOverlayMgr')
: null;
/**
* @type {?import("ngeo/map/FeatureOverlay.js").FeatureOverlay}
*/
this.featureOverlay_ = ngeoFeatureOverlayMgr ? ngeoFeatureOverlayMgr.getFeatureOverlay() : null;
/**
* @type {?import("ngeo/misc/FeatureHelper.js").FeatureHelper}
*/
this.featureHelper_ = $injector.has('ngeoFeatureHelper') ? $injector.get('ngeoFeatureHelper') : null;
/**
* @type {?import("ngeo/query/Querent.js").Querent}
*/
this.ngeoQuerent_ = $injector.has('ngeoQuerent') ? $injector.get('ngeoQuerent') : null;
if (gmfPermalinkOptions.useLocalStorage === true) {
// localStorage is deactivated by default
this.ngeoStateManager_.setUseLocalStorage(true);
}
/**
* @type {boolean}
*/
this.crosshairEnabledByDefault_ = !!gmfPermalinkOptions.crosshairEnabledByDefault;
/**
* @type {number|undefined}
*/
this.pointRecenterZoom_ = gmfPermalinkOptions.pointRecenterZoom;
/**
* @type {?import("gmf/datasource/ExternalDataSourcesManager.js").ExternalDatSourcesManager}
*/
this.gmfExternalDataSourcesManager_ = $injector.has('gmfExternalDataSourcesManager')
? $injector.get('gmfExternalDataSourcesManager')
: null;
/**
* @type {?import("gmf/theme/Themes.js").ThemesService}
*/
this.gmfThemes_ = $injector.has('gmfThemes') ? $injector.get('gmfThemes') : null;
/**
* @type {?import("gmf/objectediting/Manager.js").ObjecteditingManagerService}
*/
this.gmfObjectEditingManager_ = $injector.has('gmfObjectEditingManager')
? $injector.get('gmfObjectEditingManager')
: null;
/**
* @type {?import("gmf/theme/Manager.js").ThemeManagerService}
*/
this.gmfThemeManager_ = $injector.has('gmfThemeManager') ? $injector.get('gmfThemeManager') : null;
/**
* @type {string|undefined}
*/
this.defaultTheme_ = $injector.has('defaultTheme') ? $injector.get('defaultTheme') : undefined;
/**
* @type {?import("gmf/layertree/TreeManager.js").LayertreeTreeManager}
*/
this.gmfTreeManager_ = $injector.has('gmfTreeManager') ? $injector.get('gmfTreeManager') : null;
// == other properties ==
/**
* @type {import("ngeo/statemanager/Location.js").StatemanagerLocation}
*/
this.ngeoLocation_ = ngeoLocation;
/**
* @type {?import("ngeo/statemanager/WfsPermalink.js").WfsPermalinkService}
*/
this.ngeoWfsPermalink_ = $injector.has('ngeoWfsPermalink') ? $injector.get('ngeoWfsPermalink') : null;
/**
* @type {?import('gmf/authentication/Service.js').User}
*/
this.gmfUser_ = $injector.has('gmfUser') ? $injector.get('gmfUser') : null;
/**
* @type {?import("ol/Map.js").default}
*/
this.map_ = null;
/**
* @type {?import("ngeo/misc/AutoProjection.js").AutoProjectionService}
*/
this.ngeoAutoProjection_ = $injector.has('ngeoAutoProjection') ? $injector.get('ngeoAutoProjection') : null;
/**
* @type {import("ol/events.js").EventsKey[]}
*/
this.listenerKeys_ = [];
/**
* A list of projections that the coordinates in the permalink can be in.
* @type {?Array<import("ol/proj/Projection.js").default>}
*/
this.sourceProjections_ = null;
if (gmfPermalinkOptions.projectionCodes !== undefined && this.ngeoAutoProjection_) {
const projections = this.ngeoAutoProjection_.getProjectionList(gmfPermalinkOptions.projectionCodes);
if (projections.length > 0) {
this.sourceProjections_ = projections;
}
}
/**
* @type {?olFeature<import("ol/geom/Geometry.js").default>}
*/
this.crosshairFeature_ = null;
/**
* @type {import("ol/style/Style.js").StyleLike}
*/
this.crosshairStyle_ = buildStyle(gmfPermalinkOptions.crosshairStyle);
/**
* @type {?import("ngeo/Popover.js").default}
*/
this.mapTooltip_ = null;
/**
* @type {import("ngeo/format/FeatureHash.js").default}
*/
this.featureHashFormat_ = new ngeoFormatFeatureHash({
setStyle: false,
encodeStyles: false,
propertiesType: {
'fillColor': ngeoFormatFeatureProperties.COLOR,
'fillOpacity': ngeoFormatFeatureProperties.OPACITY,
'fontColor': ngeoFormatFeatureProperties.COLOR,
'fontSize': ngeoFormatFeatureProperties.SIZE,
'isBox': ngeoFormatFeatureProperties.IS_RECTANGLE,
'isCircle': ngeoFormatFeatureProperties.IS_CIRCLE,
'isLabel': ngeoFormatFeatureProperties.IS_TEXT,
'name': ngeoFormatFeatureProperties.NAME,
'pointRadius': ngeoFormatFeatureProperties.SIZE,
'showLabel': ngeoFormatFeatureProperties.SHOW_LABEL,
'showMeasure': ngeoFormatFeatureProperties.SHOW_MEASURE,
'strokeColor': ngeoFormatFeatureProperties.COLOR,
'strokeWidth': ngeoFormatFeatureProperties.STROKE,
},
defaultValues: {
'name': (feature) => {
const geometry = feature.getGeometry();
if (!geometry) {
throw new Error('Missing geometry');
}
return gettextCatalog.getString(geometry.getType());
},
'fillOpacity': () => 0.5,
'showLabel': () => false,
'showMeasure': () => false,
},
});
// == event listeners ==
if (this.ngeoBackgroundLayerMgr_) {
listen(this.ngeoBackgroundLayerMgr_, 'change', this.handleBackgroundLayerManagerChange_, this);
}
// visibility
this.rootScope_.$on('ngeo-layertree-state', (event, treeCtrl, firstParent) => {
/** @type {Object<string, string>} */
const newState = {};
if (firstParent.node.mixed) {
const state = treeCtrl.getState();
if (state !== 'on' && state !== 'off') {
throw new Error('Wrong state');
}
const visible = state === 'on';
treeCtrl.traverseDepthFirst(
/**
* @param {import('ngeo/layertree/Controller.js').LayertreeController} ctrl
*/
(ctrl) => {
const groupNode = /** @type {import('gmf/themes.js').GmfGroup} */ (ctrl.node);
if (groupNode.children === undefined) {
const param = ParamPrefix.TREE_ENABLE + ctrl.node.name;
newState[param] = `${visible}`;
}
}
);
} else {
/** @type {string[]} */
const gmfLayerNames = [];
firstParent.traverseDepthFirst(
/**
* @param {import('ngeo/layertree/Controller.js').LayertreeController} ctrl
*/
(ctrl) => {
const groupNode = /** @type {import('gmf/themes.js').GmfGroup} */ (ctrl.node);
if (groupNode.children === undefined && ctrl.getState() === 'on') {
gmfLayerNames.push(ctrl.node.name);
}
}
);
newState[`${ParamPrefix.TREE_GROUP_LAYERS}${firstParent.node.name}`] = gmfLayerNames.join(',');
}
this.ngeoStateManager_.updateState(newState);
});
this.rootScope_.$on('ngeo-layertree-opacity', (event, treeCtrl) => {
/** @type {Object<string, string>} */
const newState = {};
const opacity = treeCtrl.layer.getOpacity();
const stateName = `${
treeCtrl.parent.node.mixed ? ParamPrefix.TREE_OPACITY : ParamPrefix.TREE_GROUP_OPACITY
}${treeCtrl.node.name}`;
newState[stateName] = opacity;
this.ngeoStateManager_.updateState(newState);
});
// ngeoFeatures
// (1) read from features from the state manager first, add them
// (2) listen for further features added/removed
const features = this.getFeatures();
if (this.ngeoFeatures_) {
features.forEach((feature) => {
if (this.featureHelper_) {
this.featureHelper_.setStyle(feature);
}
this.addNgeoFeature_(feature);
});
this.ngeoFeatures_.extend(features);
listen(this.ngeoFeatures_, 'add', this.handleNgeoFeaturesAdd_, this);
listen(this.ngeoFeatures_, 'remove', this.handleNgeoFeaturesRemove_, this);
}
if (this.featureHelper_) {
this.rootScope_.$on('$localeChangeSuccess', () => {
features.forEach((feature) => {
if (!this.featureHelper_) {
throw new Error('Missing featureHelper');
}
this.featureHelper_.setStyle(feature);
});
});
}
if (this.gmfThemeManager_) {
this.rootScope_.$on(ThemeEventType.THEME_NAME_SET, (event, name) => {
this.setThemeInUrl_(name);
});
}
// Watch gmfLayerBeingSwipe
this.rootScope_.$watch(() => this.gmfLayerBeingSwipe_.layer, this.handleLayerBeingSwipeChange_.bind(this));
// Watch map swipe value.
this.rootScope_.$watch(() => this.gmfLayerBeingSwipe_.swipeValue, this.handleMapSwipeValue_.bind(this));
// External DataSources
/**
* @type {?angular.IPromise<void>}
*/
this.setExternalDataSourcesStatePromise_ = null;
if (this.ngeoQuerent_ && this.gmfExternalDataSourcesManager_) {
// First, load the external data sources that are defined in the url
this.initExternalDataSources_().then(() => {
if (!this.gmfExternalDataSourcesManager_) {
throw new Error('Missing gmfExternalDataSourcesManager');
}
// Then, listen to the changes made to the external data sources to
// update the url accordingly.
listen(
this.gmfExternalDataSourcesManager_.wmsGroupsCollection,
'add',
this.handleExternalDSGroupCollectionAdd_,
this
);
listen(
this.gmfExternalDataSourcesManager_.wmsGroupsCollection,
'remove',
this.handleExternalDSGroupCollectionRemove_,
this
);
listen(
this.gmfExternalDataSourcesManager_.wmtsGroupsCollection,
'add',
this.handleExternalDSGroupCollectionAdd_,
this
);
listen(
this.gmfExternalDataSourcesManager_.wmtsGroupsCollection,
'remove',
this.handleExternalDSGroupCollectionRemove_,
this
);
// We also need to 'register' the existing groups as well, i.e. those
// that were created by the Permalink
for (const wmsGroup of this.gmfExternalDataSourcesManager_.wmsGroups) {
this.registerExternalDSGroup_(wmsGroup);
}
for (const wmtsGroup of this.gmfExternalDataSourcesManager_.wmtsGroups) {
this.registerExternalDSGroup_(wmtsGroup);
}
});
}
this.initLayers_();
}
/**
* Called when layer being swipe
* @param {?import("ol/layer/Layer.js").default<import('ol/source/Source.js').default>|import("ol/layer/Group.js").default} layer layer object.
* @param {?import("ol/layer/Layer.js").default<import('ol/source/Source.js').default>|import("ol/layer/Group.js").default} oldLayer old layer object.
*/
PermalinkService.prototype.handleLayerBeingSwipeChange_ = function (layer, oldLayer) {
if (layer === oldLayer) {
return;
}
if (layer) {
/** @type {Object<string, string>} */
const object = {};
const mapSwipeValue = this.gmfLayerBeingSwipe_.swipeValue;
if (mapSwipeValue === null) {
this.gmfLayerBeingSwipe_.swipeValue = 0.5;
}
const dataSourceId = layer.get('dataSourceId');
object[PermalinkParam.MAP_SWIPE] = dataSourceId;
object[PermalinkParam.MAP_SWIPE_VALUE] = `${mapSwipeValue}`;
this.ngeoStateManager_.updateState(object);
} else {
this.ngeoStateManager_.deleteParam(PermalinkParam.MAP_SWIPE);
this.ngeoStateManager_.deleteParam(PermalinkParam.MAP_SWIPE_VALUE);
}
};
/**
* Called when map swipe value change.
*/
PermalinkService.prototype.handleMapSwipeValue_ = function () {
const mapSwipeValue = this.gmfLayerBeingSwipe_.swipeValue;
/** @type {Object<string, string>} */
const object = {};
if (mapSwipeValue) {
object[PermalinkParam.MAP_SWIPE_VALUE] = `${mapSwipeValue}`;
this.ngeoStateManager_.updateState(object);
}
};
// === Map X, Y, Z ===
/**
* Get the coordinate to use to initialize the map view from the state manager.
* @return {?import("ol/coordinate.js").Coordinate} The coordinate for the map view center.
*/
PermalinkService.prototype.getMapCenter = function () {
const x = this.ngeoStateManager_.getInitialNumberValue(PermalinkParam.MAP_X);
const y = this.ngeoStateManager_.getInitialNumberValue(PermalinkParam.MAP_Y);
if (x !== undefined && y !== undefined && !isNaN(x) && !isNaN(y)) {
const center = [x, y];
if (this.sourceProjections_ !== null && this.ngeoAutoProjection_) {
if (!this.map_) {
throw new Error('Missing map');
}
const targetProjection = this.map_.getView().getProjection();
const reprojectedCenter = this.ngeoAutoProjection_.tryProjectionsWithInversion(
center,
targetProjection.getExtent(),
targetProjection,
this.sourceProjections_
);
if (reprojectedCenter) {
return reprojectedCenter;
}
}
return center;
}
return null;
};
/**
* Get the zoom level to use to initialize the map view from the state manager.
* @return {number|undefined} The zoom for the map view.
*/
PermalinkService.prototype.getMapZoom = function () {
const zoom = this.ngeoStateManager_.getInitialNumberValue(PermalinkParam.MAP_Z);
return zoom !== undefined && isNaN(zoom) ? undefined : zoom;
};
// === Map crosshair ===
/**
* Get the map crosshair property from the state manager, if defined.
* @return {boolean} Whether map crosshair property is set or not.
*/
PermalinkService.prototype.getMapCrosshair = function () {
const crosshair = this.ngeoStateManager_.getInitialBooleanValue(PermalinkParam.MAP_CROSSHAIR);
return crosshair === undefined ? this.crosshairEnabledByDefault_ : crosshair;
};
/**
* Sets the map crosshair to the center (or the map center if nothing provided).
* Overwrites an existing map crosshair.
* @param {?import("ol/coordinate.js").Coordinate=} opt_center Optional center coordinate.
*/
PermalinkService.prototype.setMapCrosshair = function (opt_center) {
if (!this.map_) {
throw new Error('Missing map');
}
if (!this.featureOverlay_) {
throw new Error('Missing featureOverlay');
}
let crosshairCoordinate;
if (opt_center) {
crosshairCoordinate = opt_center;
} else {
crosshairCoordinate = this.map_.getView().getCenter();
}
if (!Array.isArray(crosshairCoordinate)) {
throw new Error('Wrong crosshairCoordinate');
}
// remove existing crosshair first
if (this.crosshairFeature_) {
this.featureOverlay_.removeFeature(this.crosshairFeature_);
}
// set new crosshair
this.crosshairFeature_ = new olFeature(new olGeomPoint(crosshairCoordinate));
this.crosshairFeature_.setStyle(this.crosshairStyle_);
// add to overlay
this.featureOverlay_.addFeature(this.crosshairFeature_);
};
// === Map tooltip ===
/**
* Get the tooltip text from the state manager.
* @return {string|undefined} Tooltip text.
*/
PermalinkService.prototype.getMapTooltip = function () {
return this.ngeoStateManager_.getInitialStringValue(PermalinkParam.MAP_TOOLTIP);
};
/**
* Sets the map tooltip to the center (or the map center if nothing provided).
* Overwrites an existing map tooltip.
* @param {string} tooltipText Text to display in tooltip.
* @param {?import("ol/coordinate.js").Coordinate=} opt_center Optional center coordinate.
*/
PermalinkService.prototype.setMapTooltip = function (tooltipText, opt_center) {
if (!this.map_) {
throw new Error('Missing map');
}
/** @type {import("ol/coordinate.js").Coordinate} */
let tooltipPosition;
if (opt_center) {
tooltipPosition = opt_center;
} else {
const center = this.map_.getView().getCenter();
if (!center) {
throw new Error('Missing center');
}
tooltipPosition = center;
}
if (!Array.isArray(tooltipPosition)) {
throw new Error('Wrong tooltipPosition type');
}
const div = $('<div/>', {
'class': 'gmf-permalink-tooltip',
'text': tooltipText,
})[0];
if (this.mapTooltip_ !== null) {
this.map_.removeOverlay(this.mapTooltip_);
}
this.mapTooltip_ = new ngeoPopover({
element: div,
position: tooltipPosition,
});
this.map_.addOverlay(this.mapTooltip_);
};
// === NgeoFeatures (A.K.A. DrawFeature, RedLining) ===
/**
* Get the ngeo features from the state manager for initialization purpose
* @return {olFeature<import("ol/geom/Geometry.js").default>[]} The features read from the state manager.
*/
PermalinkService.prototype.getFeatures = function () {
const f = this.ngeoStateManager_.getInitialStringValue(PermalinkParam.FEATURES);
if (f !== undefined && f !== '') {
return /** @type {olFeature<import("ol/geom/Geometry.js").default>[]} */ (this.featureHashFormat_.readFeatures(
f
));
}
return [];
};
/**
* @param {Object<string, string>} dimensions The global dimensions object.
*/
PermalinkService.prototype.setDimensions = function (dimensions) {
// apply initial state
const keys = this.ngeoLocation_.getParamKeysWithPrefix(ParamPrefix.DIMENSIONS);
for (const key of keys) {
const value = this.ngeoLocation_.getParam(key);
if (!value) {
throw new Error('Missing value');
}
dimensions[key.slice(ParamPrefix.DIMENSIONS.length)] = value;
}
this.rootScope_.$watchCollection(
() => dimensions,
(dimensions) => {
/** @type {Object<string, string>} */
const params = {};
for (const key in dimensions) {
params[ParamPrefix.DIMENSIONS + key] = dimensions[key];
}
this.ngeoLocation_.updateParams(params);
}
);
};
/**
* Bind an ol3 map object to this service. The service will, from there on,
* listen to the properties changed within the map view and update the following
* state properties: map_x, map_y and map_zoom.
*
* If the service is already bound to a map, those events are unlistened first.
*
* @param {?import("ol/Map.js").default} map The ol3 map object.
*/
PermalinkService.prototype.setMap = function (map) {
if (map === this.map_) {
return;
}
if (this.map_) {
this.unregisterMap_();
this.map_ = null;
}
if (map) {
this.map_ = map;
if (this.gmfObjectEditingManager_) {
this.gmfObjectEditingManager_.getFeature().then((feature) => {
this.registerMap_(map, feature);
});
} else {
this.registerMap_(map, null);
}
}
};
/**
* Listen to the map view property change and update the state accordingly.
* @param {import("ol/Map.js").default} map The ol3 map object.
* @param {?olFeature<import("ol/geom/Geometry.js").default>} oeFeature ObjectEditing feature
*/
PermalinkService.prototype.registerMap_ = function (map, oeFeature) {
const view = map.getView();
let center;
let zoom;
// (1) Initialize the map view with either:
// a) the given ObjectEditing feature
// b) the X, Y and Z available within the permalink service, if available
const geom = typeof oeFeature !== 'undefined' && oeFeature !== null ? oeFeature.getGeometry() : undefined;
if (geom) {
const size = map.getSize();
if (!size) {
throw new Error('Missing size');
}
let maxZoom;
if (geom instanceof olGeomPoint || geom instanceof olGeomMultiPoint) {
maxZoom = this.pointRecenterZoom_;
}
/** @type {import('ol/View.js').FitOptions} */
const options = {
size,
};
if (maxZoom) {
options.maxZoom = maxZoom;
}
view.fit(geom.getExtent(), options);
} else {
const enabled3d = this.ngeoStateManager_.getInitialBooleanValue(Permalink3dParam.ENABLED);
if (!enabled3d) {
center = this.getMapCenter();
if (center) {
view.setCenter(center);
}
zoom = this.getMapZoom();
if (zoom !== undefined) {
view.setZoom(zoom);
}
}
}
// (2) Listen to any property changes within the view and apply them to
// the permalink service
this.mapViewPropertyChangeEventKey_ = listen(
view,
'propertychange',
this.ngeoDebounce_(
/** @type {import("ol/events.js").ListenerFunction} */
() => {
const center = view.getCenter();
if (!center) {
throw new Error('Missing center');
}
const zoom = view.getZoom();
/** @type {Object<string, string>} */
const object = {};
object[PermalinkParam.MAP_X] = `${Math.round(center[0])}`;
object[PermalinkParam.MAP_Y] = `${Math.round(center[1])}`;
object[PermalinkParam.MAP_Z] = `${zoom}`;
this.ngeoStateManager_.updateState(object);
},
300,
/* invokeApply */ true