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 | 1×
1×
1×
1×
1×
1×
1×
1×
1×
1×
1×
1×
56×
56×
56×
56×
56×
56×
56×
1×
1×
2×
2×
2×
2×
2×
1×
1×
71×
71×
71×
20×
51×
51×
24×
51×
1×
18×
18×
18×
18×
18×
3×
18×
1×
30×
20×
10×
10×
10×
6×
4×
4×
4×
4×
6×
4×
4×
1×
26×
26×
26×
26×
26×
26×
26×
26×
26×
26×
1×
1×
2×
2×
2×
2×
2×
2×
1×
6×
| goog.provide('ol.source.TileImage');
goog.require('goog.asserts');
goog.require('goog.events');
goog.require('goog.events.EventType');
goog.require('goog.object');
goog.require('ol.ImageTile');
goog.require('ol.TileCache');
goog.require('ol.TileState');
goog.require('ol.proj');
goog.require('ol.reproj.Tile');
goog.require('ol.source.UrlTile');
/**
* @classdesc
* Base class for sources providing images divided into a tile grid.
*
* @constructor
* @fires ol.source.TileEvent
* @extends {ol.source.UrlTile}
* @param {olx.source.TileImageOptions} options Image tile options.
* @api
*/
ol.source.TileImage = function(options) {
goog.base(this, {
attributions: options.attributions,
extent: options.extent,
logo: options.logo,
opaque: options.opaque,
projection: options.projection,
state: options.state !== undefined ?
/** @type {ol.source.State} */ (options.state) : undefined,
tileGrid: options.tileGrid,
tileLoadFunction: options.tileLoadFunction ?
options.tileLoadFunction : ol.source.TileImage.defaultTileLoadFunction,
tilePixelRatio: options.tilePixelRatio,
tileUrlFunction: options.tileUrlFunction,
url: options.url,
urls: options.urls,
wrapX: options.wrapX
});
/**
* @protected
* @type {?string}
*/
this.crossOrigin =
options.crossOrigin !== undefined ? options.crossOrigin : null;
/**
* @protected
* @type {function(new: ol.ImageTile, ol.TileCoord, ol.TileState, string,
* ?string, ol.TileLoadFunctionType)}
*/
this.tileClass = options.tileClass !== undefined ?
options.tileClass : ol.ImageTile;
/**
* @protected
* @type {Object.<string, ol.TileCache>}
*/
this.tileCacheForProjection = {};
/**
* @protected
* @type {Object.<string, ol.tilegrid.TileGrid>}
*/
this.tileGridForProjection = {};
/**
* @private
* @type {number|undefined}
*/
this.reprojectionErrorThreshold_ = options.reprojectionErrorThreshold;
/**
* @private
* @type {boolean}
*/
this.renderReprojectionEdges_ = false;
};
goog.inherits(ol.source.TileImage, ol.source.UrlTile);
/**
* @inheritDoc
*/
ol.source.TileImage.prototype.canExpireCache = function() {
Iif (!ol.ENABLE_RASTER_REPROJECTION) {
return goog.base(this, 'canExpireCache');
}
var canExpire = this.tileCache.canExpireCache();
Iif (canExpire) {
return true;
} else {
return goog.object.some(this.tileCacheForProjection, function(tileCache) {
return tileCache.canExpireCache();
});
}
};
/**
* @inheritDoc
*/
ol.source.TileImage.prototype.expireCache = function(projection, usedTiles) {
if (!ol.ENABLE_RASTER_REPROJECTION) {
goog.base(this, 'expireCache', projection, usedTiles);
return;
}
var usedTileCache = this.getTileCacheForProjection(projection);
this.tileCache.expireCache(this.tileCache == usedTileCache ? usedTiles : {});
goog.object.forEach(this.tileCacheForProjection, function(tileCache) {
tileCache.expireCache(tileCache == usedTileCache ? usedTiles : {});
});
};
/**
* @inheritDoc
*/
ol.source.TileImage.prototype.getTileGridForProjection = function(projection) {
Iif (!ol.ENABLE_RASTER_REPROJECTION) {
return goog.base(this, 'getTileGridForProjection', projection);
}
var thisProj = this.getProjection();
if (this.tileGrid &&
(!thisProj || ol.proj.equivalent(thisProj, projection))) {
return this.tileGrid;
} else {
var projKey = goog.getUid(projection).toString();
if (!(projKey in this.tileGridForProjection)) {
this.tileGridForProjection[projKey] =
ol.tilegrid.getForProjection(projection);
}
return this.tileGridForProjection[projKey];
}
};
/**
* @inheritDoc
*/
ol.source.TileImage.prototype.getTileCacheForProjection = function(projection) {
Iif (!ol.ENABLE_RASTER_REPROJECTION) {
return goog.base(this, 'getTileCacheForProjection', projection);
}
var thisProj = this.getProjection();
Iif (!thisProj || ol.proj.equivalent(thisProj, projection)) {
return this.tileCache;
} else {
var projKey = goog.getUid(projection).toString();
if (!(projKey in this.tileCacheForProjection)) {
this.tileCacheForProjection[projKey] = new ol.TileCache();
}
return this.tileCacheForProjection[projKey];
}
};
/**
* @inheritDoc
*/
ol.source.TileImage.prototype.getTile =
function(z, x, y, pixelRatio, projection) {
if (!ol.ENABLE_RASTER_REPROJECTION ||
!this.getProjection() ||
!projection ||
ol.proj.equivalent(this.getProjection(), projection)) {
return this.getTileInternal(z, x, y, pixelRatio, projection);
} else {
var cache = this.getTileCacheForProjection(projection);
var tileCoordKey = this.getKeyZXY(z, x, y);
if (cache.containsKey(tileCoordKey)) {
return /** @type {!ol.Tile} */(cache.get(tileCoordKey));
} else {
var sourceProjection = this.getProjection();
var sourceTileGrid = this.getTileGridForProjection(sourceProjection);
var targetTileGrid = this.getTileGridForProjection(projection);
var tile = new ol.reproj.Tile(
sourceProjection, sourceTileGrid,
projection, targetTileGrid,
z, x, y, this.getTilePixelRatio(),
goog.bind(function(z, x, y, pixelRatio) {
return this.getTileInternal(z, x, y, pixelRatio, sourceProjection);
}, this), this.reprojectionErrorThreshold_,
this.renderReprojectionEdges_);
cache.set(tileCoordKey, tile);
return tile;
}
}
};
/**
* @param {number} z Tile coordinate z.
* @param {number} x Tile coordinate x.
* @param {number} y Tile coordinate y.
* @param {number} pixelRatio Pixel ratio.
* @param {ol.proj.Projection} projection Projection.
* @return {!ol.Tile} Tile.
* @protected
*/
ol.source.TileImage.prototype.getTileInternal =
function(z, x, y, pixelRatio, projection) {
var tileCoordKey = this.getKeyZXY(z, x, y);
Iif (this.tileCache.containsKey(tileCoordKey)) {
return /** @type {!ol.Tile} */ (this.tileCache.get(tileCoordKey));
} else {
goog.asserts.assert(projection, 'argument projection is truthy');
var tileCoord = [z, x, y];
var urlTileCoord = this.getTileCoordForTileUrlFunction(
tileCoord, projection);
var tileUrl = !urlTileCoord ? undefined :
this.tileUrlFunction(urlTileCoord, pixelRatio, projection);
var tile = new this.tileClass(
tileCoord,
tileUrl !== undefined ? ol.TileState.IDLE : ol.TileState.EMPTY,
tileUrl !== undefined ? tileUrl : '',
this.crossOrigin,
this.tileLoadFunction);
goog.events.listen(tile, goog.events.EventType.CHANGE,
this.handleTileChange, false, this);
this.tileCache.set(tileCoordKey, tile);
return tile;
}
};
/**
* Sets whether to render reprojection edges or not (usually for debugging).
* @param {boolean} render Render the edges.
* @api
*/
ol.source.TileImage.prototype.setRenderReprojectionEdges = function(render) {
if (!ol.ENABLE_RASTER_REPROJECTION ||
this.renderReprojectionEdges_ == render) {
return;
}
this.renderReprojectionEdges_ = render;
goog.object.forEach(this.tileCacheForProjection, function(tileCache) {
tileCache.clear();
});
this.changed();
};
/**
* Sets the tile grid to use when reprojecting the tiles to the given
* projection instead of the default tile grid for the projection.
*
* This can be useful when the default tile grid cannot be created
* (e.g. projection has no extent defined) or
* for optimization reasons (custom tile size, resolutions, ...).
*
* @param {ol.proj.ProjectionLike} projection Projection.
* @param {ol.tilegrid.TileGrid} tilegrid Tile grid to use for the projection.
* @api
*/
ol.source.TileImage.prototype.setTileGridForProjection =
function(projection, tilegrid) {
Eif (ol.ENABLE_RASTER_REPROJECTION) {
var proj = ol.proj.get(projection);
Eif (proj) {
var projKey = goog.getUid(proj).toString();
Eif (!(projKey in this.tileGridForProjection)) {
this.tileGridForProjection[projKey] = tilegrid;
}
}
}
};
/**
* @param {ol.ImageTile} imageTile Image tile.
* @param {string} src Source.
*/
ol.source.TileImage.defaultTileLoadFunction = function(imageTile, src) {
imageTile.getImage().src = src;
};
|