proj4是一个很好用的坐标系转换库,其丰富的坐标系类型和开发语言,让转换很方便,但是当我们只需要其中的很少坐标系相互转换,坐标量比较大的时候,就面临性能问题,鉴于在工作的使用,提取了其中的lcc和wgs84的坐标系转换,源码如下:
`var lccToWgs84 = function() {
"use strict";
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 c_lat = 33.30001;
// var c_lon = 103.3;
var long0 = 1.8029251173101;
var lat0 = 0;
var k0 = 1;
// var lat1 = 30 / conv;
// var lat2 = 60 / conv;
// var c_lat = 33.30001;
// var c_lon = 103.3;
// var long0 = c_lon / conv;
// var lat0 = c_lat / conv;
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;
};
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];
}
return {
init: init,
projCood: projCood
};
}();`