-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Expand file tree
/
Copy pathfloattileoverlay.js
More file actions
160 lines (139 loc) · 4.88 KB
/
floattileoverlay.js
File metadata and controls
160 lines (139 loc) · 4.88 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
goog.provide('ee.FloatTileOverlay');
goog.require('goog.array');
goog.require('goog.dom');
goog.require('goog.dom.TagName');
goog.require('goog.net.XmlHttp');
goog.require('goog.structs.Map');
goog.require('earthengine_api.javascript.abstractoverlay');
/**
* A google.maps.MapType implementation used to display Earth Engine tiles in
* Float32Array format.
* @export
* @ignore
* @deprecated Use ee.layers.BinaryOverlay instead.
*/
ee.FloatTileOverlay =
class extends earthengine_api.javascript.abstractoverlay
.AbstractOverlay {
/**
* @param {string} url The URL for fetching floating point tiles.
* @param {string} mapId The map ID for fetching floating point tiles.
* @param {string} token The temporary token for fetching tiles.
*/
constructor(url, mapId, token) {
super(url, mapId, token);
this.tileSize = new google.maps.Size(
ee.FloatTileOverlay.TILE_EDGE_LENGTH,
ee.FloatTileOverlay.TILE_EDGE_LENGTH);
/**
* The set of loaded floating point buffer tiles. The keys are the
* coordinates of the tiles, and the values are the corresponding
* Float32Arrays.
* @private {goog.structs.Map<!google.maps.Point, Float32Array>}
*/
this.floatTiles_ = new goog.structs.Map();
/**
* The floating point buffer tile DIV elements returned by getTile().
* The keys are the coordinates of the tiles, and the values are the
* corresponding DIV elements.
* @private {goog.structs.Map<!google.maps.Point, !Element>}
*/
this.floatTileDivs_ = new goog.structs.Map();
}
/** @override */
getTile(coord, zoom, ownerDocument) {
var tileId = this.getTileId(coord, zoom);
var src = [this.url, tileId].join('/') + '?token=' + this.token;
var uniqueTileId = [tileId, this.tileCounter, this.token].join('/');
this.tilesLoading.push(uniqueTileId);
this.tileCounter += 1;
var div = goog.dom.createDom(goog.dom.TagName.DIV);
var floatTile = this.loadFloatTile_(src, coord, uniqueTileId, div);
this.dispatchTileEvent_();
// The Maps API expects a div for the tile. We don't actually want to render
// the floating point tiles as a visible layer, so we return an empty div.
return div;
}
/**
* Requests a floating point tile from the provided URL.
* @param {string} tileUrl Tile URL
* @param {google.maps.Point} coord Coordinates of the floating tile
* @param {string} tileId Unique tile ID
* @param {!Element} div The corresponding DIV element.
* @private
*/
loadFloatTile_(tileUrl, coord, tileId, div) {
var tileRequest = goog.net.XmlHttp();
tileRequest.open('GET', tileUrl, true);
tileRequest.responseType = 'arraybuffer';
tileRequest.onreadystatechange = goog.bind(function() {
if (tileRequest.readyState === XMLHttpRequest.DONE &&
tileRequest.status === 200) {
var tileResponse = /** @type {Float32Array} */ (tileRequest.response);
if (tileResponse) {
var floatBuffer = new Float32Array(tileResponse);
this.handleFloatTileLoaded_(floatBuffer, coord, tileId, div);
} else {
this.tilesFailed.add(tileId);
throw new Error('Unable to request floating point array buffers.');
}
}
}, this);
tileRequest.send();
}
/**
* Handles float tile loaded events by storing the tile data and dispatching
* a tile event.
* @param {Float32Array} floatTile Successfully requested float tile
* @param {google.maps.Point} coord Coordinate of the floating tile
* @param {string} tileId Unique tile ID
* @param {!Element} div The corresponding DIV element.
* @private
*/
handleFloatTileLoaded_(floatTile, coord, tileId, div) {
this.floatTiles_.set(coord, floatTile);
this.floatTileDivs_.set(coord, div);
goog.array.remove(this.tilesLoading, tileId);
this.dispatchTileEvent_();
}
/**
* Returns the map of all visible floating tiles and the corresponding
* coordinates.
* @return {goog.structs.Map}
*/
getAllFloatTiles() {
return this.floatTiles_;
}
/**
* Returns the map of all the floating tile divs that are visible and
* the corresponding coordinates.
* @return {goog.structs.Map}
*/
getAllFloatTileDivs() {
return this.floatTileDivs_;
}
/** @return {number} The number of tiles successfully loaded. */
getLoadedFloatTilesCount() {
return this.floatTiles_.size;
}
/**
* Dispatches an event about a change in the number of outstanding tiles.
* @private
*/
dispatchTileEvent_() {
this.dispatchEvent(
new earthengine_api.javascript.abstractoverlay
.TileEvent(this.tilesLoading.length));
}
/** @override */
disposeInternal() {
this.floatTiles_ = null;
this.floatTileDivs_ = null;
super.disposeInternal();
}
};
/**
* The tile edge length of a float overlay tile.
* @const {number}
*/
ee.FloatTileOverlay.TILE_EDGE_LENGTH = 256;