dyomedea/src/lib/geo.ts

36 lines
1.0 KiB
TypeScript

import { Rectangle } from '../components/map/types';
// cf https://wiki.openstreetmap.org/wiki/Slippy_map_tilenames#ECMAScript_(JavaScript/ActionScript,_etc.)
export const lon2tile = (lon: number, zoom: number) => {
return ((Number(lon) + 180) / 360) * Math.pow(2, zoom);
};
export const lat2tile = (lat: number, zoom: number) => {
return (
((1 -
Math.log(
Math.tan((Number(lat) * Math.PI) / 180) +
1 / Math.cos((Number(lat) * Math.PI) / 180)
) /
Math.PI) /
2) *
Math.pow(2, zoom)
);
};
export function tile2long(x: number, z: number) {
return (x / Math.pow(2, z)) * 360 - 180;
}
export function tile2lat(y: number, z: number) {
var n = Math.PI - (2 * Math.PI * y) / Math.pow(2, z);
return (180 / Math.PI) * Math.atan(0.5 * (Math.exp(n) - Math.exp(-n)));
}
export const rectanglesIntersect = (r1: Rectangle, r2: Rectangle) =>
!(
r2.topLeft.x > r1.bottomRight.x ||
r2.bottomRight.x < r1.topLeft.x ||
r2.topLeft.y > r1.bottomRight.y ||
r2.bottomRight.y < r1.topLeft.y
);