Skip to content

Commit 7e1c115

Browse files
authored
Don't Use tileSize for Determining z of Tile Indexing in InfoVis (visgl#5895)
1 parent d8e8686 commit 7e1c115

File tree

3 files changed

+35
-15
lines changed

3 files changed

+35
-15
lines changed

modules/geo-layers/src/tile-layer/tileset-2d.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -174,7 +174,8 @@ export default class Tileset2D {
174174

175175
// Add custom metadata to tiles
176176
getTileMetadata({x, y, z}) {
177-
return {bbox: tileToBoundingBox(this._viewport, x, y, z)};
177+
const {tileSize} = this.opts;
178+
return {bbox: tileToBoundingBox(this._viewport, x, y, z, tileSize)};
178179
}
179180

180181
// Returns {x, y, z} of the parent tile

modules/geo-layers/src/tile-layer/utils.js

Lines changed: 20 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -117,38 +117,37 @@ function getIndexingCoords(bbox, scale, modelMatrixInverse) {
117117
return bbox.map(i => (i * scale) / TILE_SIZE);
118118
}
119119

120-
function getScale(z) {
121-
return Math.pow(2, z);
120+
function getScale(z, tileSize) {
121+
return (Math.pow(2, z) * TILE_SIZE) / tileSize;
122122
}
123123

124124
// https://wiki.openstreetmap.org/wiki/Slippy_map_tilenames#Lon..2Flat._to_tile_numbers_2
125125
export function osmTile2lngLat(x, y, z) {
126-
const scale = getScale(z);
126+
const scale = getScale(z, TILE_SIZE);
127127
const lng = (x / scale) * 360 - 180;
128128
const n = Math.PI - (2 * Math.PI * y) / scale;
129129
const lat = (180 / Math.PI) * Math.atan(0.5 * (Math.exp(n) - Math.exp(-n)));
130130
return [lng, lat];
131131
}
132132

133-
function tile2XY(x, y, z) {
134-
const scale = getScale(z);
133+
function tile2XY(x, y, z, tileSize) {
134+
const scale = getScale(z, tileSize);
135135
return [(x / scale) * TILE_SIZE, (y / scale) * TILE_SIZE];
136136
}
137-
138-
export function tileToBoundingBox(viewport, x, y, z) {
137+
export function tileToBoundingBox(viewport, x, y, z, tileSize = TILE_SIZE) {
139138
if (viewport.isGeospatial) {
140139
const [west, north] = osmTile2lngLat(x, y, z);
141140
const [east, south] = osmTile2lngLat(x + 1, y + 1, z);
142141
return {west, north, east, south};
143142
}
144-
const [left, top] = tile2XY(x, y, z);
145-
const [right, bottom] = tile2XY(x + 1, y + 1, z);
143+
const [left, top] = tile2XY(x, y, z, tileSize);
144+
const [right, bottom] = tile2XY(x + 1, y + 1, z, tileSize);
146145
return {left, top, right, bottom};
147146
}
148147

149-
function getIdentityTileIndices(viewport, z, extent, modelMatrixInverse) {
148+
function getIdentityTileIndices(viewport, z, tileSize, extent, modelMatrixInverse) {
150149
const bbox = getBoundingBox(viewport, null, extent);
151-
const scale = getScale(z);
150+
const scale = getScale(z, tileSize);
152151
const [minX, minY, maxX, maxY] = getIndexingCoords(bbox, scale, modelMatrixInverse);
153152
const indices = [];
154153

@@ -180,7 +179,9 @@ export function getTileIndices({
180179
modelMatrix,
181180
modelMatrixInverse
182181
}) {
183-
let z = Math.round(viewport.zoom + Math.log2(TILE_SIZE / tileSize));
182+
let z = viewport.isGeospatial
183+
? Math.round(viewport.zoom + Math.log2(TILE_SIZE / tileSize))
184+
: Math.ceil(viewport.zoom);
184185
if (Number.isFinite(minZoom) && z < minZoom) {
185186
if (!extent) {
186187
return [];
@@ -196,7 +197,13 @@ export function getTileIndices({
196197
}
197198
return viewport.isGeospatial
198199
? getOSMTileIndices(viewport, z, zRange, extent || DEFAULT_EXTENT)
199-
: getIdentityTileIndices(viewport, z, transformedExtent || DEFAULT_EXTENT, modelMatrixInverse);
200+
: getIdentityTileIndices(
201+
viewport,
202+
z,
203+
tileSize,
204+
transformedExtent || DEFAULT_EXTENT,
205+
modelMatrixInverse
206+
);
200207
}
201208

202209
/**

test/modules/geo-layers/tile-layer/utils.spec.js

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -142,7 +142,7 @@ const TEST_CASES = [
142142
}
143143
}),
144144
tileSize: 256,
145-
output: ['1,2,4', '1,3,4', '2,2,4', '2,3,4', '3,2,4', '3,3,4', '4,2,4', '4,3,4']
145+
output: ['1,2,3', '1,3,3', '2,2,3', '2,3,3', '3,2,3', '3,3,3', '4,2,3', '4,3,3']
146146
},
147147
{
148148
title: 'non-geospatial modelMatrix identity',
@@ -396,6 +396,12 @@ test('tileToBoundingBox#Infovis', t => {
396396
'0,0,1 Should match the results.'
397397
);
398398

399+
t.deepEqual(
400+
tileToBoundingBox(viewport, 0, 0, 0, 256),
401+
{left: 0, top: 0, right: 256, bottom: 256},
402+
'0,0,0 with custom tileSize Should match the results.'
403+
);
404+
399405
t.deepEqual(
400406
tileToBoundingBox(viewport, 4, -1, 2),
401407
{left: 512, top: -128, right: 640, bottom: 0},
@@ -408,6 +414,12 @@ test('tileToBoundingBox#Infovis', t => {
408414
'4,-1,3 Should match the results.'
409415
);
410416

417+
t.deepEqual(
418+
tileToBoundingBox(viewport, 4, -1, 2, 256),
419+
{left: 256, top: -64, right: 320, bottom: 0},
420+
'4,-1,2 with custom tileSize Should match the results.'
421+
);
422+
411423
t.end();
412424
});
413425

0 commit comments

Comments
 (0)