Skip to content

Commit

Permalink
Fix crash in getURLFromTemplate (visgl#6898)
Browse files Browse the repository at this point in the history
  • Loading branch information
Pessimistress authored May 11, 2022
1 parent b613f85 commit 2d5ddd2
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 7 deletions.
10 changes: 3 additions & 7 deletions modules/geo-layers/src/tile-layer/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ function transformBox(bbox: Bounds, modelMatrix: Matrix4): Bounds {
}

function stringHash(s: string): number {
return s.split('').reduce((a, b) => ((a << 5) - a + b.charCodeAt(0)) | 0, 0);
return Math.abs(s.split('').reduce((a, b) => ((a << 5) - a + b.charCodeAt(0)) | 0, 0));
}

export function getURLFromTemplate(template: string | string[], tile: Tile2DHeader): string | null {
Expand Down Expand Up @@ -137,19 +137,15 @@ function getScale(z: number, tileSize: number): number {
}

// https://wiki.openstreetmap.org/wiki/Slippy_map_tilenames#Lon..2Flat._to_tile_numbers_2
export function osmTile2lngLat(
x: number,
y: number,
z: number
): [latitude: number, longitude: number] {
export function osmTile2lngLat(x: number, y: number, z: number): [number, number] {
const scale = getScale(z, TILE_SIZE);
const lng = (x / scale) * 360 - 180;
const n = Math.PI - (2 * Math.PI * y) / scale;
const lat = (180 / Math.PI) * Math.atan(0.5 * (Math.exp(n) - Math.exp(-n)));
return [lng, lat];
}

function tile2XY(x: number, y: number, z: number, tileSize: number): [x: number, y: number] {
function tile2XY(x: number, y: number, z: number, tileSize: number): [number, number] {
const scale = getScale(z, tileSize);
return [(x / scale) * TILE_SIZE, (y / scale) * TILE_SIZE];
}
Expand Down
5 changes: 5 additions & 0 deletions test/modules/geo-layers/tile-layer/utils.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -527,6 +527,11 @@ test('getURLFromTemplate', t => {
'https://server.com/ep1/2/2.png',
'array of templates'
);
t.is(
getURLFromTemplate(TEST_TEMPLATE_ARRAY, {index: {x: 17, y: 11, z: 5}, id: '17-11-5'}),
'https://server.com/ep2/17/11.png',
'array of templates'
);
t.is(getURLFromTemplate(null, {index: {x: 1, y: 2, z: 0}}), null, 'invalid template');
t.is(getURLFromTemplate([], {index: {x: 1, y: 2, z: 0}}), null, 'empty array');
t.end();
Expand Down

0 comments on commit 2d5ddd2

Please sign in to comment.