-
Notifications
You must be signed in to change notification settings - Fork 427
Expand file tree
/
Copy pathTiledElevationCoverage.js
More file actions
674 lines (578 loc) · 28.8 KB
/
Copy pathTiledElevationCoverage.js
File metadata and controls
674 lines (578 loc) · 28.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
/*
* Copyright 2003-2006, 2009, 2017, 2020 United States Government, as represented
* by the Administrator of the National Aeronautics and Space Administration.
* All rights reserved.
*
* The NASAWorldWind/WebWorldWind platform is licensed under the Apache License,
* Version 2.0 (the "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License
* at http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software distributed
* under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
* CONDITIONS OF ANY KIND, either express or implied. See the License for the
* specific language governing permissions and limitations under the License.
*
* NASAWorldWind/WebWorldWind also contains the following 3rd party Open Source
* software:
*
* ES6-Promise – under MIT License
* libtess.js – SGI Free Software License B
* Proj4 – under MIT License
* JSZip – under MIT License
*
* A complete listing of 3rd Party software notices and licenses included in
* WebWorldWind can be found in the WebWorldWind 3rd-party notices and licenses
* PDF found in code directory.
*/
/**
* @exports TiledElevationCoverage
*/
define(['../util/AbsentResourceList',
'../geom/Angle',
'../error/ArgumentError',
'../globe/ElevationCoverage',
'../globe/ElevationImage',
'../formats/geotiff/GeoTiffReader',
'../util/LevelSet',
'../geom/Location',
'../util/Logger',
'../cache/MemoryCache',
'../geom/Sector',
'../util/Tile',
'../util/WWMath'],
function (AbsentResourceList,
Angle,
ArgumentError,
ElevationCoverage,
ElevationImage,
GeoTiffReader,
LevelSet,
Location,
Logger,
MemoryCache,
Sector,
Tile,
WWMath) {
"use strict";
/**
* Constructs a TiledElevationCoverage
* @alias TiledElevationCoverage
* @constructor
* @classdesc Represents the elevations for an area, often but not necessarily the whole globe.
* @param {{}} config Configuration properties for the coverage:
* <ul>
* <li>coverageSector: {Sector} The sector this coverage spans.</li>
* <li>resolution: {Number} The resolution of the coverage, in degrees. (To compute degrees from meters, divide the number of meters by the globe's radius to obtain radians and convert the result to degrees.)</li>
* <li>retrievalImageFormat: {String} The mime type of the elevation data retrieved by this coverage.</li>
* <li>minElevation (optional): {Number} The coverage's minimum elevation in meters.</li>
* <li>maxElevation (optional): {Number} Te coverage's maximum elevation in meters.</li>
* <li>urlBuilder (optional): {UrlBuilder} The factory to create URLs for elevation data requests.</li>
* <ul>
* @throws {ArgumentError} If any required configuration parameter is null or undefined.
*/
var TiledElevationCoverage = function (config) {
if (!config) {
throw new ArgumentError(
Logger.logMessage(Logger.LEVEL_SEVERE, "TiledElevationCoverage", "constructor", "missingConfig"));
}
if (!config.coverageSector) {
throw new ArgumentError(
Logger.logMessage(Logger.LEVEL_SEVERE, "TiledElevationCoverage", "constructor", "missingSector"));
}
if (!config.resolution) {
throw new ArgumentError(
Logger.logMessage(Logger.LEVEL_SEVERE, "TiledElevationCoverage", "constructor", "missingResolution"));
}
if (!config.retrievalImageFormat) {
throw new ArgumentError(
Logger.logMessage(Logger.LEVEL_SEVERE, "TiledElevationCoverage", "constructor", "missingImageFormat"));
}
ElevationCoverage.call(this, config.resolution);
var firstLevelDelta = 45,
tileWidth = 256,
lastLevel = LevelSet.numLevelsForResolution(firstLevelDelta / tileWidth, config.resolution),
numLevels = Math.ceil(lastLevel); // match or exceed the specified resolution
/**
* The sector this coverage spans.
* @type {Sector}
* @readonly
*/
this.coverageSector = config.coverageSector;
/**
* The mime type to use when retrieving elevations.
* @type {String}
* @readonly
*/
this.retrievalImageFormat = config.retrievalImageFormat;
/**
* This coverage's minimum elevation in meters.
* @type {Number}
* @default 0
*/
this.minElevation = config.minElevation || 0;
/**
* This coverage's maximum elevation in meters.
* @type {Number}
*/
this.maxElevation = config.maxElevation || 0;
/**
* Indicates whether the data associated with this coverage is point data. A value of false
* indicates that the data is area data (pixel is area).
* @type {Boolean}
* @default true
*/
this.pixelIsPoint = false;
/**
* The {@link LevelSet} dividing this coverage's geographic domain into a multi-resolution, hierarchical
* collection of tiles.
* @type {LevelSet}
* @readonly
*/
this.levels = new LevelSet(this.coverageSector, new Location(firstLevelDelta, firstLevelDelta),
numLevels, tileWidth, tileWidth);
/**
* Internal use only
* The list of assembled tiles.
* @type {Array}
* @ignore
*/
this.currentTiles = [];
/**
* Internal use only
* A scratch sector for use in computations.
* @type {Sector}
* @ignore
*/
this.currentSector = new Sector(0, 0, 0, 0);
/**
* Internal use only
* A cache of elevation tiles.
* @type {MemoryCache}
* @ignore
*/
this.tileCache = new MemoryCache(1000000, 800000);
/**
* Internal use only
* A cache of elevations.
* @type {MemoryCache}
* @ignore
*/
this.imageCache = new MemoryCache(10000000, 8000000);
/**
* Controls how many concurrent tile requests are allowed for this coverage.
* @type {Number}
* @default WorldWind.configuration.coverageRetrievalQueueSize
*/
this.retrievalQueueSize = WorldWind.configuration.coverageRetrievalQueueSize;
/**
* Internal use only
* The list of elevation retrievals in progress.
* @type {Array}
* @ignore
*/
this.currentRetrievals = [];
/**
* Internal use only
* The list of resources pending acquisition.
* @type {Array}
* @ignore
*/
this.absentResourceList = new AbsentResourceList(3, 5e3);
/**
* Internal use only
* The factory to create URLs for data requests. This property is typically set in the constructor of child
* classes. See {@link WcsUrlBuilder} for a concrete example.
* @type {UrlBuilder}
* @ignore
*/
this.urlBuilder = config.urlBuilder || null;
};
TiledElevationCoverage.prototype = Object.create(ElevationCoverage.prototype);
// Documented in super class
TiledElevationCoverage.prototype.minAndMaxElevationsForSector = function (sector, result) {
if (!sector) {
throw new ArgumentError(
Logger.logMessage(Logger.LEVEL_SEVERE, "TiledElevationCoverage", "minAndMaxElevationsForSector", "missingSector"));
}
if (!result) {
throw new ArgumentError(
Logger.logMessage(Logger.LEVEL_SEVERE, "TiledElevationCoverage", "minAndMaxElevationsForSector", "missingResult"));
}
var level = this.levels.levelForTexelSize(sector.deltaLatitude() * Angle.DEGREES_TO_RADIANS / 64);
this.assembleTiles(level, sector, false);
if (this.currentTiles.length === 0) {
return false; // Sector is outside the coverage's coverage area. Don't modify the result.
}
var hasCompleteCoverage = true;
for (var i = 0, len = this.currentTiles.length; i < len; i++) {
var image = this.imageCache.entryForKey(this.currentTiles[i].tileKey);
if (image && image.hasData) {
var imageMin = image.minElevation;
if (result[0] > imageMin) {
result[0] = imageMin;
}
var imageMax = image.maxElevation;
if (result[1] < imageMax) {
result[1] = imageMax;
}
}
if (!image || !image.hasData || image.hasMissingData) {
hasCompleteCoverage = false;
}
}
return hasCompleteCoverage;
};
// Documented in super class
TiledElevationCoverage.prototype.elevationAtLocation = function (latitude, longitude) {
if (!this.coverageSector.containsLocation(latitude, longitude)) {
return null; // location is outside the coverage's coverage
}
return this.pointElevationForLocation(latitude, longitude);
};
// Documented in super class
TiledElevationCoverage.prototype.elevationsForGrid = function (sector, numLat, numLon, result) {
if (!sector) {
throw new ArgumentError(
Logger.logMessage(Logger.LEVEL_SEVERE, "TiledElevationCoverage", "elevationsForGrid", "missingSector"));
}
if (!numLat || !numLon || numLat < 1 || numLon < 1) {
throw new ArgumentError(
Logger.logMessage(Logger.LEVEL_SEVERE, "TiledElevationCoverage", "elevationsForGrid",
"The specified number of latitudinal or longitudinal positions is less than one."));
}
var gridResolution = sector.deltaLatitude() / (numLat - 1) * Angle.DEGREES_TO_RADIANS;
var level = this.levels.levelForTexelSize(gridResolution);
if (this.pixelIsPoint) {
return this.pointElevationsForGrid(sector, numLat, numLon, level, result);
} else {
return this.areaElevationsForGrid(sector, numLat, numLon, level, result);
}
};
// Intentionally not documented.
TiledElevationCoverage.prototype.pointElevationForLocation = function (latitude, longitude) {
var level = this.levels.lastLevel(),
deltaLat = level.tileDelta.latitude,
deltaLon = level.tileDelta.longitude,
r = Tile.computeRow(deltaLat, latitude),
c = Tile.computeColumn(deltaLon, longitude),
tileKey,
image = null;
for (var i = level.levelNumber; i >= 0; i--) {
tileKey = Tile.computeTileKey(i, r, c);
image = this.imageCache.entryForKey(tileKey);
if (image) {
var elevation = image.elevationAtLocation(latitude, longitude);
return isNaN(elevation) ? null : elevation;
}
r = Math.floor(r / 2);
c = Math.floor(c / 2);
}
return null; // did not find a tile with an image
};
// Intentionally not documented.
TiledElevationCoverage.prototype.pointElevationsForGrid = function (sector, numLat, numLon, level, result) {
this.assembleTiles(level, sector, true);
if (this.currentTiles.length === 0) {
return false; // Sector is outside the coverage's coverage area. Do not modify the results array.
}
// Sort from lowest resolution to highest so that higher resolutions override lower resolutions in the
// loop below.
this.currentTiles.sort(function (tileA, tileB) {
return tileA.level.levelNumber - tileB.level.levelNumber;
});
for (var i = 0, len = this.currentTiles.length; i < len; i++) {
var image = this.imageCache.entryForKey(this.currentTiles[i].tileKey);
if (image) {
image.elevationsForGrid(sector, numLat, numLon, result);
}
}
return !result.includes(NaN); // true if the result array is fully populated.
};
// Internal. Returns elevations for a grid assuming pixel-is-area.
TiledElevationCoverage.prototype.areaElevationsForGrid = function (sector, numLat, numLon, level, result) {
var minLat = sector.minLatitude,
maxLat = sector.maxLatitude,
minLon = sector.minLongitude,
maxLon = sector.maxLongitude,
deltaLat = sector.deltaLatitude() / (numLat > 1 ? numLat - 1 : 1),
deltaLon = sector.deltaLongitude() / (numLon > 1 ? numLon - 1 : 1),
lat, lon, s, t,
latIndex, lonIndex, resultIndex = 0;
for (latIndex = 0, lat = minLat; latIndex < numLat; latIndex += 1, lat += deltaLat) {
if (latIndex === numLat - 1) {
lat = maxLat; // explicitly set the last lat to the max latitude ensure alignment
}
for (lonIndex = 0, lon = minLon; lonIndex < numLon; lonIndex += 1, lon += deltaLon) {
if (lonIndex === numLon - 1) {
lon = maxLon; // explicitly set the last lon to the max longitude ensure alignment
}
if (isNaN(result[resultIndex])) {
if (this.coverageSector.containsLocation(lat, lon)) { // ignore locations outside of the model
s = (lon + 180) / 360;
t = (lat + 90) / 180;
this.areaElevationForCoord(s, t, level.levelNumber, result, resultIndex);
}
}
resultIndex++;
}
}
return !result.includes(NaN); // true if the result array is fully populated.
};
// Internal. Returns an elevation for a location assuming pixel-is-area.
TiledElevationCoverage.prototype.areaElevationForCoord = function (s, t, levelNumber, result, resultIndex) {
var level, levelWidth, levelHeight,
tMin, tMax,
vMin, vMax,
u, v,
x0, x1, y0, y1,
xf, yf,
retrieveTiles,
pixels = new Float64Array(4);
for (var i = levelNumber; i >= 0; i--) {
level = this.levels.level(i);
levelWidth = Math.round(level.tileWidth * 360 / level.tileDelta.longitude);
levelHeight = Math.round(level.tileHeight * 180 / level.tileDelta.latitude);
tMin = 1 / (2 * levelHeight);
tMax = 1 - tMin;
vMin = 0;
vMax = levelHeight - 1;
u = levelWidth * WWMath.fract(s); // wrap the horizontal coordinate
v = levelHeight * WWMath.clamp(t, tMin, tMax); // clamp the vertical coordinate to the level edge
x0 = WWMath.mod(Math.floor(u - 0.5), levelWidth);
x1 = WWMath.mod((x0 + 1), levelWidth);
y0 = WWMath.clamp(Math.floor(v - 0.5), vMin, vMax);
y1 = WWMath.clamp(y0 + 1, vMin, vMax);
xf = WWMath.fract(u - 0.5);
yf = WWMath.fract(v - 0.5);
retrieveTiles = (i == levelNumber) || (i == 0);
if (this.lookupPixels(x0, x1, y0, y1, level, retrieveTiles, pixels)) {
if (ElevationImage.isNoData(pixels[0], pixels[1], pixels[2], pixels[3])) {
return false;
}
else {
result[resultIndex] = (1 - xf) * (1 - yf) * pixels[0] +
xf * (1 - yf) * pixels[1] +
(1 - xf) * yf * pixels[2] +
xf * yf * pixels[3];
return true;
}
}
}
return false;
};
// Internal. Bilinearly interpolates tile-image elevations.
TiledElevationCoverage.prototype.lookupPixels = function (x0, x1, y0, y1, level, retrieveTiles, result) {
var levelNumber = level.levelNumber,
tileWidth = level.tileWidth,
tileHeight = level.tileHeight,
row0 = Math.floor(y0 / tileHeight),
row1 = Math.floor(y1 / tileHeight),
col0 = Math.floor(x0 / tileWidth),
col1 = Math.floor(x1 / tileWidth),
r0c0, r0c1, r1c0, r1c1;
if (row0 == row1 && row0 == this.cachedRow && col0 == col1 && col0 == this.cachedCol) {
r0c0 = r0c1 = r1c0 = r1c1 = this.cachedImage; // use results from previous lookup
} else if (row0 == row1 && col0 == col1) {
r0c0 = this.lookupImage(levelNumber, row0, col0, retrieveTiles); // only need to lookup one image
r0c1 = r1c0 = r1c1 = r0c0; // re-use the single image
this.cachedRow = row0;
this.cachedCol = col0;
this.cachedImage = r0c0; // note the results for subsequent lookups
} else {
r0c0 = this.lookupImage(levelNumber, row0, col0, retrieveTiles);
r0c1 = this.lookupImage(levelNumber, row0, col1, retrieveTiles);
r1c0 = this.lookupImage(levelNumber, row1, col0, retrieveTiles);
r1c1 = this.lookupImage(levelNumber, row1, col1, retrieveTiles);
}
if (r0c0 && r0c1 && r1c0 && r1c1) {
result[0] = r0c0.pixel(x0 % tileWidth, y0 % tileHeight);
result[1] = r0c1.pixel(x1 % tileWidth, y0 % tileHeight);
result[2] = r1c0.pixel(x0 % tileWidth, y1 % tileHeight);
result[3] = r1c1.pixel(x1 % tileWidth, y1 % tileHeight);
return true;
}
return false;
};
// Internal. Intentionally not documented.
TiledElevationCoverage.prototype.lookupImage = function (levelNumber, row, column, retrieveTiles) {
var tileKey = Tile.computeTileKey(levelNumber, row, column),
image = this.imageCache.entryForKey(tileKey);
if (image == null && retrieveTiles) {
var tile = this.tileForLevel(levelNumber, row, column);
this.retrieveTileImage(tile);
}
return image;
};
// Intentionally not documented.
TiledElevationCoverage.prototype.assembleTiles = function (level, sector, retrieveTiles) {
this.currentTiles = [];
// Intersect the requested sector with the coverage's coverage area. This avoids attempting to assemble tiles
// that are outside the coverage area.
this.currentSector.copy(sector);
this.currentSector.intersection(this.coverageSector);
if (this.currentSector.isEmpty())
return; // sector is outside the coverage's coverage area
var deltaLat = level.tileDelta.latitude,
deltaLon = level.tileDelta.longitude,
firstRow = Tile.computeRow(deltaLat, this.currentSector.minLatitude),
lastRow = Tile.computeLastRow(deltaLat, this.currentSector.maxLatitude),
firstCol = Tile.computeColumn(deltaLon, this.currentSector.minLongitude),
lastCol = Tile.computeLastColumn(deltaLon, this.currentSector.maxLongitude);
for (var row = firstRow; row <= lastRow; row++) {
for (var col = firstCol; col <= lastCol; col++) {
this.addTileOrAncestor(level, row, col, retrieveTiles);
}
}
};
// Intentionally not documented.
TiledElevationCoverage.prototype.addTileOrAncestor = function (level, row, column, retrieveTiles) {
var tile = this.tileForLevel(level.levelNumber, row, column);
if (this.isTileImageInMemory(tile)) {
this.addToCurrentTiles(tile);
} else {
if (retrieveTiles) {
this.retrieveTileImage(tile);
}
if (level.isFirstLevel()) {
this.currentTiles.push(tile); // no ancestor tile to add
} else {
this.addAncestor(level, row, column, retrieveTiles);
}
}
};
// Intentionally not documented.
TiledElevationCoverage.prototype.addAncestor = function (level, row, column, retrieveTiles) {
var tile = null,
r = Math.floor(row / 2),
c = Math.floor(column / 2);
for (var i = level.levelNumber - 1; i >= 0; i--) {
tile = this.tileForLevel(i, r, c);
if (this.isTileImageInMemory(tile)) {
this.addToCurrentTiles(tile);
return;
}
r = Math.floor(r / 2);
c = Math.floor(c / 2);
}
// No ancestor tiles have an in-memory image. Retrieve the ancestor tile corresponding for the first level, and
// add it. We add the necessary tiles to provide coverage over the requested sector in order to accurately return
// whether or not this coverage has data for the entire sector.
this.addToCurrentTiles(tile);
if (retrieveTiles) {
this.retrieveTileImage(tile);
}
};
// Intentionally not documented.
TiledElevationCoverage.prototype.addToCurrentTiles = function (tile) {
this.currentTiles.push(tile);
};
// Intentionally not documented.
TiledElevationCoverage.prototype.tileForLevel = function (levelNumber, row, column) {
var tileKey = Tile.computeTileKey(levelNumber, row, column),
tile = this.tileCache.entryForKey(tileKey);
if (tile) {
return tile;
}
var level = this.levels.level(levelNumber),
sector = Tile.computeSector(level, row, column);
tile = new Tile(sector, level, row, column);
this.tileCache.putEntry(tileKey, tile, tile.size());
return tile;
};
// Intentionally not documented.
TiledElevationCoverage.prototype.isTileImageInMemory = function (tile) {
return this.imageCache.containsKey(tile.tileKey);
};
// Intentionally not documented.
TiledElevationCoverage.prototype.resourceUrlForTile = function (tile) {
return this.urlBuilder.urlForTile(tile, this.retrievalImageFormat);
};
// Intentionally not documented.
TiledElevationCoverage.prototype.retrieveTileImage = function (tile) {
if (this.currentRetrievals.length < this.retrievalQueueSize
&& this.currentRetrievals.indexOf(tile.tileKey) < 0
&& !this.absentResourceList.isResourceAbsent(tile.tileKey)) {
var url = this.resourceUrlForTile(tile, this.retrievalImageFormat),
xhr = new XMLHttpRequest(),
elevationCoverage = this;
if (!url) return;
xhr.open("GET", url, true);
xhr.responseType = 'arraybuffer';
xhr.onreadystatechange = function () {
if (xhr.readyState === 4) {
elevationCoverage.removeFromCurrentRetrievals(tile.tileKey);
var contentType = xhr.getResponseHeader("content-type");
if (xhr.status === 200) {
if (contentType === elevationCoverage.retrievalImageFormat
|| contentType === "text/plain"
|| contentType === "application/octet-stream") {
Logger.log(Logger.LEVEL_INFO, "Elevations retrieval succeeded: " + url);
elevationCoverage.loadElevationImage(tile, xhr);
elevationCoverage.absentResourceList.unmarkResourceAbsent(tile.tileKey);
// Send an event to request a redraw.
var e = document.createEvent('Event');
e.initEvent(WorldWind.REDRAW_EVENT_TYPE, true, true);
window.dispatchEvent(e);
} else if (contentType === "text/xml") {
elevationCoverage.absentResourceList.markResourceAbsent(tile.tileKey);
Logger.log(Logger.LEVEL_WARNING,
"Elevations retrieval failed (" + xhr.statusText + "): " + url + ".\n "
+ String.fromCharCode.apply(null, new Uint8Array(xhr.response)));
} else {
elevationCoverage.absentResourceList.markResourceAbsent(tile.tileKey);
Logger.log(Logger.LEVEL_WARNING,
"Elevations retrieval failed: " + url + ". " + "Unexpected content type "
+ contentType);
}
} else {
elevationCoverage.absentResourceList.markResourceAbsent(tile.tileKey);
Logger.log(Logger.LEVEL_WARNING,
"Elevations retrieval failed (" + xhr.statusText + "): " + url);
}
}
};
xhr.onerror = function () {
elevationCoverage.removeFromCurrentRetrievals(tile.tileKey);
elevationCoverage.absentResourceList.markResourceAbsent(tile.tileKey);
Logger.log(Logger.LEVEL_WARNING, "Elevations retrieval failed: " + url);
};
xhr.ontimeout = function () {
elevationCoverage.removeFromCurrentRetrievals(tile.tileKey);
elevationCoverage.absentResourceList.markResourceAbsent(tile.tileKey);
Logger.log(Logger.LEVEL_WARNING, "Elevations retrieval timed out: " + url);
};
xhr.send(null);
this.currentRetrievals.push(tile.tileKey);
}
};
// Intentionally not documented
TiledElevationCoverage.prototype.removeFromCurrentRetrievals = function (tileKey) {
var index = this.currentRetrievals.indexOf(tileKey);
if (index > -1) {
this.currentRetrievals.splice(index, 1);
}
};
// Intentionally not documented.
TiledElevationCoverage.prototype.loadElevationImage = function (tile, xhr) {
var elevationImage = new ElevationImage(tile.sector, tile.tileWidth, tile.tileHeight),
geoTiff;
if (this.retrievalImageFormat === "application/bil16") {
elevationImage.imageData = new Int16Array(xhr.response);
elevationImage.size = elevationImage.imageData.length * 2;
} else if (this.retrievalImageFormat === "application/bil32") {
elevationImage.imageData = new Float32Array(xhr.response);
elevationImage.size = elevationImage.imageData.length * 4;
} else if (this.retrievalImageFormat === "image/tiff") {
geoTiff = new GeoTiffReader(xhr.response);
elevationImage.imageData = geoTiff.getImageData();
elevationImage.size = elevationImage.imageData.length * geoTiff.metadata.bitsPerSample[0] / 8;
}
if (elevationImage.imageData) {
elevationImage.findMinAndMaxElevation();
this.imageCache.putEntry(tile.tileKey, elevationImage, elevationImage.size);
this.timestamp = Date.now();
}
};
return TiledElevationCoverage;
});