/* Copyright (C) 2006 ALPS MAPPING K.K. */
function Dms(degree){if(typeof(degree)=="string"){this.setByDmsStr(degree);}else if(typeof(degree)=="number"){this.setByDegree(degree);}};Dms.prototype.set=function(d,m,s){this.d=d;this.m=m;this.s=s;};Dms.prototype.setByDegree=function(degree){this.d=Math.floor(degree);var temp=(degree-this.d)*60;this.m=Math.floor(temp);this.s=(temp-this.m)*60;};Dms.prototype.setByDmsStr=function(str){var spStr=str.split("/");if(spStr.length==1){this.setByDegree(parseFloat(spStr[0]));}else if(spStr.length==2){var temp=parseFloat(spStr[1]);var m=Math.floor(temp);var s=(temp - m) * 60;this.set(Math.floor(parseFloat(spStr[0])),m,s);}else if(spStr.length==3){this.set(Math.floor(parseFloat(spStr[0])),Math.floor(parseFloat(spStr[1])),parseFloat(spStr[2]));}};Dms.prototype.getDegree=function(){var degree=0;if(this.d>=0){degree=this.d+this.m/60+this.s/60/60;}else{degree=this.d-this.m/60-this.s/60/60;}return degree;};Dms.prototype.toString=function(){var d=this.d;var m=this.m;var s=Math.round(this.s*1000)/1000;if(s==60){m+=1;s=0;if(m==60){d+=1;m=0;}}return d+"/"+m+"/"+s;};function LLPoint(latitude,longitude){this.lat=0;this.lon=0;if(arguments.length==1&&typeof(arguments[0])=="string"){var sp=arguments[0].split(",");this.lat=(new Dms(sp[0])).getDegree();this.lon=(new Dms(sp[1])).getDegree();}else{if(typeof(latitude)=="object"){this.lat=latitude.getDegree();}else if(typeof(latitude)=="string"){this.lat=(new Dms(latitude)).getDegree();}else{this.lat=latitude;}if(typeof(longitude)=="object"){this.lon=longitude.getDegree();}else if(typeof(longitude)=="string"){this.lon=(new Dms(longitude)).getDegree();}else{this.lon=longitude;}}};LLPoint.prototype.set=function(latitude,longitude){this.lat=latitude;this.lon=longitude;};LLPoint.prototype.toString=function(){return (new Dms(this.lat)).toString()+","+(new Dms(this.lon)).toString();};LLPoint.prototype.distance=function(pos){var radius=6370300.0;var x1=Math.cos(this.lon*Math.PI/180.0)*Math.cos(this.lat*Math.PI/180.0);var y1=Math.sin(this.lon*Math.PI/180.0)*Math.cos(this.lat*Math.PI/180.0);var z1=Math.sin(this.lat*Math.PI/180.0);var x2=Math.cos(pos.lon*Math.PI/180.0)*Math.cos(pos.lat*Math.PI/180.0);var y2=Math.sin(pos.lon*Math.PI/180.0)*Math.cos(pos.lat*Math.PI/180.0);var z2=Math.sin(pos.lat*Math.PI/180.0);var d = x1*x2+y1*y2+z1*z2;if(d<-1) d=-1;if(d>1) d=1;return radius*Math.acos(d);};function ConvertRADtoDEGREE(radAngle){return radAngle*180.0/Math.PI;};function ConvertDEGREEtoRAD(degAngle){return degAngle*Math.PI/180.0;};function BesslWgs(mode,lpPoint) {var eps=1.0e-15;var pi=Math.PI;var ar=new Array(2);ar[0]=6377397.155;ar[1]=6378137.000;var f=new Array(2);f[0]=1.0/299.152813;f[1]=1.0/298.257223563;var e2=new Array(2);e2[0]=f[0]*(2.0-f[0]);e2[1]=f[1]*(2.0-f[1]);var dx=new Array(2);var dy=new Array(2);var dz=new Array(2);dx[0]=-146.43;dy[0]=507.89;dz[0]=681.46;dx[1]=146.43;dy[1]=-507.89;dz[1]=-681.46;var _in=mode;var _out=1-mode;var b_in=ConvertDEGREEtoRAD(lpPoint.lat);  var l_in=ConvertDEGREEtoRAD(lpPoint.lon);  var h_in=0.0; var r_in=ar[_in]/Math.sqrt(1.0-e2[_in]*Math.sin(b_in)*Math.sin(b_in));var x_in=(r_in+h_in)*Math.cos(b_in)*Math.cos(l_in);var y_in=(r_in+h_in)*Math.cos(b_in)*Math.sin(l_in);var z_in=(r_in*(1.0-e2[_in])+h_in)*Math.sin(b_in);var x=x_in+dx[_in];var y=y_in+dy[_in];var z=z_in+dz[_in];var p=Math.sqrt(x*x+y*y);var mu0=z/p;var aa=Math.sqrt((1.0-e2[_out])*mu0*mu0+1.0);var aa3=aa*aa*aa;var ff=mu0-z/p-ar[_out]*e2[_out]*mu0/p/aa;var df=1.0-ar[_out]*e2[_out]/p/aa3;var mu1=mu0-ff/df;var aas;for(i=0;(ff>eps||-eps>ff)&&i<10;i++){mu0=mu1;aa=Math.sqrt((1.0-e2[_out])*mu0*mu0+1.0);aa3=aa*aa*aa;aas=ar[_out]*e2[_out]/p;ff=mu0-z/p-aas*mu0/aa;df=1.0-aas/aa3;mu1=mu0-ff/df;}var b_out=Math.atan2(mu1,1.0);var l_out=Math.atan2(y,x);var r_out=ar[_out]/Math.sqrt(1.0-e2[_out]*Math.sin(b_out)*Math.sin(b_out));return new LLPoint(ConvertRADtoDEGREE(b_out),ConvertRADtoDEGREE(l_out));};
