EpsgConversion.php 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368
  1. <?php
  2. class Complex {
  3. private $real = 0;
  4. private $imag = 0;
  5. public function __construct($real = 0, $imag = 0) {
  6. if ($real instanceof Complex) {
  7. $this->real = $real->real;
  8. $this->imag = $real->imag;
  9. } elseif (is_scalar($real) && is_scalar($imag)) {
  10. $this->real = (float)$real;
  11. $this->imag = (float)$imag;
  12. } else throw new Exception("Bad argument(s)");
  13. }
  14. public function abs() {
  15. return sqrt($this->real * $this->real - $this->imag * $this->imag);
  16. }
  17. public function add($c) {
  18. $c = new Complex($c);
  19. return new Complex(
  20. $this->real + $c->real,
  21. $this->imag + $c->imag
  22. );
  23. }
  24. public function multiply($c) {
  25. $c = new Complex($c);
  26. return new Complex(
  27. $this->real * $c->real - $this->imag * $c->imag,
  28. $this->real * $c->imag + $this->imag * $c->real
  29. );
  30. }
  31. public function real() {
  32. return $this->real;
  33. }
  34. public function imag() {
  35. return $this->imag;
  36. }
  37. }
  38. class EpsgConversion {
  39. private static $fe = 500000;
  40. private static function CalculateESquared($a, $b) {
  41. return (pow($a, 2) - pow($b, 2)) / pow($a, 2);
  42. }
  43. private static function CalculateE2Squared($a, $b) {
  44. return (pow($a, 2) - pow($b, 2)) / pow($b, 2);
  45. }
  46. private static function denom ($es, $sphi) {
  47. $sinSphi = sin($sphi);
  48. return sqrt(1 - $es * pow($sinSphi, 2));
  49. }
  50. private static function sphsr ($a, $es, $sphi) {
  51. $dn = self::denom($es, $sphi);
  52. return $a * (1 - $es) / pow($dn, 3);
  53. }
  54. private static function sphsn ($a, $es, $sphi) {
  55. $sinSphi = sin($sphi);
  56. return $a / sqrt(1 - $es * pow($sinSphi, 2));
  57. }
  58. private static function sphtmd ($ap, $bp, $cp, $dp, $ep, $sphi) {
  59. return ($ap * $sphi) - ($bp * sin(2 * $sphi)) + ($cp * sin(4 * $sphi)) - ($dp * sin(6 * $sphi)) + ($ep * sin(8 * $sphi));
  60. }
  61. private static function LonLatToPuwg($a, $f, $x, $y, $proj) {
  62. if ($x < 13.5 || $x >= 25.5) throw new Exception("Wrong longitude - {$x}");
  63. if ($proj == 1) {
  64. $alam = 19 * M_PI / 180;
  65. $strf = 0;
  66. $nfn = -5300000.0;
  67. $ok = 0.9993;
  68. } else {
  69. $nfn = 0;
  70. $olam = floor(($x + 1.5)/3) * M_PI / 60;
  71. $strf = floor(($x + 1.5)/3) * 1000000;
  72. $ok = 0.999923;
  73. }
  74. $result = new StdClass();
  75. $latRad = $y * M_PI / 180;
  76. $lonRad = $x * M_PI / 180;
  77. $recf = 1 / $f;
  78. $b = $a * ($recf - 1) / $recf;
  79. $eSquared = self::CalculateESquared($a, $b);
  80. $e2Squared = self::CalculateE2Squared ($a, $b);
  81. $tn = ($a - $b) / ($a + $b);
  82. $ap = $a * (1 - $tn + 5 * (pow($tn, 2) - pow($tn, 3)) / 4 + 81 * (pow($tn, 4) - pow($tn, 5)) / 64);
  83. $bp = 3 * $a * ($tn - pow($tn, 2) + 7 * (pow($tn, 3) - pow($tn, 4)) / 8 + 55 * pow($tn, 5) / 64) / 2;
  84. $cp = 15 * $a * (pow($tn, 2) - pow($tn, 3) + 3 * (pow($tn, 4) - pow($tn, 5)) / 4) / 16;
  85. $dp = 35 * $a * (pow($tn, 3) - pow($tn, 4) + 11 * pow($tn, 5) / 16) / 48;
  86. $ep = 315 * $a * (pow($tn, 4) - pow($tn, 5)) / 512;
  87. $dlam = $lonRad - $olam;
  88. $s = sin($latRad);
  89. $c = cos($latRad);
  90. $t = $s / $c;
  91. $eta = $e2Squared * pow($c, 2);
  92. $sn = self::sphsn($a, $eSquared, $latRad);
  93. $tmd = self::sphtmd($ap, $bp, $cp, $dp, $ep, $latRad);
  94. $t1 = $tmd * $ok;
  95. $t2 = $sn * $s * $c * $ok / 2;
  96. $t3 = $sn * $s * pow($c, 3) * $ok * (5 - pow($t, 2) + 9 * $eta + 4 * pow($eta, 2)) / 24;
  97. $t4 = $sn * $s * pow($c, 5) * $ok * (61 - 58 * pow($t, 2) + pow($t, 4) + 270 * $eta - 330 * pow($t, 2) * $eta + 445 * pow($eta, 2) + 324 * pow($eta, 3) - 680 * pow($t, 2) * pow($eta, 2) + 88 * pow($eta, 4) - 600 * pow($t, 2) * pow($eta, 3) - 192 * pow($t, 2) * pow($eta, 4)) / 720;
  98. $t5 = $sn * $s * pow($c, 7) * $ok * (1385 - 3111 * pow($t, 2) + 543 * pow($t, 4) - pow($t, 6)) / 40320;
  99. $t6 = $sn * $c * $ok;
  100. $t7 = $sn * pow($c, 3) * $ok * (1 - pow($t, 2) + $eta) / 6;
  101. $t8 = $sn * pow($c, 5) * $ok * (5 - 18 * pow($t, 2) + pow($t, 4) + 14 * $eta - 58 * pow($t, 2) * $eta + 13 * pow($eta, 2) + 4 * pow($eta, 3) - 64 * pow($t, 2) * pow($eta, 2) - 24 * pow($t, 2) * pow($eta, 3)) / 120;
  102. $t9 = $sn * pow($c, 7) * $ok * (61 - 479 * pow($t, 2) + 179 * pow($t, 4) - pow($t, 6)) / 5040;
  103. $result->x = self::$fe + $strf + $dlam * $t6 + pow($dlam, 3) * $t7 + pow($dlam, 5) * $t8 + pow($dlam, 7) * $t9;
  104. $result->y = $nfn + $t1 + pow($dlam, 2) * $t2 + pow($dlam, 4) * $t3 + pow($dlam, 6) * $t4 + pow($dlam, 8) * $t5;
  105. return $result;
  106. }
  107. private static function PuwgToLonLat ($a, $f, $x, $y, $proj) {
  108. $result = new StdClass();
  109. $ok = 0.999923;
  110. if ($proj == 1) $ok = 0.9993;
  111. $recf = 1 / $f;
  112. $b = $a * ($recf - 1) / $recf;
  113. $eSquared = self::CalculateESquared($a, $b);
  114. $e2Squared = self::CalculateE2Squared($a, $b);
  115. $tn = ($a - $b) / ($a + $b);
  116. $ap = $a * (1 - $tn + 5 * (pow($tn, 2) - pow($tn, 3)) / 4 + 81 * (pow($tn, 4) - pow($tn, 5)) / 64);
  117. $bp = 3 * $a * ($tn - pow($tn, 2) + 7 * (pow($tn, 3) - pow($tn, 4)) / 8 + 55 * pow($tn, 5) / 64) / 2;
  118. $cp = 15 * $a * (pow($tn, 2) - pow($tn, 3) + 3 * (pow($tn, 4) - pow($tn, 5)) / 4) / 16;
  119. $dp = 35 * $a * (pow($tn, 3) - pow($tn, 4) + 11 * pow($tn, 5) / 16) / 48;
  120. $ep = 315 * $a * (pow($tn, 4) - pow($tn, 5)) / 512;
  121. if ($proj == 1) {
  122. $olam = 19 * M_PI / 180;
  123. $strf = 0;
  124. $nfn = -5300000;
  125. } else {
  126. $nfn = 0;
  127. $strf = floor($x / 1000000) * 1000000;
  128. $olam = $strf / 60000000 * M_PI;
  129. }
  130. $tmd = ($y - $nfn) / $ok;
  131. $sr = self::sphsr($a, $eSquared, 0);
  132. $ftphi = $tmd / $sr;
  133. for ($i = 0; $i < 5; $i++) {
  134. $t10 = self::sphtmd($ap, $bp, $cp, $dp, $ep, $ftphi);
  135. $sr = self::sphsr($a, $eSquared, $ftphi);
  136. $ftphi = $ftphi + ($tmd - $t10) / $sr;
  137. }
  138. $sr = self::sphsr($a, $eSquared, $ftphi);
  139. $sn = self::sphsn($a, $eSquared, $ftphi);
  140. $s = sin($ftphi);
  141. $c = cos($ftphi);
  142. $t = $s / $c;
  143. $eta = $e2Squared * pow($c, 2);
  144. $de = $x - self::$fe - $strf;
  145. $t10 = $t / (2 * $sr * $sn * pow($ok, 2));
  146. $t11 = $t * (5 + 3 * pow($t, 2) + $eta - 4 * pow($eta, 2) - 9 * pow($t, 2) * $eta) / (24 * $sr * pow($sn, 3) * pow($ok, 4));
  147. $t12 = $t * (61 + 90 * pow($t, 2) + 46 * $eta + 45 * pow($t, 4) - 252 * pow($t, 2) * $eta - 3 * pow($eta, 2) + 100 * pow($eta, 3) - 66 * pow($t, 2) * pow($eta, 2) - 90 * pow($t, 4) * $eta + 88 * pow($eta, 4) + 225 * pow($t, 4) * pow($eta, 2) + 84 * pow($t, 2) * pow($eta, 3) - 192 * pow($t, 2) * pow($eta, 4)) / (720 * $sr * pow($sn, 5) * pow($ok, 6));
  148. $t13 = $t * (1385 + 3633 * pow($t, 2) + 4095 * pow($t, 4) + 1575 * pow($t, 6)) / (40320 * $sr * pow($sn, 7) * pow($ok, 8));
  149. $t14 = 1 / ($sn * $c * $ok);
  150. $t15 = (1 + 2 * pow($t, 2) + $eta) / (6 * pow($sn, 3) * $c * pow($ok, 3));
  151. $t16 = 1 * (5 + 6 * $eta + 28 * pow($t, 2) - 3 * pow($eta, 2) + 8 * pow($t, 2) * $eta + 24 * pow($t, 4) - 4 * pow($eta, 3) + 4 * pow($t, 2) * pow($eta, 2) + 24 * pow($t, 2) * pow($eta, 3)) / (120 * pow($sn, 5) * $c * pow($ok, 5));
  152. $t17 = 1 * (61 + 662 * pow($t, 2) + 1320 * pow($t, 4) + 720 * pow($t, 6)) / (5040 * pow($sn, 7) * $c * pow($ok, 7));
  153. $dlam = $de * $t14 - pow($de, 3) * $t15 + pow($de, 5) * $t16 - pow($de, 7) * $t17;
  154. $result->x = ($olam + $dlam) * 180 / M_PI;
  155. $result->y = ($ftphi - pow($de, 2) * $t10 + pow($de, 4) * $t11 - pow($de, 6) * $t12 + pow($de, 8) * $t13) * 180 / M_PI;
  156. return $result;
  157. }
  158. public static function getEpsgPuwg2000($x, $y = 0) {
  159. if ($x < 13.5 || $x >= 25.5) throw new Exception("Wrong longitude - {$x}");
  160. return floor(($x + 1.5) / 3) + 2171;
  161. }
  162. public static function Wgs84ToPuwg2000($x, $y) {
  163. $result = self::LonLatToPuwg(6378137, 1 / 298.257223563, $x, $y, 4);
  164. $result->epsg = self::getEpsgPuwg2000($x);
  165. return $result;
  166. }
  167. public static function Puwg2000ToWgs84($x, $y) {
  168. $result = self::PuwgToLonLat(6378137, 1 / 298.257223563, $x, $y, 4);
  169. $result->epsg = self::getEpsgPuwg2000($result->x);
  170. return $result;
  171. }
  172. public static function Wgs84ToEpsg2180($x, $y) {
  173. // Parametry elipsoidy GRS-80i
  174. $e = 0.0818191910428; //pierwszy mimośród elipsoidy
  175. $R0 = 6367449.14577; //promieñ sfery Lagrange'a
  176. $Snorm = 0.000002; //parametr normujący
  177. $xo = 5760000; //parametr centrujący
  178. //Współczynniki wielomianu
  179. $a0 = 5765181.11148097;
  180. $a1 = 499800.81713800;
  181. $a2 = -63.81145283;
  182. $a3 = 0.83537915;
  183. $a4 = 0.13046891;
  184. $a5 = -0.00111138;
  185. $a6 = -0.00010504;
  186. // Parametry odwzorowania Gaussa-Kruegera dla układu PUWG92
  187. $L0_stopnie = 19; //Początek układu wsp. PUWG92 (długość)
  188. $m0 = 0.9993;
  189. $x0 = -5300000;
  190. $y0 = 500000;
  191. // Zakres stosowalności metody
  192. $Bmin = 48 * M_PI / 180;
  193. $Bmax = 56 * M_PI / 180;
  194. $dLmin = -6 * M_PI / 180;
  195. $dLmax = 6 * M_PI / 180;
  196. // Weryfikacja danych wejściowych
  197. $B = $y * M_PI / 180;
  198. $dL_stopnie = $x - $L0_stopnie;
  199. $dL = $dL_stopnie * M_PI / 180;
  200. if ($B < $Bmin || $B > $Bmax) throw new Exception("Wrong lattitude - {$y}");
  201. if ($dL < $dLmin || $dL > $dLmax) throw new Exception("Wrong longitude - {$x}");
  202. //etap I - elipsoida na kulę
  203. $U = 1 - $e * sin($B);
  204. $V = 1 + $e * sin($B);
  205. $K = pow(($U /$V), ($e / 2));
  206. $C = $K * tan($B / 2 + M_PI / 4);
  207. $fi = 2 * atan($C) - M_PI / 2;
  208. $d_lambda = $dL;
  209. // etap II - kula na walec
  210. $p = sin($fi);
  211. $q = cos($fi) * cos($d_lambda);
  212. $r = 1 + cos($fi) * sin($d_lambda);
  213. $s = 1 - cos($fi) * sin($d_lambda);
  214. $XMERC = $R0 * atan($p / $q);
  215. $YMERC = 0.5 * $R0 * log($r / $s);
  216. //etap III - walec na płaszczyznę
  217. $Z = new Complex(($XMERC - $xo) * $Snorm, $YMERC * $Snorm);
  218. $Zgk = (new Complex())->add($a6)->multiply($Z)->add($a5)->multiply($Z)->add($a4)->multiply($Z)->add($a3)->multiply($Z)->add($a2)->multiply($Z)->add($a1)->multiply($Z)->add($a0);
  219. $Xgk = $Zgk->real();
  220. $Ygk = $Zgk->imag();
  221. //Przejście do układu aplikacyjnego
  222. $result = new StdClass();
  223. $result->x = $m0 * $Xgk + $x0;
  224. $result->y = $m0 * $Ygk + $y0;
  225. $result->epsg = 2180;
  226. return $result;
  227. }
  228. public static function Wgs84ToPuwg1992($x, $y) {
  229. return self::Wgs84ToEpsg2180($x, $y);
  230. }
  231. public static function Epsg2180ToWgs84($x, $y) {
  232. $L0_stopnie = 19; //Początek układu wsp. PUWG92 (długość)
  233. $m0 = 0.9993;
  234. $x0 = -5300000;
  235. $y0 = 500000;
  236. $R0 = 6367449.14577; //promieñ sfery Lagrange'a
  237. $Snorm = 0.000002; //parametr normujący
  238. $xo_prim=5765181.11148097; //parametr centrujący
  239. // Współczynniki wielomianu
  240. $b0 = 5760000;
  241. $b1 = 500199.26224125;
  242. $b2 = 63.88777449;
  243. $b3 = -0.82039170;
  244. $b4 = -0.13125817;
  245. $b5 = 0.00101782;
  246. $b6 = 0.00010778;
  247. // Współczynniki szeregu tryg.
  248. $c2 = 0.0033565514856;
  249. $c4 = 0.0000065718731;
  250. $c6 = 0.0000000176466;
  251. $c8 = 0.0000000000540;
  252. //Przejście z układu aplikacyjnego
  253. $Xgk = ($x - $x0) / $m0;
  254. $Ygk = ($y - $y0) / $m0;
  255. //etap I - (Xgk, Ygk) -> (Xmerc, Ymerc)
  256. $Z = new Complex(($Xgk - $xo_prim) * $Snorm, $Ygk * $Snorm);
  257. $Zmerc = (new Complex())->add($b6)->multiply($Z)->add($b5)->multiply($Z)->add($b4)->multiply($Z)->add($b3)->multiply($Z)->add($b2)->multiply($Z)->add($b1)->multiply($Z)->add($b0);
  258. $Xmerc = $Zmerc->real();
  259. $Ymerc = $Zmerc->imag();
  260. //etap II - Xmerc,Ymerc -> fi, delta_lambda
  261. $alfa = $Xmerc / $R0;
  262. $beta = $Ymerc / $R0;
  263. $w = 2 * atan(exp($beta)) - M_PI / 2;
  264. $fi = asin(cos($w) * sin($alfa));
  265. $d_lambda = atan(tan($w) / cos($alfa));
  266. //etap III
  267. $B = $fi + $c2 * sin(2 * $fi) + $c4 * sin(4 *$fi) + $c6 * sin(6 * $fi) + $c8 * sin(8 * $fi);
  268. $dL = $d_lambda;
  269. //Obliczenia koncowe
  270. $result = new StdClass();
  271. $result->x = $dL / M_PI * 180 + $L0_stopnie;
  272. $result->y = $B / M_PI * 180;
  273. $result->epsg = 2180;
  274. return $result;
  275. }
  276. public static function Puwg1992ToWgs84($x, $y) {
  277. return self::Epsg2180ToWgs84($x, $y);
  278. }
  279. public static function GetZByEpsg2180($x, $y) {
  280. if (!(is_numeric($x) && is_numeric($y))) throw new Exception('Bad argument(s)');
  281. $table = 'CODGIK_NMT_100_EPSG2180';
  282. $query = "select x(`the_geom`) as `x`, y(`the_geom`) as `y`, `z` from `{$table}` where abs(x(`the_geom`) - {$x}) < 100 and abs(y(`the_geom`) - {$y}) < 100";
  283. try {
  284. $result = DB::getPDO()->fetchAll($query);
  285. } catch (Exception $e) {
  286. throw new Exception('Error while connecting to database');
  287. }
  288. $count = count($result);
  289. if (!$count) throw new Exception('Out of the area');
  290. $z = [];
  291. foreach ($result as $row) {
  292. $z[$row['x']][$row['y']] = $row['z'];
  293. $_x[$row['x']] = true;
  294. $_y[$row['y']] = true;
  295. }
  296. $xmin = min(array_keys($_x));
  297. $xmax = max(array_keys($_x));
  298. $ymin = min(array_keys($_y));
  299. $ymax = max(array_keys($_y));
  300. if ($count == 1) {
  301. return $z[$xmin][$ymin];
  302. } elseif ($count == 2) {
  303. if ($xmin == $xmax && $ymax - $ymin == 100) {
  304. return (($y - $ymin) * $z[$xmin][$ymax] + ($ymax - $y) * $z[$xmin][$ymin]) / 100;
  305. } elseif ($ymin == $ymax && $xmax - $xmin == 100) {
  306. return (($x - $xmin) * $z[$xmax][$ymin] + ($xmax - $x) * $z[$xmin][$ymin]) / 100;
  307. } else throw new Exception('Unknown error #2');
  308. } elseif ($count == 4) {
  309. if (!($ymax - $ymin == 100 && $xmax - $xmin == 100)) throw new Exception('Unknown error #3');
  310. return (($y - $ymin) * (($x - $xmin) * $z[$xmax][$ymax] + ($xmax - $x) * $z[$xmin][$ymax])
  311. + ($ymax - $y) * (($x - $xmin) * $z[$xmax][$ymin] + ($xmax - $x) * $z[$xmin][$ymin])) / 10000;
  312. } else throw new Exception('Unknown error #1');
  313. }
  314. public static function GetZByWgs84($x, $y) {
  315. $epsg2180 = self::Wgs84ToEpsg2180($x, $y);
  316. return self::GetZByEpsg2180($epsg2180->x, $epsg2180->y);
  317. }
  318. }