js蘭伯特和經緯度轉換(提取自proj4js)

來源:互聯網
上載者:User

標籤:js   座標轉換   蘭伯特   proj4   

       技術交流群:665060698

       proj4是一個非常有用的座標轉換類庫,有各種語言版本的,C++,java,js,python版等,可以很方便的將座標從一個座標系轉換到另一個

座標系。

       在網頁端使用的時候,轉換大量的座標時,發現存在效能有問題,就查看了一下proj4js的原始碼,發現初始化很多不相關的類型,對象等,

基於現有的項目,就進行相關代碼的提取,以下是提取的蘭伯特和經緯度座標的轉換,簡略代碼說明如下:

        //初始化常用的變數,直接換算成弧度,提升計算效能        var EPSLN = (typeof Number.EPSILON === 'undefined') ? 1.0e-10 : Number.EPSILON;var conv = 180 / Math.PI;var HALF_PI = Math.PI / 2;var SPI = 3.14159265359;var TWO_PI = 2 * Math.PI;var a = 6378137;var b = 6356752.314245179;var e = 0.08181919084262157;var lat1 = 0.52359877559829;var lat2 = 1.04719755119659; var long0 = 1.8029251173101;var lat0 = 0;var k0 = 1; var ns;var f0;var rh;//常用的轉換參數,直接提取引用var tsfnz = function(eccent, phi, sinphi) {var con = eccent * sinphi;var com = 0.5 * eccent;con = Math.pow(((1 - con) / (1 + con)), com);return(Math.tan(0.5 * (HALF_PI - phi)) / con);};var sign = function(x) {return x < 0 ? -1 : 1;};var msfnz = function(eccent, sinphi, cosphi) {var con = eccent * sinphi;return cosphi / (Math.sqrt(1 - con * con));};var adjust_lon = function(x) {return(Math.abs(x) <= SPI) ? x : (x - (sign(x) * TWO_PI));};var phi2z = function(eccent, ts) {var eccnth = 0.5 * eccent;var con, dphi;var phi = HALF_PI - 2 * Math.atan(ts);for(var i = 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) {return phi;}}//console.log("phi2z has NoConvergence");return -9999;};//根據proj4的座標系描述字串,解析其中的參數function init(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;}})}var sin1 = Math.sin(lat1);var cos1 = Math.cos(lat1);var ms1 = msfnz(e, sin1, cos1);var ts1 = tsfnz(e, lat1, sin1);var sin2 = Math.sin(lat2);var cos2 = Math.cos(lat2);var ms2 = msfnz(e, sin2, cos2);var ts2 = tsfnz(e, lat2, sin2);var ts0 = 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);}//經緯度座標轉蘭伯特座標function projCood(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);}var con = Math.abs(Math.abs(lat) - HALF_PI);var ts, 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) {return null;}rh1 = 0;}var theta = ns * adjust_lon(lon - long0);var nlon = (rh1 * Math.sin(theta));var nlat = (rh - rh1 * Math.cos(theta));return [nlon, nlat];}//蘭伯特座標轉經緯度座標function inverseProj(x1, y1) {var rh1, con, ts;var lat, lon;var x = x1 / k0;var y = (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;}var theta = 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) {return null;}} else {lat = -HALF_PI;}lon = adjust_lon(theta / ns + long0);return [lon * conv, conv * lat];}


js蘭伯特和經緯度轉換(提取自proj4js)

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.