Attachment 'geotrans.php.txt'

Download

   1 <?php
   2 	class GeoTrans{
   3 		var $srctype;
   4 		var $dsttype;
   5 		var $m_Ind, $m_Es, $m_Esp, $src_m, $dst_m;
   6 
   7 		var $EPSLN = 0.0000000001;
   8 		var $m_arMajor = 6378137.0;
   9 		var $m_arMinor = 6356752.3142;
  10 
  11 		var $m_arScaleFactor = array();
  12 		var $m_arLonCenter = array();
  13 		var $m_arLatCenter = array();
  14 		var $m_arFalseNorthing = array();
  15 		var $m_arFalseEasting = array();
  16 
  17 
  18 		function GeoTrans($srctype = "katec", $dsttype = "geo")
  19 		{
  20 			$this->m_arScaleFactor["geo"] = 1;
  21 			$this->m_arLonCenter["geo"] = 0.0;
  22 			$this->m_arLatCenter["geo"] = 0.0;
  23 			$this->m_arFalseNorthing["geo"] = 0.0;
  24 			$this->m_arFalseEasting["geo"] = 0.0;
  25 
  26 			$this->m_arScaleFactor["katec"] = 0.9999;
  27 			$this->m_arLonCenter["katec"] = 2.23402144255274;
  28 			$this->m_arLatCenter["katec"] = 0.663225115757845;
  29 			$this->m_arFalseNorthing["katec"] = 600000.0;
  30 			$this->m_arFalseEasting["katec"] = 400000.0;
  31 
  32 			$this->srctype = $srctype;
  33 			$this->dsttype = $dsttype;
  34 
  35 			$temp = $this->m_arMinor / $this->m_arMajor;
  36 			$this->m_Es = 1.0 - $temp * $temp;
  37 			$this->m_Esp = $this->m_Es / (1.0 - $this->m_Es);
  38 
  39 			if ($this->m_Es < 0.00001) {
  40 				$this->m_Ind = 1.0;
  41 			}
  42 			else {
  43 				$this->m_Ind = 0.0;
  44 			}
  45 
  46 			$this->src_m = $this->m_arMajor * $this->mlfn($this->e0fn($this->m_Es), $this->e1fn($this->m_Es), $this->e2fn($this->m_Es), $this->e3fn($this->m_Es), $this->m_arLatCenter[$srctype]);
  47 			$this->dst_m = $this->m_arMajor * $this->mlfn($this->e0fn($this->m_Es), $this->e1fn($this->m_Es), $this->e2fn($this->m_Es), $this->e3fn($this->m_Es), $this->m_arLatCenter[$dsttype]);
  48 		}
  49 
  50 		function D2R($degree)
  51 		{
  52 			return $degree * M_PI / 180.0;
  53 		}
  54 
  55 		function R2D($radian)
  56 		{
  57 			return $radian * 180.0 / M_PI;
  58 		}
  59 
  60 		function e0fn($x)
  61 		{
  62 			return 1.0 - 0.25 * $x * (1.0 + $x / 16.0 * (3.0 + 1.25 * $x));
  63 		}
  64 
  65 		function e1fn($x)
  66 		{
  67 			return 0.375 * $x * (1.0 + 0.25 * $x * (1.0 + 0.46875 * $x));
  68 		}
  69 
  70 		function e2fn($x)
  71 		{
  72 			return 0.05859375 * $x * $x * (1.0 + 0.75 * $x);
  73 		}
  74 
  75 		function e3fn($x)
  76 		{
  77 			return $x * $x * $x * (35.0 / 3072.0);
  78 		}
  79 
  80 		function mlfn($e0, $e1, $e2, $e3, $phi)
  81 		{
  82 			return $e0 * $phi - $e1 * sin(2.0 * $phi) + $e2 * sin(4.0 * $phi) - $e3 * sin(6.0 * $phi);
  83 		}
  84 
  85 		function asinz($value)
  86 		{
  87 			if (abs($value) > 1.0) $value = ($value > 0 ? 1 : -1);
  88 			return asin($value);
  89 		}
  90 
  91 		function conv($in_x, $in_y, &$out_x, &$out_y)
  92 		{
  93 			if ($this->srctype == "geo") {
  94 				$inlon = $this->D2R($in_x);
  95 				$inlat = $this->D2R($in_y);
  96 			}
  97 			else {
  98 				$this->tm2geo($in_x, $in_y, $inlon, $inlat);
  99 			}
 100 
 101 			$outlon = $inlon;
 102 			$outlat = $inlat;
 103 
 104 			if ($this->dsttype == "geo") {
 105 				$out_x = $this->R2D($outlon);
 106 				$out_y = $this->R2D($outlat);
 107 			}
 108 			else {
 109 				$this->geo2tm($outlon, $outlat, $out_x, $out_y);
 110 				$out_x = round($out_x);
 111 				$out_y = round($out_y);
 112 			}
 113 		}
 114 
 115 		function geo2tm($lon, $lat, &$x, &$y)
 116 		{
 117 			$delta_lon = $lon - $this->m_arLonCenter[$this->dsttype];
 118 			$sin_phi = sin($lat);
 119 			$cos_phi = cos($lat);
 120 
 121 			if ($this->m_Ind != 0) {
 122 				$b = $cos_phi * sin($delta_lon);
 123 
 124 				if ((abs(abs($b) - 1.0)) < $this->EPSLN) {
 125 					echo ("geo2tm: 무한대 에러");
 126 				}
 127 			}
 128 			else {
 129 				$b = 0;
 130 				$x = 0.5 * $this->m_arMajor * $this->m_arScaleFactor[$this->dsttype] * log((1.0 + $b) / (1.0 - $b));
 131 				$con = acos($cos_phi * cos($delta_lon) / sqrt(1.0 - $b * $b));
 132 
 133 				if ($lat < 0) {
 134 					$con = $con * -1;
 135 					$y = $this->m_arMajor * $this->m_arScaleFactor[$this->dsttype] * ($con - $this->m_arLatCenter[$this->dsttype]);
 136 				}
 137 			}
 138 
 139 			$al = $cos_phi * $delta_lon;
 140 			$als = $al * $al;
 141 			$c = $this->m_Esp * $cos_phi * $cos_phi;
 142 			$tq = tan($lat);
 143 			$t = $tq * $tq;
 144 			$con = 1.0 - $this->m_Es * $sin_phi * $sin_phi;
 145 			$n = $this->m_arMajor / sqrt($con);
 146 			$ml = $this->m_arMajor * $this->mlfn($this->e0fn($this->m_Es), $this->e1fn($this->m_Es), $this->e2fn($this->m_Es), $this->e3fn($this->m_Es), $lat);
 147 
 148 			$x = $this->m_arScaleFactor[$this->dsttype] * $n * $al * (1.0 + $als / 6.0 * (1.0 - $t + $c + $als / 20.0 * (5.0 - 18.0 * $t + $t * $t + 72.0 * $c - 58.0 * $this->m_Esp))) + $this->m_arFalseEasting[$this->dsttype];
 149 			$y = $this->m_arScaleFactor[$this->dsttype] * ($ml - $this->dst_m + $n * $tq * ($als * (0.5 + $als / 24.0 * (5.0 - $t + 9.0 * $c + 4.0 * $c * $c + $als / 30.0 * (61.0 - 58.0 * $t + $t * $t + 600.0 * $c - 330.0 * $this->m_Esp))))) + $this->m_arFalseNorthing[$this->dsttype];
 150 		}
 151 
 152 		function tm2geo($x, $y, &$lon, &$lat)
 153 		{
 154 			$max_iter = 6;
 155 
 156 			if ($this->m_Ind != 0) {
 157 				$f = exp($x / ($this->m_arMajor * $this->m_arScaleFactor[$this->srctype]));
 158 				$g = 0.5 * ($f - 1.0 / $f);
 159 				$temp = $this->m_arLatCenter[$this->srctype] + $y / ($this->m_arMajor * $this->m_arScaleFactor[$this->srctype]);
 160 				$h = cos($temp);
 161 				$con = sqrt((1.0 - $h * $h) / (1.0 + $g * $g));
 162 				$lat = asinz($con);
 163 
 164 				if ($temp < 0) $lat *= -1;
 165 
 166 				if (($g == 0) && ($h == 0))
 167 					$lon = $this->m_arLonCenter[$this->srctype];
 168 				else
 169 					$lon = atan($g / $h) + $this->m_arLonCenter[$this->srctype];
 170 			}
 171 
 172 			$x -= $this->m_arFalseEasting[$this->srctype];
 173 			$y -= $this->m_arFalseNorthing[$this->srctype];
 174 
 175 			$con = ($this->src_m + $y / $this->m_arScaleFactor[$this->srctype]) / $this->m_arMajor;
 176 			$phi = $con;
 177 
 178 			$i = 0;
 179 
 180 			while (true) {
 181 				$delta_Phi = (($con + $this->e1fn($this->m_Es) * sin(2.0 * $phi) - $this->e2fn($this->m_Es) * sin(4.0 * $phi) + $this->e3fn($this->m_Es) * sin(6.0 * $phi)) / $this->e0fn($this->m_Es)) - $phi;
 182 				$phi = $phi + $delta_Phi;
 183 				if (abs($delta_Phi) <= $this->EPSLN) break;
 184 
 185 				if ($i >= $max_iter) 
 186 					echo ("tm2geo: 무한대 에러");
 187 
 188 				$i++;
 189 			}
 190 
 191 			if (abs($phi) < (M_PI / 2)) {
 192 				$sin_phi = sin($phi);
 193 				$cos_phi = cos($phi);
 194 				$tan_phi = tan($phi);
 195 				$c = $this->m_Esp * $cos_phi * $cos_phi;
 196 				$cs = $c * $c;
 197 				$t = $tan_phi * $tan_phi;
 198 				$ts = $t * $t;
 199 				$con = 1.0 - $this->m_Es * $sin_phi * $sin_phi;
 200 				$n = $this->m_arMajor / sqrt($con);
 201 				$r = $n * (1.0 - $this->m_Es) / $con;
 202 				$d = $x / ($n * $this->m_arScaleFactor[$this->srctype]);
 203 				$ds = $d * $d;
 204 				$lat = $phi - ($n * $tan_phi * $ds / $r) * (0.5 - $ds / 24.0 * (5.0 + 3.0 * $t + 10.0 * $c - 4.0 * $cs - 9.0 * $this->m_Esp - $ds / 30.0 * (61.0 + 90.0 * $t + 298.0 * $c + 45.0 * $ts - 252.0 * $this->m_Esp - 3.0 * $cs)));
 205 				$lon = $this->m_arLonCenter[$this->srctype] + ($d * (1.0 - $ds / 6.0 * (1.0 + 2.0 * $t + $c - $ds / 20.0 * (5.0 - 2.0 * $c + 28.0 * $t - 3.0 * $cs + 8.0 * $this->m_Esp + 24.0 * $ts))) / $cos_phi);
 206 			}
 207 			else {
 208 				$lat = M_PI * 0.5 * sin($y);
 209 				$lon = $this->m_arLonCenter[$this->srctype];
 210 			}
 211 		}
 212 
 213 		function getDistancebyGeo($lon1, $lat1, $lon2, $lat2)
 214 		{
 215 			$lat1 = $this->D2R($lat1);
 216 			$lon1 = $this->D2R($lon1);
 217 			$lat2 = $this->D2R($lat2);
 218 			$lon2 = $this->D2R($lon2);
 219 
 220 			$longitude = $lon2 - $lon1;
 221 			$latitude  = $lat2 - $lat1;
 222 
 223 			$a = pow(sin($latitude / 2.0), 2) + cos($lat1) * cos($lat2) * pow(sin($longitude / 2.0), 2);
 224 			return 6376.5 * 2.0 * atan2(sqrt($a), sqrt(1.0 - $a));
 225 		}
 226 
 227 		function getDistancebyKatec($x1, $y1, $x2, $y2)
 228 		{
 229 			$geo = new GeoTrans;
 230 			$geo->conv($x1, $y1, $lon1, $lat1);
 231 			$geo->conv($x2, $y2, $lon2, $lat2);
 232 
 233 			return $this->getDistancebyGeo($lon1, $lat1, $lon2, $lat2);
 234 		}
 235 
 236 		function getTimebySec($distance)
 237 		{
 238 			return round(3600 * $distance / 4);
 239 		}
 240 
 241 		function getTimebyMin($distance)
 242 		{
 243 			return (int)ceil($this->getTimebySec($distance) / 60);
 244 		}
 245 	}
 246 ?>

Attached Files

To refer to attachments on a page, use attachment:filename, as shown below in the list of files. Do NOT use the URL of the [get] link, since this is subject to change and can break easily.
  • [get | view] (2011-11-25 19:28:41, 7.4 KB) [[attachment:geotrans.php.txt]]
 All files | Selected Files: delete move to page copy to page

You are not allowed to attach a file to this page.

web biohackers.net