web版 cgcs2000与wgs84 坐标相互转换代码
2023-04-28 18:03:47
94次阅读
0个评论
/**
*坐标转换对象
* @alias EV_CoodTransfrom
* @constructor
*
* @see EV_CoodTransfrom
*/
function EV_CoodTransfrom() {
};
/**
* 大地2000坐标转经纬度
* @param {Number} [longitude=0.0] The longitude, in radians.
* @param {Number} [latitude=0.0] The latitude, in radians.
* @param {Number} [height=0.0] The height, in meters.
*/
EV_CoodTransfrom.prototype.cgcs2000ToWgs84 = function(longitude, latitude, height) {
const a = 6378137;
let f = 298.257222101;
let xyz = blhToxyz(a, f, latitude, longitude, height);
f = 298.257223563;
let outPos = xyzToblh(a, f, xyz[0], xyz[1], xyz[2]);
return new Cesium.Cartographic(outPos[0], outPos[1], outPos[2]);
};
/**
* 经纬度转大地2000坐标
* @param {Number} [longitude=0.0] The longitude, in radians.
* @param {Number} [latitude=0.0] The latitude, in radians.
* @param {Number} [height=0.0] The height, in meters.
*/
EV_CoodTransfrom.prototype.wgs84ToCgcs2000 = function(longitude, latitude, height) {
const a = 6378137;
let f = 298.257223563;
let xyz = blhToxyz(a, f, latitude, longitude, height);
f = 298.257222101;
let outPos = xyzToblh(a, f, xyz[0], xyz[1], xyz[2]);
return new Cesium.Cartographic(outPos[0], outPos[1], outPos[2]);
};
//大地测量球面坐标转直角坐标xyz
function blhToxyz(a, f, b, l, h) {
let x = 0,
y = 0,
z = 0;
let tempB = b;
let tempL = l;
let tempf = 0;
if (f != 0) {
tempf = 1 / f;
}
let e = 2 * tempf - tempf * tempf;
let N = a / Math.sqrt(1 - e * Math.sin(tempB) * Math.sin(tempB));
//
x = (N + h) * Math.cos(tempB) * Math.cos(tempL);
//
y = (N + h) * Math.cos(tempB) * Math.sin(tempL);
//
z = ((1 - e) * N + h) * Math.sin(tempB);
return [x, y, z];
}
function xyzToblh(a, f, X, Y, Z) {
let b = 0,
l = 0,
h = 0;
let tempf = 0;
if (f != 0) {
tempf = 1 / f;
}
////
let e = 2 * tempf - tempf * tempf;
let B_i = 0;
let N = a; //初值
let delta_b = 0;
//B迭代求解
let Max_ITR = 20;
let i = 0;
do {
let temp = (Z + e * N * Math.sin(B_i)) / Math.sqrt(X * X + Y * Y);
B_i = Math.atan(temp);
N = a / Math.sqrt(1 - e * Math.sin(B_i) * Math.sin(B_i));
delta_b = B_i - b;
b = B_i;
++i;
if (i > Max_ITR)
return false;
} while (Math.abs(delta_b) > 1E-9);
N = a / Math.sqrt(1 - e * Math.sin(b) * Math.sin(b));
//l
l = Math.atan(Y / X);
//象限判断
if (Y > 0 && l < 0) l += Cesium.Math.PI;
if (Y < 0 && l > 0) l -= Cesium.Math.PI;
//h
h = X / (Math.cos(l) * Math.cos(b)) - N;
return [l, b, h];
}
export default EV_CoodTransfrom;
0
0