gcoord
Version:
geographic coordinate library
7 lines (6 loc) • 7.26 kB
JavaScript
/**
* @preserve
* gcoord 1.0.7, geographic coordinate library
* Copyright (c) 2025 Jiulong Hu <me@hujiulong.com>
*/
const{sin:t,cos:e,sqrt:n,abs:r,PI:o}=Math,i=6378245,a=.006693421622965823;function c(t,e){return t>=72.004&&t<=137.8347&&e>=.8293&&e<=55.8271}function u(c,u){let s=function(e,i){let a=300+e+2*i+.1*e*e+.1*e*i+.1*n(r(e));return a+=2*(20*t(6*e*o)+20*t(2*e*o))/3,a+=2*(20*t(e*o)+40*t(e/3*o))/3,a+=2*(150*t(e/12*o)+300*t(e/30*o))/3,a}(c-105,u-35),l=function(e,i){let a=2*e-100+3*i+.2*i*i+.1*e*i+.2*n(r(e));return a+=2*(20*t(6*e*o)+20*t(2*e*o))/3,a+=2*(20*t(i*o)+40*t(i/3*o))/3,a+=2*(160*t(i/12*o)+320*t(i*o/30))/3,a}(c-105,u-35);const f=u/180*o;let g=t(f);g=1-a*g*g;const h=n(g);return s=180*s/(i/h*e(f)*o),l=180*l/(i*(1-a)/(g*h)*o),[s,l]}function s(t){const[e,n]=t;if(!c(e,n))return[e,n];const r=u(e,n);return[e+r[0],n+r[1]]}function l(t){const[e,n]=t;if(!c(e,n))return[e,n];let[o,i]=[e,n],a=s([o,i]),u=a[0]-e,l=a[1]-n;for(;r(u)>1e-6||r(l)>1e-6;)o-=u,i-=l,a=s([o,i]),u=a[0]-e,l=a[1]-n;return[o,i]}const{sin:f,cos:g,atan2:h,sqrt:M,PI:G}=Math,S=3e3*G/180;function d(t){const[e,n]=t,r=e-.0065,o=n-.006,i=M(r*r+o*o)-2e-5*f(o*S),a=h(o,r)-3e-6*g(r*S);return[i*g(a),i*f(a)]}function P(t){const[e,n]=t,r=e,o=n,i=M(r*r+o*o)+2e-5*f(o*S),a=h(o,r)+3e-6*g(r*S);return[i*g(a)+.0065,i*f(a)+.006]}const p=180/Math.PI,y=Math.PI/180,b=6378137,B=20037508.342789244;function C(t){return[t[0]*p/b,(.5*Math.PI-2*Math.atan(Math.exp(-t[1]/b)))*p]}function D(t){const e=Math.abs(t[0])<=180?t[0]:t[0]-360*(t[0]<0?-1:1),n=[b*e*y,b*Math.log(Math.tan(.25*Math.PI+.5*t[1]*y))];return n[0]>B&&(n[0]=B),n[0]<-B&&(n[0]=-B),n[1]>B&&(n[1]=B),n[1]<-B&&(n[1]=-B),n}const{abs:m}=Math,E=[12890594.86,8362377.87,5591021,3481989.83,1678043.12,0],w=[75,60,45,30,15,0],I=[[1.410526172116255e-8,898305509648872e-20,-1.9939833816331,200.9824383106796,-187.2403703815547,91.6087516669843,-23.38765649603339,2.57121317296198,-.03801003308653,17337981.2],[-7.435856389565537e-9,8983055097726239e-21,-.78625201886289,96.32687599759846,-1.85204757529826,-59.36935905485877,47.40033549296737,-16.50741931063887,2.28786674699375,10260144.86],[-3.030883460898826e-8,898305509983578e-20,.30071316287616,59.74293618442277,7.357984074871,-25.38371002664745,13.45380521110908,-3.29883767235584,.32710905363475,6856817.37],[-1.981981304930552e-8,8983055099779535e-21,.03278182852591,40.31678527705744,.65659298677277,-4.44255534477492,.85341911805263,.12923347998204,-.04625736007561,4482777.06],[3.09191371068437e-9,8983055096812155e-21,6995724062e-14,23.10934304144901,-.00023663490511,-.6321817810242,-.00663494467273,.03430082397953,-.00466043876332,2555164.4],[2.890871144776878e-9,8983055095805407e-21,-3.068298e-8,7.47137025468032,-353937994e-14,-.02145144861037,-1234426596e-14,.00010322952773,-323890364e-14,826088.5]],W=[[-.0015702102444,111320.7020616939,0x60e374c3105a3,-0x24bb4115e2e164,0x5cc55543bb0ae8,-0x7ce070193f3784,0x5e7ca61ddf8150,-0x261a578d8b24d0,0x665d60f3742ca,82.5],[.0008277824516172526,111320.7020463578,647795574.6671607,-4082003173.641316,10774905663.51142,-15171875531.51559,12053065338.62167,-5124939663.577472,913311935.9512032,67.5],[.00337398766765,111320.7020202162,4481351.045890365,-23393751.19931662,79682215.47186455,-115964993.2797253,97236711.15602145,-43661946.33752821,8477230.501135234,52.5],[.00220636496208,111320.7020209128,51751.86112841131,3796837.749470245,992013.7397791013,-1221952.21711287,1340652.697009075,-620943.6990984312,144416.9293806241,37.5],[-.0003441963504368392,111320.7020576856,278.2353980772752,2485758.690035394,6070.750963243378,54821.18345352118,9540.606633304236,-2710.55326746645,1405.483844121726,22.5],[-.0003218135878613132,111320.7020701615,.00369383431289,823725.6402795718,.46104986909093,2351.343141331292,1.58060784298199,8.77738589078284,.37238884252424,7.45]];function k(t,e,n){const r=m(e)/n[9];let o=n[0]+n[1]*m(t),i=n[2]+n[3]*r+n[4]*Math.pow(r,2)+n[5]*Math.pow(r,3)+n[6]*Math.pow(r,4)+n[7]*Math.pow(r,5)+n[8]*Math.pow(r,6);return o*=t<0?-1:1,i*=e<0?-1:1,[o,i]}function x(t){const[e,n]=t;let r=[];for(let o=0;o<w.length;o++)if(m(n)>w[o]){r=W[o];break}return k(e,n,r)}function J(t){const[e,n]=t;let r=[];for(let o=0;o<E.length;o++)if(m(n)>=E[o]){r=I[o];break}return k(e,n,r)}function v(t,e){if(!t)throw new Error(e)}function L(t){return!!t&&"[object Array]"===Object.prototype.toString.call(t)}function $(t){return!isNaN(Number(t))&&null!==t&&!L(t)}function j(...t){const e=t.length-1;return function(...n){let r=e,o=t[e].apply(null,n);for(;r--;)o=t[r].call(null,o);return o}}function q(t,e,n=!1){if(null===t)return;let r,o,i,a,c,u,s,l,f=0,g=0;const{type:h}=t,M="FeatureCollection"===h,G="Feature"===h,S=M?t.features.length:1;for(let d=0;d<S;d++){s=M?t.features[d].geometry:G?t.geometry:t,l=!!s&&"GeometryCollection"===s.type,u=l?s.geometries.length:1;for(let t=0;t<u;t++){let u=0,h=0;if(a=l?s.geometries[t]:s,null===a)continue;const M=a.type;switch(f=!n||"Polygon"!==M&&"MultiPolygon"!==M?0:1,M){case null:break;case"Point":if(c=a.coordinates,!1===e(c,g,d,u,h))return!1;g++,u++;break;case"LineString":case"MultiPoint":for(c=a.coordinates,r=0;r<c.length;r++){if(!1===e(c[r],g,d,u,h))return!1;g++,"MultiPoint"===M&&u++}"LineString"===M&&u++;break;case"Polygon":case"MultiLineString":for(c=a.coordinates,r=0;r<c.length;r++){for(o=0;o<c[r].length-f;o++){if(!1===e(c[r][o],g,d,u,h))return!1;g++}"MultiLineString"===M&&u++,"Polygon"===M&&h++}"Polygon"===M&&u++;break;case"MultiPolygon":for(c=a.coordinates,r=0;r<c.length;r++){for(h=0,o=0;o<c[r].length;o++){for(i=0;i<c[r][o].length-f;i++){if(!1===e(c[r][o][i],g,d,u,h))return!1;g++}h++}u++}break;case"GeometryCollection":for(r=0;r<a.geometries.length;r++)if(!1===q(a.geometries[r],e,n))return!1;break;default:throw new Error("Unknown Geometry Type")}}}}var N;!function(t){t.WGS84="WGS84",t.WGS1984="WGS84",t.EPSG4326="WGS84",t.GCJ02="GCJ02",t.AMap="GCJ02",t.BD09="BD09",t.BD09LL="BD09",t.Baidu="BD09",t.BMap="BD09",t.BD09MC="BD09MC",t.BD09Meter="BD09MC",t.EPSG3857="EPSG3857",t.EPSG900913="EPSG3857",t.EPSG102100="EPSG3857",t.WebMercator="EPSG3857",t.WM="EPSG3857"}(N||(N={}));var T={WGS84:{to:{[N.GCJ02]:s,[N.BD09]:j(P,s),[N.BD09MC]:j(x,P,s),[N.EPSG3857]:D}},GCJ02:{to:{[N.WGS84]:l,[N.BD09]:P,[N.BD09MC]:j(x,P),[N.EPSG3857]:j(D,l)}},BD09:{to:{[N.WGS84]:j(l,d),[N.GCJ02]:d,[N.EPSG3857]:j(D,l,d),[N.BD09MC]:x}},EPSG3857:{to:{[N.WGS84]:C,[N.GCJ02]:j(s,C),[N.BD09]:j(P,s,C),[N.BD09MC]:j(x,P,s,C)}},BD09MC:{to:{[N.WGS84]:j(l,d,J),[N.GCJ02]:j(d,J),[N.EPSG3857]:j(D,l,d,J),[N.BD09]:J}}};const O=Object.assign(Object.assign({},N),{CRSTypes:N,transform:function(t,e,n){if(v(!!t,"The args[0] input coordinate is required"),v(!!e,"The args[1] original coordinate system is required"),v(!!n,"The args[2] target coordinate system is required"),e===n)return t;const r=T[e];v(!!r,`Invalid original coordinate system: ${e}`);const o=r.to[n];v(!!o,`Invalid target coordinate system: ${n}`);const i=typeof t;if(v("string"===i||"object"===i,`Invalid input coordinate type: ${i}`),"string"===i)try{t=JSON.parse(t)}catch(u){throw new Error(`Invalid input coordinate: ${t}`)}let a=!1;L(t)&&(v(t.length>=2,`Invalid input coordinate: ${t}`),v($(t[0])&&$(t[1]),`Invalid input coordinate: ${t}`),t=t.map(Number),a=!0);const c=o;return a?c(t):(q(t,(t=>{[t[0],t[1]]=c(t)})),t)}});export{O as default};