/
ImageryLayerCatalogItem.js
1590 lines (1426 loc) · 50.8 KB
/
ImageryLayerCatalogItem.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
"use strict";
/*global require*/
var CesiumMath = require("terriajs-cesium/Source/Core/Math").default;
var ClockRange = require("terriajs-cesium/Source/Core/ClockRange").default;
var clone = require("terriajs-cesium/Source/Core/clone").default;
var DataSourceClock = require("terriajs-cesium/Source/DataSources/DataSourceClock")
.default;
var defaultValue = require("terriajs-cesium/Source/Core/defaultValue").default;
var defined = require("terriajs-cesium/Source/Core/defined").default;
var DeveloperError = require("terriajs-cesium/Source/Core/DeveloperError")
.default;
var Ellipsoid = require("terriajs-cesium/Source/Core/Ellipsoid").default;
var formatError = require("terriajs-cesium/Source/Core/formatError").default;
var ImagerySplitDirection = require("terriajs-cesium/Source/Scene/ImagerySplitDirection")
.default;
var JulianDate = require("terriajs-cesium/Source/Core/JulianDate").default;
var knockout = require("terriajs-cesium/Source/ThirdParty/knockout").default;
var Rectangle = require("terriajs-cesium/Source/Core/Rectangle").default;
var Resource = require("terriajs-cesium/Source/Core/Resource").default;
var retry = require("retry");
var TimeInterval = require("terriajs-cesium/Source/Core/TimeInterval").default;
var TimeIntervalCollection = require("terriajs-cesium/Source/Core/TimeIntervalCollection")
.default;
var when = require("terriajs-cesium/Source/ThirdParty/when").default;
var CatalogItem = require("./CatalogItem");
var CompositeCatalogItem = require("./CompositeCatalogItem");
var getUrlForImageryTile = require("../Map/getUrlForImageryTile");
var inherit = require("../Core/inherit");
var TerriaError = require("../Core/TerriaError");
var overrideProperty = require("../Core/overrideProperty");
var { setOpacity, fixNextLayerOrder } = require("./ImageryLayerPreloadHelpers");
var setClockCurrentTime = require("./setClockCurrentTime");
var ChartData = require("../Charts/ChartData");
var VarType = require("../Map/VarType");
var TableColumn = require("../Map/TableColumn");
var MapInteractionMode = require("./MapInteractionMode");
var runLater = require("../Core/runLater");
var i18next = require("i18next").default;
/**
* A {@link CatalogItem} that is added to the map as a rasterized imagery layer.
*
* @alias ImageryLayerCatalogItem
* @constructor
* @extends CatalogItem
* @abstract
*
* @param {Terria} terria The Terria instance.
*/
var ImageryLayerCatalogItem = function(terria) {
CatalogItem.call(this, terria);
this._imageryLayer = undefined;
this._clock = undefined;
this._currentIntervalIndex = -1;
this._currentInterval = undefined;
this._nextIntervalIndex = undefined;
this._nextInterval = undefined;
this._nextLayer = undefined;
/**
* Gets or sets the opacity (alpha) of the data item, where 0.0 is fully transparent and 1.0 is
* fully opaque. This property is observable.
* @type {Number}
* @default 0.6
*/
this.opacity = 0.6;
/**
* Gets or sets a value indicating whether a 404 response code when requesting a tile should be
* treated as an error. If false, 404s are assumed to just be missing tiles and need not be
* reported to the user.
* @type {Boolean}
* @default false
*/
this.treat404AsError = false;
/**
* Gets or sets a value indicating whether a 403 response code when requesting a tile should be
* treated as an error. If false, 403s are assumed to just be missing tiles and need not be
* reported to the user.
* @type {Boolean}
* @default false
*/
this.treat403AsError = true;
/**
* Gets or sets a value indicating whether non-specific (no HTTP status code) tile errors should be ignored. This is a
* last resort, for dealing with odd cases such as data sources that return non-images (eg XML) with a 200 status code.
* No error messages will be shown to the user.
* @type {Boolean}
* @default false
*/
this.ignoreUnknownTileErrors = false;
/**
* Keeps the layer on top of all other imagery layers. This property is observable.
* @type {Boolean}
* @default false
*/
this.keepOnTop = false;
/**
* Gets or sets a value indicating whether this dataset should be clipped to the {@link CatalogItem#rectangle}.
* If true, no part of the dataset will be displayed outside the rectangle. This property is true by default,
* leading to better performance and avoiding tile request errors that might occur when requesting tiles outside the
* server-specified rectangle. However, it may also cause features to be cut off in some cases, such as if a server
* reports an extent that does not take into account that the representation of features sometimes require a larger
* spatial extent than the features themselves. For example, if a point feature on the edge of the extent is drawn
* as a circle with a radius of 5 pixels, half of that circle will be cut off.
* @type {Boolean}
* @default false
*/
this.clipToRectangle = true;
/**
* Gets or sets a value indicating whether tiles of this catalog item are required to be loaded before terrain
* tiles to which they're attached can be rendered. This should usually be set to true for base layers and
* false for all others.
* @type {Boolean}
* @default false
*/
this.isRequiredForRendering = false;
/**
* Options for the value of the animation timeline at start. Valid options in config file are:
* initialTimeSource: "present" // closest to today's date
* initialTimeSource: "start" // start of time range of animation
* initialTimeSource: "end" // end of time range of animation
* initialTimeSource: An ISO8601 date e.g. "2015-08-08" // specified date or nearest if date is outside range
* @type {String}
*/
this.initialTimeSource = undefined;
/**
* Gets or sets which side of the splitter (if present) to display this imagery layer on. Defaults to both sides.
* This property is observable.
* @type {ImagerySplitDirection}
*/
this.splitDirection = ImagerySplitDirection.NONE; // NONE means show on both sides of the splitter, if there is one.
/**
* Whether to show imagery availability on charts.
* @type {Boolean}
* @default false
*/
this.showOnChart = false;
/**
* Whether this layer is sufficient to warrant a chart.
* @type {Boolean}
* @default false
*/
this.dontChartAlone = false;
/**
* Gets or sets the color of how the dataset will displayed on the chart
* @type {String}
* @default '#44C99F'
*/
this.chartColor = "#44C99F";
/**
* Gets or sets the name style of how the item will be rendered on the chart
* @type {String}
* @default 'moment'
*/
this.chartType = "moment";
/**
* Gets or sets the name of a property in a feature returned from this layer's feature query service
* that indicates the times at which this layer covers this position. For example, historical and
* near-real-time satellite imagery often comes as daily swaths, with a given area on the globe
* potentially only covered every number of days.
* @type {String}
*/
this.featureTimesProperty = undefined;
this._allIntervals = undefined;
this.intervalFilterFeature = undefined;
this._intervalFilterCoordinates = undefined;
/**
* Gets or sets a flag indicating whether this source supports comparison of
* imagery from two different times.
*/
this.supportsDeltaComparison = false;
this.leafletUpdateInterval = 100;
/**
* Allow a threshold to be configured for the number of tile fails
* before disabling the layer.
* @default 5
* @type {Number}
*/
this.tileErrorThresholdBeforeDisabling = 5;
// Need to track initialTimeSource so we can set it in the specs after setting intervals, and then have the current time update (via the clock property).
knockout.track(this, [
"_clock",
"opacity",
"treat404AsError",
"ignoreUnknownTileErrors",
"intervals",
"clipToRectangle",
"splitDirection",
"initialTimeSource",
"showOnChart",
"dontChartAlone",
"featureTimesProperty",
"_allIntervals",
"intervalFilterFeature",
"_intervalFilterCoordinates"
]);
overrideProperty(this, "_filteredIntervals", {
get: function() {
if (!defined(this.intervalFilterFeature)) {
return undefined;
}
const featureTimesProperty = this.intervalFilterFeature.properties[
this.featureTimesProperty
];
if (!defined(featureTimesProperty)) {
return undefined;
}
const featureTimes = featureTimesProperty.getValue(this.currentTime);
return new TimeIntervalCollection(
featureTimes
.map(time => {
return this._allIntervals.findIntervalContainingDate(
JulianDate.fromIso8601(time)
);
})
.filter(i => i !== undefined)
);
}
});
/**
* Gets or sets the {@link TimeIntervalCollection} defining the intervals of distinct imagery. If this catalog item
* is not time-dynamic, property is undefined. This property is observable.
* @type {TimeIntervalCollection}
* @default undefined
* @member {TimeIntervalCollection} intervals
* @memberOf ImageryLayerCatalogItem.prototype
*/
overrideProperty(this, "intervals", {
get: function() {
if (defined(this._filteredIntervals)) {
return this._filteredIntervals;
}
return this._allIntervals;
},
set: function(value) {
this._allIntervals = value;
}
});
overrideProperty(this, "clock", {
get: function() {
var clock = this._clock;
if (!clock && this.intervals && this.intervals.length > 0) {
var startTime = this.intervals.start;
var stopTime = this.intervals.stop;
// Average about 5 seconds per interval.
var totalDuration = JulianDate.secondsDifference(stopTime, startTime);
var numIntervals = this.intervals.length;
var averageDuration = totalDuration / numIntervals;
var timePerSecond = averageDuration / 5;
clock = new DataSourceClock();
clock.startTime = startTime;
clock.stopTime = stopTime;
clock.multiplier = timePerSecond;
setClockCurrentTime(clock, this.initialTimeSource);
this._clock = clock;
}
return clock;
},
set: function(value) {
this._clock = value;
}
});
/**
* Gets javascript dates describing the discrete datetimes (or intervals) available for this item.
* By declaring this as a knockout defined property, it is cached.
* @member {Date[]} availableDates
* @memberOf ImageryLayerCatalogItem.prototype
*/
knockout.defineProperty(
this,
"availableDates",
{
get: function() {
const intervals = this.intervals;
if (defined(intervals)) {
// Only show the start of each interval. If only time instants were given, this is the instant.
const datetimes = [];
for (let i = 0; i < intervals.length; i++) {
datetimes.push(JulianDate.toDate(intervals.get(i).start));
}
return datetimes;
}
return [];
}
},
this
);
overrideProperty(this, "xAxis", {
get: function() {
if (this.showOnChart) {
return new TableColumn("Date", this.availableDates, {
type: VarType.TIME,
units: "Date"
});
} else {
return undefined;
}
}
});
knockout.getObservable(this, "opacity").subscribe(function() {
updateOpacity(this);
}, this);
knockout.getObservable(this, "splitDirection").subscribe(function() {
updateSplitDirection(this);
}, this);
};
inherit(CatalogItem, ImageryLayerCatalogItem);
Object.defineProperties(ImageryLayerCatalogItem.prototype, {
/**
* Gets a value indicating whether this {@link ImageryLayerCatalogItem} supports the {@link ImageryLayerCatalogItem#intervals}
* property for configuring time-dynamic imagery.
* @type {Boolean}
*/
supportsIntervals: {
get: function() {
return false;
}
},
/**
* Gets the Cesium or Leaflet imagery layer object associated with this data source.
* This property is undefined if the data source is not enabled.
* @memberOf ImageryLayerCatalogItem.prototype
* @type {Object}
*/
imageryLayer: {
get: function() {
return this._imageryLayer;
}
},
/**
* Gets a value indicating whether this data source, when enabled, can be reordered with respect to other data sources.
* Data sources that cannot be reordered are typically displayed above reorderable data sources.
* @memberOf ImageryLayerCatalogItem.prototype
* @type {Boolean}
*/
supportsReordering: {
get: function() {
return !this.keepOnTop;
}
},
/**
* Gets a value indicating whether the opacity of this data source can be changed.
* @memberOf ImageryLayerCatalogItem.prototype
* @type {Boolean}
*/
supportsOpacity: {
get: function() {
return true;
}
},
/**
* Gets a value indicating whether this layer can be split so that it is
* only shown on the left or right side of the screen.
* @memberOf ImageryLayerCatalogItem.prototype
*/
supportsSplitting: {
get: function() {
return true;
}
},
/**
* Gets the set of functions used to update individual properties in {@link CatalogMember#updateFromJson}.
* When a property name in the returned object literal matches the name of a property on this instance, the value
* will be called as a function and passed a reference to this instance, a reference to the source JSON object
* literal, and the name of the property.
* @memberOf ImageryLayerCatalogItem.prototype
* @type {Object}
*/
updaters: {
get: function() {
return ImageryLayerCatalogItem.defaultUpdaters;
}
},
/**
* Gets the set of functions used to serialize individual properties in {@link CatalogMember#serializeToJson}.
* When a property name on the model matches the name of a property in the serializers object literal,
* the value will be called as a function and passed a reference to the model, a reference to the destination
* JSON object literal, and the name of the property.
* @memberOf ImageryLayerCatalogItem.prototype
* @type {Object}
*/
serializers: {
get: function() {
return ImageryLayerCatalogItem.defaultSerializers;
}
},
/**
* Gets the set of names of the properties to be serialized for this object when {@link CatalogMember#serializeToJson} is called
* for a share link.
* @memberOf ImageryLayerCatalogItem.prototype
* @type {String[]}
*/
propertiesForSharing: {
get: function() {
return ImageryLayerCatalogItem.defaultPropertiesForSharing;
}
},
/**
* Gets a value indicating whether this catalog item can filter its
* {@link ImageryLayerCatalogItem#intervals} by feature.
* @memberOf ImageryLayerCatalogItem.prototype
* @type {Boolean}
*/
canFilterIntervalsByFeature: {
get: function() {
return defined(this.featureTimesProperty);
}
}
});
ImageryLayerCatalogItem.defaultUpdaters = clone(CatalogItem.defaultUpdaters);
ImageryLayerCatalogItem.defaultUpdaters.intervals = function(
catalogItem,
json,
propertyName
) {
if (!defined(json.intervals)) {
return;
}
if (!catalogItem.supportsIntervals) {
throw new TerriaError({
sender: catalogItem,
title: i18next.t("models.imageryLayer.intervalSupportErrorTitle"),
message: i18next.t("models.imageryLayer.intervalSupportErrorTitle", {
typeName: catalogItem.typeName,
type: catalogItem.type
})
});
}
var result = new TimeIntervalCollection();
for (var i = 0; i < json.intervals.length; ++i) {
var interval = json.intervals[i];
result.addInterval(
TimeInterval.fromIso8601({
iso8601: interval.interval,
data: interval.data
})
);
}
catalogItem.intervals = result;
};
ImageryLayerCatalogItem.defaultUpdaters.initialTimeSource = function(
catalogItem,
json,
propertyName
) {
catalogItem.initialTimeSource = json.initialTimeSource;
if (defined(catalogItem.clock)) {
setClockCurrentTime(catalogItem.clock, catalogItem.initialTimeSource);
}
};
ImageryLayerCatalogItem.defaultUpdaters.availableDates = function() {
// Do not update/serialize availableDates.
};
ImageryLayerCatalogItem.defaultUpdaters.xAxis = function() {
// Do not update/serialize xAxis.
};
ImageryLayerCatalogItem.defaultUpdaters.intervalFilterFeature = function(
catalogItem,
json,
propertyName
) {
if (defined(json.intervalFilterFeature)) {
let mapPromise = runLater(() => {});
if (catalogItem.terria.currentViewer === catalogItem.terria._noViewer) {
// We can't resolve the feature until we have a map.
const deferred = when.defer();
const removeViewerChangedListener = catalogItem.terria.afterViewerChanged.addEventListener(
() => {
if (
catalogItem.terria.currentViewer !== catalogItem.terria._noViewer
) {
removeViewerChangedListener();
deferred.resolve();
}
}
);
mapPromise = deferred.promise;
}
mapPromise.then(() => {
if (!catalogItem.isEnabled) {
// This will cause intervalFilterFeature to be ignored on a
// catalog item with isEnabled set to false
return;
}
return when(catalogItem.load()).then(() => {
const resolveFilterFeature = new MapInteractionMode({
message: i18next.t("models.imageryLayer.resolvingAvailability"),
onCancel: () => {
catalogItem.terria.mapInteractionModeStack.pop();
}
});
catalogItem.terria.mapInteractionModeStack.push(resolveFilterFeature);
catalogItem._intervalFilterCoordinates = {
level: json.intervalFilterFeature.level,
x: json.intervalFilterFeature.x,
y: json.intervalFilterFeature.y
};
catalogItem.terria.currentViewer.pickFromLocation(
{
lng: json.intervalFilterFeature.longitude,
lat: json.intervalFilterFeature.latitude,
height: json.intervalFilterFeature.height
},
{
[catalogItem._imageryLayer.imageryProvider.url]:
catalogItem._intervalFilterCoordinates
},
undefined,
true
);
catalogItem.terria.mapInteractionModeStack.pop();
const pickedFeatures = resolveFilterFeature.pickedFeatures;
return pickedFeatures.allFeaturesAvailablePromise.then(() => {
const thisLayerFeature = resolveFilterFeature.pickedFeatures.features.filter(
feature => {
return feature.imageryLayer === catalogItem.imageryLayer;
}
)[0];
if (thisLayerFeature !== undefined) {
try {
catalogItem.filterIntervalsByFeature(
thisLayerFeature,
pickedFeatures
);
} catch (e) {}
}
catalogItem.terria.mapInteractionModeStack.pop();
});
});
});
}
};
Object.freeze(ImageryLayerCatalogItem.defaultUpdaters);
ImageryLayerCatalogItem.defaultSerializers = clone(
CatalogItem.defaultSerializers
);
ImageryLayerCatalogItem.defaultSerializers.intervals = function(
catalogItem,
json,
propertyName
) {
if (defined(catalogItem.intervals)) {
var result = [];
for (var i = 0; i < catalogItem.intervals.length; ++i) {
var interval = catalogItem.intervals.get(i);
result.push({
interval: TimeInterval.toIso8601(interval),
data: interval.data
});
}
json.intervals = result;
}
};
// Do not serialize the original intialTimeSource - serialize the current time.
// That way if the item is shared, the desired time is used.
ImageryLayerCatalogItem.defaultSerializers.initialTimeSource = function(
catalogItem,
json,
propertyName
) {
if (defined(catalogItem.clock)) {
json.initialTimeSource = JulianDate.toIso8601(
catalogItem.clock.currentTime
);
} else {
json.initialTimeSource = catalogItem.initialTimeSource;
}
};
ImageryLayerCatalogItem.defaultSerializers.clock = function() {
// Do not serialize the clock when duplicating the item.
// Since this is not shared, it is not serialized for sharing anyway.
};
ImageryLayerCatalogItem.defaultSerializers.availableDates = function() {
// Do not update/serialize availableDates.
};
ImageryLayerCatalogItem.defaultSerializers.xAxis = function() {
// Do not update/serialize xAxis.
};
ImageryLayerCatalogItem.defaultSerializers.intervalFilterFeature = function(
catalogItem,
json,
propertyName
) {
if (
defined(catalogItem.intervalFilterFeature) &&
defined(catalogItem._intervalFilterCoordinates) &&
defined(catalogItem.intervalFilterFeature.position)
) {
const position = catalogItem.intervalFilterFeature.position.getValue(
this.currentTime
);
const cartographic = Ellipsoid.WGS84.cartesianToCartographic(position);
json.intervalFilterFeature = {
level: catalogItem._intervalFilterCoordinates.level,
x: catalogItem._intervalFilterCoordinates.x,
y: catalogItem._intervalFilterCoordinates.y,
longitude: CesiumMath.toDegrees(cartographic.longitude),
latitude: CesiumMath.toDegrees(cartographic.latitude),
height: cartographic.height
};
}
};
Object.freeze(ImageryLayerCatalogItem.defaultSerializers);
/**
* Gets or sets the default set of properties that are serialized when serializing a {@link CatalogItem}-derived object
* for a share link.
* @type {String[]}
*/
ImageryLayerCatalogItem.defaultPropertiesForSharing = clone(
CatalogItem.defaultPropertiesForSharing
);
ImageryLayerCatalogItem.defaultPropertiesForSharing.push("opacity");
ImageryLayerCatalogItem.defaultPropertiesForSharing.push("keepOnTop");
ImageryLayerCatalogItem.defaultPropertiesForSharing.push("initialTimeSource");
ImageryLayerCatalogItem.defaultPropertiesForSharing.push("splitDirection");
ImageryLayerCatalogItem.defaultPropertiesForSharing.push(
"intervalFilterFeature"
);
ImageryLayerCatalogItem.defaultPropertiesForSharing.push("showOnChart");
Object.freeze(ImageryLayerCatalogItem.defaultPropertiesForSharing);
/**
* Creates the {@link ImageryProvider} for this catalog item.
* @param {ImageryLayerTime} [time] The time for which to create an imagery provider. In layers that are not time-dynamic,
* this parameter is ignored.
* @return {ImageryProvider} The created imagery provider.
*/
ImageryLayerCatalogItem.prototype.createImageryProvider = function(time) {
var result = this._createImageryProvider(time);
result._leafletUpdateInterval = this.leafletUpdateInterval;
return result;
};
/**
* When implemented in a derived class, creates the {@link ImageryProvider} for this catalog item.
* @abstract
* @protected
* @param {ImageryLayerTime} [time] The time for which to create an imagery provider. In layers that are not time-dynamic,
* this parameter is ignored.
* @return {ImageryProvider} The created imagery provider.
*/
ImageryLayerCatalogItem.prototype._createImageryProvider = function(time) {
throw new DeveloperError(
"_createImageryProvider must be implemented in the derived class."
);
};
ImageryLayerCatalogItem.prototype._enable = function(layerIndex) {
if (defined(this._imageryLayer)) {
return;
}
var isTimeDynamic =
defined(this.intervals) && this.intervals.length > 0 && defined(this.clock);
var currentTimeIdentifier;
var nextTimeIdentifier;
if (isTimeDynamic) {
var index = this.intervals.indexOf(this.clock.currentTime);
// Here we use the terria clock because we want to optimize for the case where the item is playing on the
// timeline (which is linked to the terria clock) and preload the layer at the next time that the timeslider
// will move to.
const multiplier = this.terria.clock.multiplier;
var nextIndex;
if (index < 0) {
// No imagery matches the current time. ~index is the next available time.
this._currentIntervalIndex = -1;
this._currentInterval = undefined;
currentTimeIdentifier = undefined;
nextIndex = ~index + (multiplier < 0.0 ? -1 : 0);
} else {
// There is imagery for the current time
this._currentIntervalIndex = index;
this._currentInterval = this.intervals.get(index);
currentTimeIdentifier = this._currentInterval.data;
nextIndex = index + (multiplier < 0.0 ? -1 : 1);
}
// Ideally we should also check (this.terria.clock.clockRange === ClockRange.LOOP_STOP) here (to save preloading
// where it won't be used), but due to initaliseation order this.terria.clock.clockRange is not necessarily in
// the state that it will be when nextIndex is needed. So we make the assumption that this is the most likely
// case and optomise for this (since for the other cases UNBOUNDED / CLAMPED there is nothing to effectively preload).
if (nextIndex === this.intervals.length) {
nextIndex = 0;
}
if (nextIndex >= 0 && nextIndex < this.intervals.length) {
this._nextIntervalIndex = nextIndex;
this._nextInterval = this.intervals.get(nextIndex);
nextTimeIdentifier = this._nextInterval.data;
} else {
this._nextIntervalIndex = -1;
this._nextInterval = undefined;
}
this._currentTimeSubscription = knockout
.getObservable(this, "currentTime")
.subscribe(function() {
onClockTick(this);
}, this);
}
if (!isTimeDynamic || defined(currentTimeIdentifier)) {
var currentImageryProvider = this.createImageryProvider(
currentTimeIdentifier
);
this._imageryLayer = ImageryLayerCatalogItem.enableLayer(
this,
currentImageryProvider,
this.opacity,
layerIndex,
undefined,
currentTimeIdentifier
);
}
if (defined(nextTimeIdentifier)) {
var nextImageryProvider = this.createImageryProvider(nextTimeIdentifier);
// Do not allow picking from the preloading layer.
nextImageryProvider.enablePickFeatures = false;
this._nextLayer = ImageryLayerCatalogItem.enableLayer(
this,
nextImageryProvider,
0.0,
layerIndex + 1,
undefined,
nextTimeIdentifier
);
}
updateSplitDirection(this);
};
ImageryLayerCatalogItem.prototype._disable = function() {
if (defined(this._currentTimeSubscription)) {
this._currentTimeSubscription.dispose();
this._currentTimeSubscription = undefined;
}
ImageryLayerCatalogItem.disableLayer(this, this._imageryLayer);
this._imageryLayer = undefined;
ImageryLayerCatalogItem.disableLayer(this, this._nextLayer);
this._nextLayer = undefined;
};
ImageryLayerCatalogItem.prototype._show = function() {
// When the layer is not shown .showDataForTime() has no effect so if someone has updated the currentTime while the
// item was not shown update the layer now.
showDataForTime(this, this.currentTime);
ImageryLayerCatalogItem.showLayer(this, this._imageryLayer);
ImageryLayerCatalogItem.showLayer(this, this._nextLayer);
if (this.terria.catalog.chartableItems.indexOf(this) < 0) {
this.terria.catalog.addChartableItem(this);
}
};
ImageryLayerCatalogItem.prototype._hide = function() {
ImageryLayerCatalogItem.hideLayer(this, this._imageryLayer);
ImageryLayerCatalogItem.hideLayer(this, this._nextLayer);
this.terria.catalog.removeChartableItem(this);
};
ImageryLayerCatalogItem.prototype.showOnSeparateMap = function(globeOrMap) {
var imageryProvider = this._createImageryProvider();
var layer = ImageryLayerCatalogItem.enableLayer(
this,
imageryProvider,
this.opacity,
undefined,
globeOrMap,
undefined
);
globeOrMap.updateItemForSplitter(this); // equivalent to updateSplitDirection(catalogItem), but for any viewer (globeOrMap).
ImageryLayerCatalogItem.showLayer(this, layer, globeOrMap);
var that = this;
return function() {
ImageryLayerCatalogItem.hideLayer(that, layer, globeOrMap);
ImageryLayerCatalogItem.disableLayer(that, layer, globeOrMap);
};
};
ImageryLayerCatalogItem.prototype.showDeltaImagery = function(
firstImageDate,
secondImageDate
) {
this._disable();
const time = `${firstImageDate},${secondImageDate}`;
const imageryProvider = this._createImageryProvider(time);
this._imageryLayer = ImageryLayerCatalogItem.enableLayer(
this,
imageryProvider,
this.opacity,
undefined,
undefined,
time
);
ImageryLayerCatalogItem.showLayer(this, this._imageryLayer);
};
/**
* Refreshes this layer on the map. This is useful when, for example, parameters that went into
* {@link ImageryLayerCatalogItem#_createImageryProvider} change.
*/
ImageryLayerCatalogItem.prototype.refresh = function() {
if (!defined(this._imageryLayer)) {
return;
}
var currentIndex;
if (defined(this.terria.cesium)) {
var imageryLayers = this.terria.cesium.scene.imageryLayers;
currentIndex = imageryLayers.indexOf(this._imageryLayer);
}
this._hide();
this._disable();
if (this.isEnabled) {
this._enable(currentIndex);
if (this.isShown) {
this._show();
}
}
this.terria.currentViewer.notifyRepaintRequired();
};
function updateOpacity(item) {
if (defined(item._imageryLayer) && item.isEnabled && item.isShown) {
if (defined(item._imageryLayer.alpha)) {
item._imageryLayer.alpha = item.opacity;
}
if (defined(item._imageryLayer.setOpacity)) {
item._imageryLayer.setOpacity(item.opacity);
}
item.terria.currentViewer.notifyRepaintRequired();
}
}
function updateSplitDirection(item) {
item.terria.currentViewer.updateItemForSplitter(item);
}
ImageryLayerCatalogItem.enableLayer = function(
catalogItem,
imageryProvider,
opacity,
layerIndex,
globeOrMap,
time
) {
globeOrMap = defaultValue(globeOrMap, catalogItem.terria.currentViewer);
let tileFailures = 0;
let tileRetriesByMap = {};
const layer = globeOrMap.addImageryProvider({
imageryProvider: imageryProvider,
rectangle: catalogItem.rectangle,
clipToRectangle: catalogItem.clipToRectangle,
opacity: opacity,
layerIndex: layerIndex,
treat403AsError: catalogItem.treat403AsError,
treat404AsError: catalogItem.treat404AsError,
ignoreUnknownTileErrors: catalogItem.ignoreUnknownTileErrors,
isRequiredForRendering: catalogItem.isRequiredForRendering,
/*
Handling tile errors is really complicated because:
1) things go wrong for a variety of weird reasons including server misconfiguration, servers that are flakey but not totally broken, etc.
2) we want to fail as gracefully as possible, and give flakey servers every chance chance to shine
3) we don't generally have enough information the first time something fails.
There are several mechanisms in play here:
- Cesium's Resource automatically keeps trying to load any resource that fails until told to stop, but tells us each time.
- The "retry" library knows how to pace the retries, and when to actually stop.
*/
onLoadError: function(tileProviderError) {
if (!defined(layer) || !globeOrMap.isImageryLayerShown({ layer })) {
// If the layer is no longer shown, ignore errors and don't retry.
return undefined;
}
if (tileProviderError.timesRetried === 0) {
// There was an intervening success, so restart our count of the tile failures.
tileFailures = 0;
tileRetriesByMap = {};
}
tileProviderError.retry = undefined;
// deferred.reject = stop trying to load this tile
// deferred.resolve = retry loading this tile
const deferred = when.defer();
// operation has methods attempt, retry, stop.
const operation = retry.operation({
retries: 8,
minTimeout: 200,
randomize: true
});
let error = tileProviderError.error;
function fetchImage() {
// Attempt to fetch the image again.
const tileUrl = getUrlForImageryTile(
imageryProvider,
tileProviderError.x,
tileProviderError.y,
tileProviderError.level
);
if (tileUrl) {
return Resource.fetchImage({
url: tileUrl,
preferBlob: true
});
} else {