| 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 | 1
1
1
1
1
1
1
1
1
1
1
1
3
1
1
1
1
1
1
1
3
1
1
1
1
1
3
1
1
436
380
380
380
380
56
1
273
1
5
1
1
1
1
1
1
3
3
2
3
2
3
3
1
1
9
1
29
29
29
29
6
6
6
6
23
1
48
48
48
48
2
46
46
46
10
36
46
4
42
| goog.provide('ol.TileCoord');
goog.provide('ol.tilecoord');
goog.require('goog.array');
goog.require('goog.asserts');
goog.require('ol.extent');
/**
* An array of three numbers representing the location of a tile in a tile
* grid. The order is `z`, `x`, and `y`. `z` is the zoom level.
* @typedef {Array.<number>} ol.TileCoord
* @api
*/
ol.TileCoord;
/**
* @enum {number}
*/
ol.QuadKeyCharCode = {
ZERO: '0'.charCodeAt(0),
ONE: '1'.charCodeAt(0),
TWO: '2'.charCodeAt(0),
THREE: '3'.charCodeAt(0)
};
/**
* @param {string} quadKey Quad key.
* @return {ol.TileCoord} Tile coordinate.
*/
ol.tilecoord.createFromQuadKey = function(quadKey) {
var z = quadKey.length, x = 0, y = 0;
var mask = 1 << (z - 1);
var i;
for (i = 0; i < z; ++i) {
switch (quadKey.charCodeAt(i)) {
case ol.QuadKeyCharCode.ONE:
x += mask;
break;
case ol.QuadKeyCharCode.TWO:
y += mask;
break;
case ol.QuadKeyCharCode.THREE:
x += mask;
y += mask;
break;
}
mask >>= 1;
}
return [z, x, y];
};
/**
* @param {string} str String that follows pattern “z/x/y” where x, y and z are
* numbers.
* @return {ol.TileCoord} Tile coord.
*/
ol.tilecoord.createFromString = function(str) {
var v = str.split('/');
goog.asserts.assert(v.length === 3,
'must provide a string in "z/x/y" format, got "%s"', str);
v = goog.array.map(v, function(e, i, a) {
return parseInt(e, 10);
});
return v;
};
/**
* @param {number} z Z.
* @param {number} x X.
* @param {number} y Y.
* @param {ol.TileCoord=} opt_tileCoord Tile coordinate.
* @return {ol.TileCoord} Tile coordinate.
*/
ol.tilecoord.createOrUpdate = function(z, x, y, opt_tileCoord) {
if (goog.isDef(opt_tileCoord)) {
opt_tileCoord[0] = z;
opt_tileCoord[1] = x;
opt_tileCoord[2] = y;
return opt_tileCoord;
} else {
return [z, x, y];
}
};
/**
* @param {number} z Z.
* @param {number} x X.
* @param {number} y Y.
* @return {string} Key.
*/
ol.tilecoord.getKeyZXY = function(z, x, y) {
return z + '/' + x + '/' + y;
};
/**
* @param {ol.TileCoord} tileCoord Tile coord.
* @return {number} Hash.
*/
ol.tilecoord.hash = function(tileCoord) {
return (tileCoord[1] << tileCoord[0]) + tileCoord[2];
};
/**
* @param {ol.TileCoord} tileCoord Tile coord.
* @return {string} Quad key.
*/
ol.tilecoord.quadKey = function(tileCoord) {
var z = tileCoord[0];
var digits = new Array(z);
var mask = 1 << (z - 1);
var i, charCode;
for (i = 0; i < z; ++i) {
charCode = ol.QuadKeyCharCode.ZERO;
if (tileCoord[1] & mask) {
charCode += 1;
}
if (tileCoord[2] & mask) {
charCode += 2;
}
digits[i] = String.fromCharCode(charCode);
mask >>= 1;
}
return digits.join('');
};
/**
* @param {ol.TileCoord} tileCoord Tile coord.
* @return {string} String.
*/
ol.tilecoord.toString = function(tileCoord) {
return ol.tilecoord.getKeyZXY(tileCoord[0], tileCoord[1], tileCoord[2]);
};
/**
* @param {ol.TileCoord} tileCoord Tile coordinate.
* @param {ol.tilegrid.TileGrid} tileGrid Tile grid.
* @param {ol.proj.Projection} projection Projection.
* @return {ol.TileCoord} Tile coordinate.
*/
ol.tilecoord.wrapX = function(tileCoord, tileGrid, projection) {
var z = tileCoord[0];
var center = tileGrid.getTileCoordCenter(tileCoord);
var projectionExtent = ol.tilegrid.extentFromProjection(projection);
if (!ol.extent.containsCoordinate(projectionExtent, center)) {
var worldWidth = ol.extent.getWidth(projectionExtent);
var worldsAway = Math.ceil((projectionExtent[0] - center[0]) / worldWidth);
center[0] += worldWidth * worldsAway;
return tileGrid.getTileCoordForCoordAndZ(center, z);
} else {
return tileCoord;
}
};
/**
* @param {ol.TileCoord} tileCoord Tile coordinate.
* @param {!ol.tilegrid.TileGrid} tileGrid Tile grid.
* @return {ol.TileCoord} Tile coordinate.
*/
ol.tilecoord.restrictByExtentAndZ = function(tileCoord, tileGrid) {
var z = tileCoord[0];
var x = tileCoord[1];
var y = tileCoord[2];
if (tileGrid.getMinZoom() > z || z > tileGrid.getMaxZoom()) {
return null;
}
var extent = tileGrid.getExtent();
var tileRange;
if (goog.isNull(extent)) {
tileRange = tileGrid.getFullTileRange(z);
} else {
tileRange = tileGrid.getTileRangeForExtentAndZ(extent, z);
}
if (goog.isNull(tileRange)) {
return tileCoord;
} else {
return tileRange.containsXY(x, y) ? tileCoord : null;
}
};
|