proj4经纬度bl转换xy_分享proj4js中经纬度和兰伯特投影的转换代码
引言
蘭伯特投影簡(jiǎn)介參見百科搜索:
蘭伯特投影在氣象數(shù)據(jù)的處理中,是比較常用的投影坐標(biāo)系,根據(jù)不同區(qū)域、范圍進(jìn)行投影。
proj4是專業(yè)的坐標(biāo)轉(zhuǎn)換類庫(kù),有各種語言版本的,C++,java,js,python版等,可以很方便的將坐標(biāo)從一個(gè)坐標(biāo)系轉(zhuǎn)換到另一個(gè)坐標(biāo)系。
在前端使用的時(shí)候,應(yīng)用場(chǎng)景需要轉(zhuǎn)換大量的坐標(biāo),就會(huì)發(fā)現(xiàn)使用proj4js存在性能問題,查看了一下proj4js的源代碼,發(fā)現(xiàn)類庫(kù)每次調(diào)用初始化很多不相關(guān)的類型,對(duì)象等,所以,在基礎(chǔ)上,進(jìn)行了提取。
轉(zhuǎn)換代碼及說明
//初始化常用的變量,直接換算成弧度,提升計(jì)算性能
varEPSLN?=?(typeofNumber.EPSILON?==='undefined')???1.0e-10?:?Number.EPSILON;
varconv?=?180?/?Math.PI;
varHALF_PI?=?Math.PI?/?2;
varSPI?=?3.14159265359;
varTWO_PI?=?2?*?Math.PI;
vara?=?6378137;
varb?=?6356752.314245179;
vare?=?0.08181919084262157;
varlat1?=?0.52359877559829;
varlat2?=?1.04719755119659;
varlong0?=?1.8029251173101;
varlat0?=?0;
vark0?=?1;
varns;
varf0;
varrh;
//常用的轉(zhuǎn)換參數(shù),直接提取引用
vartsfnz?=function(eccent,?phi,?sinphi)?{
varcon?=?eccent?*?sinphi;
varcom?=?0.5?*?eccent;
con?=?Math.pow(((1?-?con)?/?(1?+?con)),?com);
return(Math.tan(0.5?*?(HALF_PI?-?phi))?/?con);
};
varsign?=function(x)?{
returnx?
};
varmsfnz?=function(eccent,?sinphi,?cosphi)?{
varcon?=?eccent?*?sinphi;
returncosphi?/?(Math.sqrt(1?-?con?*?con));
};
varadjust_lon?=function(x)?{
return(Math.abs(x)?<=?SPI)???x?:?(x?-?(sign(x)?*?TWO_PI));
};
varphi2z?=function(eccent,?ts)?{
vareccnth?=?0.5?*?eccent;
varcon,?dphi;
varphi?=?HALF_PI?-?2?*?Math.atan(ts);
for(vari?=?0;?i?<=?15;?i++)?{
con?=?eccent?*?Math.sin(phi);
dphi?=?HALF_PI?-?2?*?Math.atan(ts?*?(Math.pow(((1?-?con)?/?(1?+?con)),?eccnth)))?-?phi;
phi?+=?dphi;
if(Math.abs(dphi)?<=?0.0000000001)?{
returnphi;
}
}
return-9999;
};
//根據(jù)proj4的坐標(biāo)系描述字符串,解析其中的參數(shù)
functioninit(prjstr)?{
if(prjstr.indexOf("?")?>?-1)?{
var_prjArr?=?prjstr.split("?");
_prjArr.forEach(function(item,?index,?input)?{
if(item.indexOf("lat_0")?>?-1)?{
lat0?=?parseFloat(item.split("=")[1])?/?conv;
}
})
}
varsin1?=?Math.sin(lat1);
varcos1?=?Math.cos(lat1);
varms1?=?msfnz(e,?sin1,?cos1);
varts1?=?tsfnz(e,?lat1,?sin1);
varsin2?=?Math.sin(lat2);
varcos2?=?Math.cos(lat2);
varms2?=?msfnz(e,?sin2,?cos2);
varts2?=?tsfnz(e,?lat2,?sin2);
varts0?=?tsfnz(e,?lat0,?Math.sin(lat0));
if(Math.abs(lat1?-?lat2)?>?EPSLN)?{
ns?=?Math.log(ms1?/?ms2)?/?Math.log(ts1?/?ts2);
}else{
ns?=?sin1;
}
if(isNaN(ns))?{
ns?=?sin1;
}
f0?=?ms1?/?(ns?*?Math.pow(ts1,?ns));
rh?=?a?*?f0?*?Math.pow(ts0,?ns);
}
//經(jīng)緯度坐標(biāo)轉(zhuǎn)換蘭伯特坐標(biāo)
functionprojCood(lon,?lat)?{
lon?=?lon?/?conv;
lat?=?lat?/?conv;
if(Math.abs(2?*?Math.abs(lat)?-?Math.PI)?<=?EPSLN)?{
lat?=?sign(lat)?*?(HALF_PI?-?2?*?EPSLN);
}
varcon?=?Math.abs(Math.abs(lat)?-?HALF_PI);
varts,?rh1;
if(con?>?EPSLN)?{
ts?=?tsfnz(e,?lat,?Math.sin(lat));
rh1?=?a?*?f0?*?Math.pow(ts,?ns);
}else{
con?=?lat?*?ns;
if(con?<=?0)?{
returnnull;
}
rh1?=?0;
}
vartheta?=?ns?*?adjust_lon(lon?-?long0);
varnlon?=?(rh1?*?Math.sin(theta));
varnlat?=?(rh?-?rh1?*?Math.cos(theta));
return[nlon,?nlat];
}
//蘭伯特坐標(biāo)轉(zhuǎn)經(jīng)緯度坐標(biāo)
functioninverseProj(x1,?y1)?{
varrh1,?con,?ts;
varlat,?lon;
varx?=?x1?/?k0;
vary?=?(rh?-?y1?/?k0);
if(ns?>?0)?{
rh1?=?Math.sqrt(x?*?x?+?y?*?y);
con?=?1;
}else{
rh1?=?-Math.sqrt(x?*?x?+?y?*?y);
con?=?-1;
}
vartheta?=?0;
if(rh1?!==?0)?{
theta?=?Math.atan2((con?*?x),?(con?*?y));
}
if((rh1?!==?0)?||?(ns?>?0))?{
con?=?1?/?ns;
ts?=?Math.pow((rh1?/?(a?*?f0)),?con);
lat?=?phi2z(e,?ts);
if(lat?===?-9999)?{
returnnull;
}
}else{
lat?=?-HALF_PI;
}
lon?=?adjust_lon(theta?/?ns?+?long0);
return[lon?*?conv,?conv?*?lat];
}
對(duì)于在應(yīng)用中的其他坐標(biāo)系轉(zhuǎn)換,同樣能夠在其中進(jìn)行代碼提取。
總結(jié)
以上是生活随笔為你收集整理的proj4经纬度bl转换xy_分享proj4js中经纬度和兰伯特投影的转换代码的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 学习笔记(08):Python网络编程并
- 下一篇: 学习笔记(09):Python网络编程并