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 | 1×
1×
1×
1×
1×
1×
1×
1×
1×
1×
297×
297×
297×
6663×
295×
295×
295×
295×
14×
14×
293×
293×
150×
293×
292×
292×
16×
16×
290×
290×
289×
289×
289×
289×
17×
197×
197×
272×
232×
1×
1×
25×
25×
28×
34×
1×
62×
62×
62×
426×
426×
62×
1×
393×
1×
324×
1×
424×
1×
25650×
25650×
1×
38778×
1×
55×
1×
66×
65×
65×
1×
1×
82×
82×
82×
82×
82×
82×
82×
82×
1×
12362×
12362×
12362×
12362×
12362×
12362×
1×
12276×
12276×
1×
324×
324×
324×
324×
1×
443×
443×
443×
443×
443×
443×
443×
443×
1×
14×
1×
24791×
24791×
24791×
24791×
24791×
24791×
24791×
24791×
24791×
24791×
24791×
12364×
12364×
12427×
12427×
24791×
1×
49×
49×
1×
1×
25899×
25895×
4×
1×
25×
7×
18×
1×
24963×
24963×
1×
232×
232×
232×
6567×
232×
| goog.provide('ol.tilegrid.TileGrid');
goog.require('ol');
goog.require('ol.asserts');
goog.require('ol.TileRange');
goog.require('ol.array');
goog.require('ol.extent');
goog.require('ol.math');
goog.require('ol.size');
goog.require('ol.tilecoord');
/**
* @classdesc
* Base class for setting the grid pattern for sources accessing tiled-image
* servers.
*
* @constructor
* @param {olx.tilegrid.TileGridOptions} options Tile grid options.
* @struct
* @api
*/
ol.tilegrid.TileGrid = function(options) {
/**
* @protected
* @type {number}
*/
this.minZoom = options.minZoom !== undefined ? options.minZoom : 0;
/**
* @private
* @type {!Array.<number>}
*/
this.resolutions_ = options.resolutions;
ol.asserts.assert(ol.array.isSorted(this.resolutions_, function(a, b) {
return b - a;
}, true), 17); // `resolutions` must be sorted in descending order
/**
* @protected
* @type {number}
*/
this.maxZoom = this.resolutions_.length - 1;
/**
* @private
* @type {ol.Coordinate}
*/
this.origin_ = options.origin !== undefined ? options.origin : null;
/**
* @private
* @type {Array.<ol.Coordinate>}
*/
this.origins_ = null;
if (options.origins !== undefined) {
this.origins_ = options.origins;
ol.asserts.assert(this.origins_.length == this.resolutions_.length,
20); // Number of `origins` and `resolutions` must be equal
}
var extent = options.extent;
if (extent !== undefined &&
!this.origin_ && !this.origins_) {
this.origin_ = ol.extent.getTopLeft(extent);
}
ol.asserts.assert(
(!this.origin_ && this.origins_) || (this.origin_ && !this.origins_),
18); // Either `origin` or `origins` must be configured, never both
/**
* @private
* @type {Array.<number|ol.Size>}
*/
this.tileSizes_ = null;
if (options.tileSizes !== undefined) {
this.tileSizes_ = options.tileSizes;
ol.asserts.assert(this.tileSizes_.length == this.resolutions_.length,
19); // Number of `tileSizes` and `resolutions` must be equal
}
/**
* @private
* @type {number|ol.Size}
*/
this.tileSize_ = options.tileSize !== undefined ?
options.tileSize :
!this.tileSizes_ ? ol.DEFAULT_TILE_SIZE : null;
ol.asserts.assert(
(!this.tileSize_ && this.tileSizes_) ||
(this.tileSize_ && !this.tileSizes_),
22); // Either `tileSize` or `tileSizes` must be configured, never both
/**
* @private
* @type {ol.Extent}
*/
this.extent_ = extent !== undefined ? extent : null;
/**
* @private
* @type {Array.<ol.TileRange>}
*/
this.fullTileRanges_ = null;
/**
* @private
* @type {ol.Size}
*/
this.tmpSize_ = [0, 0];
if (options.sizes !== undefined) {
this.fullTileRanges_ = options.sizes.map(function(size, z) {
var tileRange = new ol.TileRange(
Math.min(0, size[0]), Math.max(size[0] - 1, -1),
Math.min(0, size[1]), Math.max(size[1] - 1, -1));
return tileRange;
}, this);
} else if (extent) {
this.calculateTileRanges_(extent);
}
};
/**
* @private
* @type {ol.TileCoord}
*/
ol.tilegrid.TileGrid.tmpTileCoord_ = [0, 0, 0];
/**
* Call a function with each tile coordinate for a given extent and zoom level.
*
* @param {ol.Extent} extent Extent.
* @param {number} zoom Zoom level.
* @param {function(ol.TileCoord)} callback Function called with each tile coordinate.
* @api
*/
ol.tilegrid.TileGrid.prototype.forEachTileCoord = function(extent, zoom, callback) {
var tileRange = this.getTileRangeForExtentAndZ(extent, zoom);
for (var i = tileRange.minX, ii = tileRange.maxX; i <= ii; ++i) {
for (var j = tileRange.minY, jj = tileRange.maxY; j <= jj; ++j) {
callback([zoom, i, j]);
}
}
};
/**
* @param {ol.TileCoord} tileCoord Tile coordinate.
* @param {function(this: T, number, ol.TileRange): boolean} callback Callback.
* @param {T=} opt_this The object to use as `this` in `callback`.
* @param {ol.TileRange=} opt_tileRange Temporary ol.TileRange object.
* @param {ol.Extent=} opt_extent Temporary ol.Extent object.
* @return {boolean} Callback succeeded.
* @template T
*/
ol.tilegrid.TileGrid.prototype.forEachTileCoordParentTileRange = function(tileCoord, callback, opt_this, opt_tileRange, opt_extent) {
var tileCoordExtent = this.getTileCoordExtent(tileCoord, opt_extent);
var z = tileCoord[0] - 1;
while (z >= this.minZoom) {
Iif (callback.call(opt_this, z,
this.getTileRangeForExtentAndZ(tileCoordExtent, z, opt_tileRange))) {
return true;
}
--z;
}
return false;
};
/**
* Get the extent for this tile grid, if it was configured.
* @return {ol.Extent} Extent.
*/
ol.tilegrid.TileGrid.prototype.getExtent = function() {
return this.extent_;
};
/**
* Get the maximum zoom level for the grid.
* @return {number} Max zoom.
* @api
*/
ol.tilegrid.TileGrid.prototype.getMaxZoom = function() {
return this.maxZoom;
};
/**
* Get the minimum zoom level for the grid.
* @return {number} Min zoom.
* @api
*/
ol.tilegrid.TileGrid.prototype.getMinZoom = function() {
return this.minZoom;
};
/**
* Get the origin for the grid at the given zoom level.
* @param {number} z Z.
* @return {ol.Coordinate} Origin.
* @api
*/
ol.tilegrid.TileGrid.prototype.getOrigin = function(z) {
Eif (this.origin_) {
return this.origin_;
} else {
return this.origins_[z];
}
};
/**
* Get the resolution for the given zoom level.
* @param {number} z Z.
* @return {number} Resolution.
* @api
*/
ol.tilegrid.TileGrid.prototype.getResolution = function(z) {
return this.resolutions_[z];
};
/**
* Get the list of resolutions for the tile grid.
* @return {Array.<number>} Resolutions.
* @api
*/
ol.tilegrid.TileGrid.prototype.getResolutions = function() {
return this.resolutions_;
};
/**
* @param {ol.TileCoord} tileCoord Tile coordinate.
* @param {ol.TileRange=} opt_tileRange Temporary ol.TileRange object.
* @param {ol.Extent=} opt_extent Temporary ol.Extent object.
* @return {ol.TileRange} Tile range.
*/
ol.tilegrid.TileGrid.prototype.getTileCoordChildTileRange = function(tileCoord, opt_tileRange, opt_extent) {
if (tileCoord[0] < this.maxZoom) {
var tileCoordExtent = this.getTileCoordExtent(tileCoord, opt_extent);
return this.getTileRangeForExtentAndZ(
tileCoordExtent, tileCoord[0] + 1, opt_tileRange);
} else {
return null;
}
};
/**
* @param {number} z Z.
* @param {ol.TileRange} tileRange Tile range.
* @param {ol.Extent=} opt_extent Temporary ol.Extent object.
* @return {ol.Extent} Extent.
*/
ol.tilegrid.TileGrid.prototype.getTileRangeExtent = function(z, tileRange, opt_extent) {
var origin = this.getOrigin(z);
var resolution = this.getResolution(z);
var tileSize = ol.size.toSize(this.getTileSize(z), this.tmpSize_);
var minX = origin[0] + tileRange.minX * tileSize[0] * resolution;
var maxX = origin[0] + (tileRange.maxX + 1) * tileSize[0] * resolution;
var minY = origin[1] + tileRange.minY * tileSize[1] * resolution;
var maxY = origin[1] + (tileRange.maxY + 1) * tileSize[1] * resolution;
return ol.extent.createOrUpdate(minX, minY, maxX, maxY, opt_extent);
};
/**
* @param {ol.Extent} extent Extent.
* @param {number} resolution Resolution.
* @param {ol.TileRange=} opt_tileRange Temporary tile range object.
* @return {ol.TileRange} Tile range.
*/
ol.tilegrid.TileGrid.prototype.getTileRangeForExtentAndResolution = function(extent, resolution, opt_tileRange) {
var tileCoord = ol.tilegrid.TileGrid.tmpTileCoord_;
this.getTileCoordForXYAndResolution_(
extent[0], extent[1], resolution, false, tileCoord);
var minX = tileCoord[1];
var minY = tileCoord[2];
this.getTileCoordForXYAndResolution_(
extent[2], extent[3], resolution, true, tileCoord);
return ol.TileRange.createOrUpdate(
minX, tileCoord[1], minY, tileCoord[2], opt_tileRange);
};
/**
* @param {ol.Extent} extent Extent.
* @param {number} z Z.
* @param {ol.TileRange=} opt_tileRange Temporary tile range object.
* @return {ol.TileRange} Tile range.
*/
ol.tilegrid.TileGrid.prototype.getTileRangeForExtentAndZ = function(extent, z, opt_tileRange) {
var resolution = this.getResolution(z);
return this.getTileRangeForExtentAndResolution(
extent, resolution, opt_tileRange);
};
/**
* @param {ol.TileCoord} tileCoord Tile coordinate.
* @return {ol.Coordinate} Tile center.
*/
ol.tilegrid.TileGrid.prototype.getTileCoordCenter = function(tileCoord) {
var origin = this.getOrigin(tileCoord[0]);
var resolution = this.getResolution(tileCoord[0]);
var tileSize = ol.size.toSize(this.getTileSize(tileCoord[0]), this.tmpSize_);
return [
origin[0] + (tileCoord[1] + 0.5) * tileSize[0] * resolution,
origin[1] + (tileCoord[2] + 0.5) * tileSize[1] * resolution
];
};
/**
* Get the extent of a tile coordinate.
*
* @param {ol.TileCoord} tileCoord Tile coordinate.
* @param {ol.Extent=} opt_extent Temporary extent object.
* @return {ol.Extent} Extent.
* @api
*/
ol.tilegrid.TileGrid.prototype.getTileCoordExtent = function(tileCoord, opt_extent) {
var origin = this.getOrigin(tileCoord[0]);
var resolution = this.getResolution(tileCoord[0]);
var tileSize = ol.size.toSize(this.getTileSize(tileCoord[0]), this.tmpSize_);
var minX = origin[0] + tileCoord[1] * tileSize[0] * resolution;
var minY = origin[1] + tileCoord[2] * tileSize[1] * resolution;
var maxX = minX + tileSize[0] * resolution;
var maxY = minY + tileSize[1] * resolution;
return ol.extent.createOrUpdate(minX, minY, maxX, maxY, opt_extent);
};
/**
* Get the tile coordinate for the given map coordinate and resolution. This
* method considers that coordinates that intersect tile boundaries should be
* assigned the higher tile coordinate.
*
* @param {ol.Coordinate} coordinate Coordinate.
* @param {number} resolution Resolution.
* @param {ol.TileCoord=} opt_tileCoord Destination ol.TileCoord object.
* @return {ol.TileCoord} Tile coordinate.
* @api
*/
ol.tilegrid.TileGrid.prototype.getTileCoordForCoordAndResolution = function(coordinate, resolution, opt_tileCoord) {
return this.getTileCoordForXYAndResolution_(
coordinate[0], coordinate[1], resolution, false, opt_tileCoord);
};
/**
* @param {number} x X.
* @param {number} y Y.
* @param {number} resolution Resolution.
* @param {boolean} reverseIntersectionPolicy Instead of letting edge
* intersections go to the higher tile coordinate, let edge intersections
* go to the lower tile coordinate.
* @param {ol.TileCoord=} opt_tileCoord Temporary ol.TileCoord object.
* @return {ol.TileCoord} Tile coordinate.
* @private
*/
ol.tilegrid.TileGrid.prototype.getTileCoordForXYAndResolution_ = function(
x, y, resolution, reverseIntersectionPolicy, opt_tileCoord) {
var z = this.getZForResolution(resolution);
var scale = resolution / this.getResolution(z);
var origin = this.getOrigin(z);
var tileSize = ol.size.toSize(this.getTileSize(z), this.tmpSize_);
var adjustX = reverseIntersectionPolicy ? 0.5 : 0;
var adjustY = reverseIntersectionPolicy ? 0 : 0.5;
var xFromOrigin = Math.floor((x - origin[0]) / resolution + adjustX);
var yFromOrigin = Math.floor((y - origin[1]) / resolution + adjustY);
var tileCoordX = scale * xFromOrigin / tileSize[0];
var tileCoordY = scale * yFromOrigin / tileSize[1];
if (reverseIntersectionPolicy) {
tileCoordX = Math.ceil(tileCoordX) - 1;
tileCoordY = Math.ceil(tileCoordY) - 1;
} else {
tileCoordX = Math.floor(tileCoordX);
tileCoordY = Math.floor(tileCoordY);
}
return ol.tilecoord.createOrUpdate(z, tileCoordX, tileCoordY, opt_tileCoord);
};
/**
* Get a tile coordinate given a map coordinate and zoom level.
* @param {ol.Coordinate} coordinate Coordinate.
* @param {number} z Zoom level.
* @param {ol.TileCoord=} opt_tileCoord Destination ol.TileCoord object.
* @return {ol.TileCoord} Tile coordinate.
* @api
*/
ol.tilegrid.TileGrid.prototype.getTileCoordForCoordAndZ = function(coordinate, z, opt_tileCoord) {
var resolution = this.getResolution(z);
return this.getTileCoordForXYAndResolution_(
coordinate[0], coordinate[1], resolution, false, opt_tileCoord);
};
/**
* @param {ol.TileCoord} tileCoord Tile coordinate.
* @return {number} Tile resolution.
*/
ol.tilegrid.TileGrid.prototype.getTileCoordResolution = function(tileCoord) {
return this.resolutions_[tileCoord[0]];
};
/**
* Get the tile size for a zoom level. The type of the return value matches the
* `tileSize` or `tileSizes` that the tile grid was configured with. To always
* get an `ol.Size`, run the result through `ol.size.toSize()`.
* @param {number} z Z.
* @return {number|ol.Size} Tile size.
* @api
*/
ol.tilegrid.TileGrid.prototype.getTileSize = function(z) {
if (this.tileSize_) {
return this.tileSize_;
} else {
return this.tileSizes_[z];
}
};
/**
* @param {number} z Zoom level.
* @return {ol.TileRange} Extent tile range for the specified zoom level.
*/
ol.tilegrid.TileGrid.prototype.getFullTileRange = function(z) {
if (!this.fullTileRanges_) {
return null;
} else {
return this.fullTileRanges_[z];
}
};
/**
* @param {number} resolution Resolution.
* @param {number=} opt_direction If 0, the nearest resolution will be used.
* If 1, the nearest lower resolution will be used. If -1, the nearest
* higher resolution will be used. Default is 0.
* @return {number} Z.
* @api
*/
ol.tilegrid.TileGrid.prototype.getZForResolution = function(
resolution, opt_direction) {
var z = ol.array.linearFindNearest(this.resolutions_, resolution,
opt_direction || 0);
return ol.math.clamp(z, this.minZoom, this.maxZoom);
};
/**
* @param {!ol.Extent} extent Extent for this tile grid.
* @private
*/
ol.tilegrid.TileGrid.prototype.calculateTileRanges_ = function(extent) {
var length = this.resolutions_.length;
var fullTileRanges = new Array(length);
for (var z = this.minZoom; z < length; ++z) {
fullTileRanges[z] = this.getTileRangeForExtentAndZ(extent, z);
}
this.fullTileRanges_ = fullTileRanges;
};
|