SHARE
TWEET

Untitled

a guest Jul 17th, 2017 45 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. class GridConversion {
  2.  
  3.     private function toRad ($x){
  4.       return $x * pi() / 180;
  5.     }
  6.  
  7.     private function toDeg($x){
  8.       return $x * 180 / pi();
  9.     }
  10.  
  11.     public function latLongToOSGrid($lat, $lon) {
  12.       $lat = $this->toRad($lat);
  13.       $lon = $this->toRad($lon);
  14.  
  15.  
  16.       $a = 6377563.396;
  17.       $b = 6356256.910;
  18.       $F0 = 0.9996012717;
  19.       $lat0 = $this->toRad(49);
  20.       $lon0 = $this->toRad(-2);
  21.       $N0 = -100000;
  22.       $E0 = 400000;
  23.       $e2 = 1 - ($b*$b)/($a*$a);
  24.       $n = ($a-$b)/($a+$b);
  25.       $n2 = $n*$n;
  26.       $n3 = $n*$n*$n;
  27.       $cosLat = cos($lat);
  28.       $sinLat = sin($lat);
  29.       $nu = $a*$F0/sqrt(1-$e2*$sinLat*$sinLat);
  30.       $rho = $a*$F0*(1-$e2)/pow(1-$e2*$sinLat*$sinLat, 1.5);
  31.       $eta2 = $nu/$rho-1;
  32.  
  33.       $Ma = (1 + $n + (5/4)*$n2 + (5/4)*$n3) * ($lat-$lat0);
  34.       $Mb = (3*$n + 3*$n*$n + (21/8)*$n3) * sin($lat-$lat0) * cos($lat+$lat0);
  35.       $Mc = ((15/8)*$n2 + (15/8)*$n3) * sin(2*($lat-$lat0)) * cos(2*($lat+$lat0));
  36.       $Md = (35/24)*$n3 * sin(3*($lat-$lat0)) * cos(3*($lat+$lat0));
  37.       $M = $b * $F0 * ($Ma - $Mb + $Mc - $Md);
  38.  
  39.       $cos3lat = $cosLat*$cosLat*$cosLat;
  40.       $cos5lat = $cos3lat*$cosLat*$cosLat;
  41.       $tan2lat = tan($lat)*tan($lat);
  42.       $tan4lat = $tan2lat*$tan2lat;
  43.  
  44.       $I = $M + $N0;
  45.       $II = ($nu/2)*$sinLat*$cosLat;
  46.       $III = ($nu/24)*$sinLat*$cos3lat*(5-$tan2lat+9*$eta2);
  47.       $IIIA = ($nu/720)*$sinLat*$cos5lat*(61-58*$tan2lat+$tan4lat);
  48.       $IV = $nu*$cosLat;
  49.       $V = ($nu/6)*$cos3lat*($nu/$rho-$tan2lat);
  50.       $VI = ($nu/120) * $cos5lat * (5 - 18*$tan2lat + $tan4lat + 14*$eta2 - 58*$tan2lat*$eta2);
  51.  
  52.       $dLon = $lon-$lon0;
  53.       $dLon2 = $dLon*$dLon;
  54.       $dLon3 = $dLon2*$dLon;
  55.       $dLon4 = $dLon3*$dLon;
  56.       $dLon5 = $dLon4*$dLon;
  57.       $dLon6 = $dLon5*$dLon;
  58.  
  59.       $N = $I + $II*$dLon2 + $III*$dLon4 + $IIIA*$dLon6;
  60.       $E = $E0 + $IV*$dLon + $V*$dLon3 + $VI*$dLon5;
  61.  
  62.  
  63.       $this->northing = $N;
  64.       $this->easting = $E;
  65.     }
  66.  
  67.     public function oSGridToLatLong($N, $E) {
  68.       $a = 6377563.396;
  69.       $b = 6356256.910;
  70.       $F0 = 0.9996012717;                             // NatGrid scale factor on central meridian
  71.       $lat0 = 49*pi()/180;
  72.       $lon0 = -2*pi()/180;
  73.       $N0 = -100000;
  74.       $E0 = 400000;
  75.       $e2 = 1 - ($b*$b)/($a*$a);
  76.       $n = ($a-$b)/($a+$b);
  77.       $n2 = $n*$n;
  78.       $n3 = $n*$n*$n;
  79.  
  80.       $lat = $lat0;
  81.       $M = 0;
  82.       do {
  83.         $lat = ($N-$N0-$M)/($a*$F0) + $lat;
  84.  
  85.         $Ma = (1 + $n + (5/4)*$n2 + (5/4)*$n3) * ($lat-$lat0);
  86.         $Mb = (3*$n + 3*$n*$n + (21/8)*$n3) * sin($lat-$lat0) * cos($lat+$lat0);
  87.         $Mc = ((15/8)*$n2 + (15/8)*$n3) * sin(2*($lat-$lat0)) * cos(2*($lat+$lat0));
  88.         $Md = (35/24)*$n3 * sin(3*($lat-$lat0)) * cos(3*($lat+$lat0));
  89.         $M = $b * $F0 * ($Ma - $Mb + $Mc - $Md);
  90.  
  91.       } while ($N-$N0-$M >= 0.00001);
  92.  
  93.       $cosLat = cos($lat);
  94.       $sinLat = sin($lat);
  95.       $nu = $a*$F0/sqrt(1-$e2*$sinLat*$sinLat);
  96.       $rho = $a*$F0*(1-$e2)/pow(1-$e2*$sinLat*$sinLat, 1.5);
  97.       $eta2 = $nu/$rho-1;
  98.  
  99.       $tanLat = tan($lat);
  100.       $tan2lat = $tanLat*$tanLat;
  101.       $tan4lat = $tan2lat*$tan2lat;
  102.       $tan6lat = $tan4lat*$tan2lat;
  103.       $secLat = 1/$cosLat;
  104.       $nu3 = $nu*$nu*$nu;
  105.       $nu5 = $nu3*$nu*$nu;
  106.       $nu7 = $nu5*$nu*$nu;
  107.       $VII = $tanLat/(2*$rho*$nu);
  108.       $VIII = $tanLat/(24*$rho*$nu3)*(5+3*$tan2lat+$eta2-9*$tan2lat*$eta2);
  109.       $IX = $tanLat/(720*$rho*$nu5)*(61+90*$tan2lat+45*$tan4lat);
  110.       $X = $secLat/$nu;
  111.       $XI = $secLat/(6*$nu3)*($nu/$rho+2*$tan2lat);
  112.       $XII = $secLat/(120*$nu5)*(5+28*$tan2lat+24*$tan4lat);
  113.       $XIIA = $secLat/(5040*$nu7)*(61+662*$tan2lat+1320*$tan4lat+720*$tan6lat);
  114.  
  115.       $dE = ($E-$E0);
  116.       $dE2 = $dE*$dE;
  117.       $dE3 = $dE2*$dE;
  118.       $dE4 = $dE2*$dE2;
  119.       $dE5 = $dE3*$dE2;
  120.       $dE6 = $dE4*$dE2;
  121.       $dE7 = $dE5*$dE2;
  122.       $lat = $lat - $VII*$dE2 + $VIII*$dE4 - $IX*$dE6;
  123.       $lon = $lon0 + $X*$dE - $XI*$dE3 + $XII*$dE5 - $XIIA*$dE7;
  124.  
  125.       $this->latitude = $this->toDeg($lat);
  126.       $this->longitude = $this->toDeg($lon);
  127.     }
  128. }
  129. ?>
RAW Paste Data
Top