MapBoxGl Mercator Coordinate

586 阅读1分钟

MapBoxGl Mercator Coordinate

/*
* Approximate radius of the earth in meters.
* Uses the WGS-84 approximation. The radius at the equator is ~6378137 and at the poles is ~6356752. https://en.wikipedia.org/wiki/World_Geodetic_System#WGS84
* 6371008.8 is one published "average radius" see https://en.wikipedia.org/wiki/Earth_radius#Mean_radius, or ftp://athena.fsv.cvut.cz/ZFG/grs80-Moritz.pdf p.4
*/
const earthRadius = 6371008.8; // 地球半径 km
const earthCircumference = 2 * Math.PI * earthRadius; // 地球周长

纬度周长

IMG_5901.HEIC

function circumferenceAtLatitude(latitude: number) {
    return earthCircumference * Math.cos(latitude * Math.PI / 180);
}

墨卡托经度转换与逆转换

经度区间[-180, 180] 对应 [0, 1]

function mercatorXfromLng(lng: number): number {
    return (180 + lng) / 360;
}

function lngFromMercatorX(x: number): number {
    return x * 360 - 180;
}

墨卡托维度转换与逆转换

维度区间[-180, 180] 对应 [0, 1]

image.png

function mercatorYfromLat(lat: number): number {
    return (180 - (180 / Math.PI * Math.log(Math.tan(Math.PI / 4 + lat * Math.PI / 360)))) / 360;
}

function latFromMercatorY(y: number): number {
    const y2 = 180 - y * 360;
    return 360 / Math.PI * Math.atan(Math.exp(y2 * Math.PI / 180)) - 90;
}

墨卡托海拔转换与逆转换

function mercatorZfromAltitude(altitude: number, lat: number): number {
    return altitude / circumferenceAtLatitude(lat);
}

function altitudeFromMercatorZ(z: number, y: number): number {
    return z * circumferenceAtLatitude(latFromMercatorY(y));
}

最大纬度

const MAX_MERCATOR_LATITUDE = 85.051129;

墨卡托比例尺

/**
 * Determine the Mercator scale factor for a given latitude, see
 * https://en.wikipedia.org/wiki/Mercator_projection#Scale_factor
 *
 * At the equator the scale factor will be 1, which increases at higher latitudes.
 *
 * @param {number} lat Latitude
 * @returns {number} scale factor
 * @private
 */
function mercatorScale(lat: number): number {
    return 1 / Math.cos(lat * Math.PI / 180);
}
Refrence

墨卡托投影算法
墨卡托wiki
墨卡托投影wiki