JS实现WSG(BLH)和空间直角坐标系(XYZ)相互转换
重新编辑我
点击查看详细内容
/**
* Created by xsg on 2014/6/23.
*/
var pi_180 = Math.PI / 180;
var _180_pi = 180 / Math.PI;
var projectionTypes = {};
projectionTypes.bj54 = {
a:6378245,//长半轴
e2:0.006693421622966//第一偏心率平方
};
projectionTypes.wgs84 = {
a:6378135,//长半轴
e2:0.00669437999013//第一偏心率平方
};
var myparams = {
dx : 31.4,
dy : -144.3,
dz : -74.8,
rx : 0,
ry : 0,
rz : 0.814,
m : -0.38
};
//7坐标系统转换方法
//sourceType:源投影类型 targetType:目标投影类型 params: 7参数 blh:源大地坐标
function coordinateTransfUse7params(sourceType,targetType,params,blh){
sourceType = sourceType.toLowerCase();
targetType = targetType.toLowerCase();
var XYZ1 = _BLH2XYZ(sourceType,blh);
var XYZ2 = _transXYZBy7params(XYZ1,params);
var blh2 = _XYZ2BLH(targetType,XYZ2);
console.log(blh2);
}
function _BLH2XYZ(projectionType,blhObj){//将大地坐标喜欢换为空间直角坐标系
var e2 = projectionTypes[projectionType].e2;//第一偏心率平方值
var a = projectionTypes[projectionType].a;//长半轴
var N = a / Math.sqrt(1 - e2 * Math.sin(blhObj.b * pi_180) * Math.sin(blhObj.b * pi_180));
var X = (N + blhObj.h) * Math.cos(blhObj.b * pi_180) * Math.cos(blhObj.l * pi_180);
var Y = (N + blhObj.h) * Math.cos(blhObj.b * pi_180) * Math.sin(blhObj.l * pi_180);
var Z = [N * (1 - e2 ) + blhObj.h] * Math.sin(blhObj.b * pi_180);
var resultObj = {};
resultObj.X = X;
resultObj.Y = Y;
resultObj.Z = Z;
return resultObj;
}
function _transXYZBy7params(xyz0,params){//根据7参数对空间直角坐标系进行运算
var x0 = xyz0.X;
var y0 = xyz0.Y;
var z0 = xyz0.Z;
var dx = params.dx;
var dy = params.dy;
var dz = params.dz;
var rx = params.rx;
var ry = params.ry;
var rz = params.rz;
var m = params.m;
var x1 = dx + (1 + m) * x0 + Math.sin(rz / 60 / 60 * pi_180) * y0 - Math.sin(ry / 60 / 60 * pi_180) * z0;
var y1 = dy + (1 + m) * y0 - Math.sin(rz / 60 / 60 * pi_180) * x0 + Math.sin(rx / 60 / 60 * pi_180) * z0;
var z1 = dz + (1 + m) * z0 + Math.sin(ry / 60 / 60 * pi_180) * x0 - Math.sin(rx / 60 / 60 * pi_180) * y0;
var resultObj = {};
resultObj.X = x1;
resultObj.Y = y1;
resultObj.Z = z1;
return resultObj;
}
function _XYZ2BLH(projectionType,xyz1){
var a = projectionTypes[projectionType].a;
var e2 = projectionTypes[projectionType].e2;//第一偏心率平方值
var X = xyz1.X;
var Y = xyz1.Y;
var Z = xyz1.Z;
var L = Math.atan(Y / X) * _180_pi;
var B = Math.atan(Z / Math.sqrt(X * X + Y * Y));
while(true){//迭代计算
var N = a / Math.sqrt(1 - e2 * Math.sin(B) * Math.sin(B));
var tempB = Math.atan((N * e2 * Math.sin(B) + Z ) / Math.sqrt(X * X + Y * Y ));
B = Math.atan((Z + N * e2 * Math.sin(tempB)) / Math.sqrt(X * X + Y * Y ));
if(tempB == B){
break;
}
}
var H = Z / Math.sin(B) - N * (1 - e2);
B = B * _180_pi;
var resultObj = {};
resultObj.L = L;
resultObj.B = B;
resultObj.H = H;
if( resultObj.B < 0 ){
resultObj.B = resultObj.B + 180;
}
if(resultObj.L < 0 ){
resultObj.L = resultObj.L + 180;
}
return resultObj;
}