gcoord.js 5.1 KB

12345
  1. /* @preserve
  2. * gcoord 0.1.1, a JS library for transform geographical coordinates.
  3. * Copyright (c) 2018 Jiulong Hu <me@hujiulong.com>
  4. */
  5. !function(t,e){"object"==typeof exports&&"undefined"!=typeof module?e(exports):"function"==typeof define&&define.amd?define(["exports"],e):e(t.gcoord={})}(this,function(t){"use strict";var e="WGS84",r="GCJ02",n="BD09",o="EPSG3857";function i(t,e){if(t)throw new Error(e)}function a(t){return!isNaN(t)&&null!==t&&!u(t)}function u(t){return!!t&&"[object Array]"===Object.prototype.toString.call(t)}function l(){var t=arguments,e=t.length-1;return function(){for(var r=e,n=t[e].apply(this,arguments);r--;)n=t[r].call(this,n);return n}}var f="function"==typeof Symbol&&"symbol"==typeof Symbol.iterator?function(t){return typeof t}:function(t){return t&&"function"==typeof Symbol&&t.constructor===Symbol&&t!==Symbol.prototype?"symbol":typeof t},s=function(){return function(t,e){if(Array.isArray(t))return t;if(Symbol.iterator in Object(t))return function(t,e){var r=[],n=!0,o=!1,i=void 0;try{for(var a,u=t[Symbol.iterator]();!(n=(a=u.next()).done)&&(r.push(a.value),!e||r.length!==e);n=!0);}catch(t){o=!0,i=t}finally{try{!n&&u.return&&u.return()}finally{if(o)throw i}}return r}(t,e);throw new TypeError("Invalid attempt to destructure non-iterable instance")}}(),c=Math.sin,y=Math.cos,d=Math.sqrt,v=Math.abs,h=Math.PI,g=6378245,b=.006693421622965823;function m(t){var e=s(t,2),r=e[0],n=e[1];if(!S(r,n))return[r,n];for(var o=r,i=n,a=p([o,i]),u=a[0]-r,l=a[1]-n;v(u)>1e-6||v(l)>1e-6;)u=(a=p([o-=u,i-=l]))[0]-r,l=a[1]-n;return[o,i]}function p(t){var e=s(t,2),r=e[0],n=e[1];if(!S(r,n))return[r,n];var o,i,a,u,l,f,m,p,G,M,P,E,B,C=(B=300+(P=(o=r)-105)+2*(E=(i=n)-35)+.1*P*P+.1*P*E+.1*d(v(P)),B+=2*(20*c(6*P*h)+20*c(2*P*h))/3,B+=2*(20*c(P*h)+40*c(P/3*h))/3,a=B+=2*(150*c(P/12*h)+300*c(P/30*h))/3,M=2*(p=o-105)-100+3*(G=i-35)+.2*G*G+.1*p*G+.2*d(v(p)),M+=2*(20*c(6*p*h)+20*c(2*p*h))/3,M+=2*(20*c(G*h)+40*c(G/3*h))/3,u=M+=2*(160*c(G/12*h)+320*c(G*h/30))/3,f=c(l=i/180*h),m=d(f=1-b*f*f),[a=180*a/(g/m*y(l)*h),u=180*u/(g*(1-b)/(f*m)*h)]);return[r+C[0],n+C[1]]}function S(t,e){return i(void 0===t||void 0===e,"lon and lat are required"),i(!a(t)||!a(e),"lon and lat must be numbers"),t>=72.004&&t<=137.8347&&e>=.8293&&e<=55.8271}var G=Math.sin,M=Math.cos,P=Math.atan2,E=Math.sqrt,B=3e3*Math.PI/180;function C(t){var e=s(t,2),r=e[0]-.0065,n=e[1]-.006,o=E(r*r+n*n)-2e-5*G(n*B),i=P(n,r)-3e-6*M(r*B);return[o*M(i),o*G(i)]}function W(t){var e=s(t,2),r=e[0],n=e[1],o=E(r*r+n*n)+2e-5*G(n*B),i=P(n,r)+3e-6*M(r*B);return[o*M(i)+.0065,o*G(i)+.006]}var j=180/Math.PI,w=Math.PI/180,D=6378137,J=20037508.342789244;function k(t){return[t[0]*j/D,(.5*Math.PI-2*Math.atan(Math.exp(-t[1]/D)))*j]}function I(t){var e=Math.abs(t[0])<=180?t[0]:t[0]-360*(t[0]<0?-1:1),r=[D*e*w,D*Math.log(Math.tan(.25*Math.PI+.5*t[1]*w))];return r[0]>J&&(r[0]=J),r[0]<-J&&(r[0]=-J),r[1]>J&&(r[1]=J),r[1]<-J&&(r[1]=-J),r}var q={to:{GCJ02:p,BD09:l(W,p),EPSG3857:I}},O={to:{WGS84:m,BD09:W,EPSG3857:l(I,m)}},x={to:{WGS84:l(m,C),GCJ02:C,EPSG3857:l(I,m,C)}},A={to:{WGS84:k,GCJ02:l(p,k),BD09:l(W,p,k)}},L=(Object.freeze||Object)({WGS84:q,GCJ02:O,BD09:x,EPSG3857:A});t.transform=function(t,e,r){i(!t,"coordinate is required"),i(!e,"original coordinate system is required"),i(!r,"target coordinate system is required");var n=L[e];i(!n,"original coordinate system is invalid");var o=n.to[r];i(!o,"target coordinate system is invalid");var l=void 0===t?"undefined":f(t);i("string"!==l&&"object"!==l,"coordinate must be an geojson or an array of position"),"string"===l&&(t=JSON.parse(t));var s=!1;u(t)&&(i(t.length<2,"position must be at 2 numbers long"),i(!a(t[0])||!a(t[1]),"position must contain numbers"),s=!0);var c=null,y=o;return s?c=y(t):(function t(e,r,n){if(null!==e)for(var o=void 0,i=void 0,a=void 0,u=void 0,l=void 0,f=void 0,s=void 0,c=0,y=0,d=void 0,v=e.type,h="FeatureCollection"===v,g="Feature"===v,b=h?e.features.length:1,m=0;m<b;m++){l=(d=!!(s=h?e.features[m].geometry:g?e.geometry:e)&&"GeometryCollection"===s.type)?s.geometries.length:1;for(var p=0;p<l;p++){var S=0,G=0;if(null!==(u=d?s.geometries[p]:s)){f=u.coordinates;var M=u.type;switch(c=!n||"Polygon"!==M&&"MultiPolygon"!==M?0:1,M){case null:break;case"Point":if(!1===r(f,y,m,S,G))return!1;y++,S++;break;case"LineString":case"MultiPoint":for(o=0;o<f.length;o++){if(!1===r(f[o],y,m,S,G))return!1;y++,"MultiPoint"===M&&S++}"LineString"===M&&S++;break;case"Polygon":case"MultiLineString":for(o=0;o<f.length;o++){for(i=0;i<f[o].length-c;i++){if(!1===r(f[o][i],y,m,S,G))return!1;y++}"MultiLineString"===M&&S++,"Polygon"===M&&G++}"Polygon"===M&&S++;break;case"MultiPolygon":for(o=0;o<f.length;o++){for(G=0,i=0;i<f[o].length;i++){for(a=0;a<f[o][i].length-c;a++){if(!1===r(f[o][i][a],y,m,S,G))return!1;y++}G++}S++}break;case"GeometryCollection":for(o=0;o<u.geometries.length;o++)if(!1===t(u.geometries[o],r,n))return!1;break;default:throw new Error("Unknown Geometry Type")}}}}}(t,function(t){var e=y(t);t[0]=e[0],t[1]=e[1]}),c=t),c},t.WGS84=e,t.WGS1984="WGS84",t.GCJ02=r,t.BD09=n,t.EPSG4326="WGS84",t.EPSG3857=o,t.EPSG900913="EPSG3857",t.Baidu="BD09",t.BMap="BD09",t.AMap="GCJ02",t.WebMercator="EPSG3857",Object.defineProperty(t,"__esModule",{value:!0})});