GisMultiPolygon.php 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645
  1. <?php
  2. /**
  3. * Handles actions related to GIS MULTIPOLYGON objects
  4. */
  5. declare(strict_types=1);
  6. namespace PhpMyAdmin\Gis;
  7. use TCPDF;
  8. use function array_merge;
  9. use function array_push;
  10. use function array_slice;
  11. use function count;
  12. use function explode;
  13. use function hexdec;
  14. use function imagecolorallocate;
  15. use function imagefilledpolygon;
  16. use function imagestring;
  17. use function json_encode;
  18. use function mb_strlen;
  19. use function mb_strpos;
  20. use function mb_substr;
  21. use function trim;
  22. /**
  23. * Handles actions related to GIS MULTIPOLYGON objects
  24. */
  25. class GisMultiPolygon extends GisGeometry
  26. {
  27. /** @var self */
  28. private static $instance;
  29. /**
  30. * A private constructor; prevents direct creation of object.
  31. *
  32. * @access private
  33. */
  34. private function __construct()
  35. {
  36. }
  37. /**
  38. * Returns the singleton.
  39. *
  40. * @return GisMultiPolygon the singleton
  41. *
  42. * @access public
  43. */
  44. public static function singleton()
  45. {
  46. if (! isset(self::$instance)) {
  47. self::$instance = new GisMultiPolygon();
  48. }
  49. return self::$instance;
  50. }
  51. /**
  52. * Scales each row.
  53. *
  54. * @param string $spatial spatial data of a row
  55. *
  56. * @return array an array containing the min, max values for x and y coordinates
  57. *
  58. * @access public
  59. */
  60. public function scaleRow($spatial)
  61. {
  62. $min_max = [];
  63. // Trim to remove leading 'MULTIPOLYGON(((' and trailing ')))'
  64. $multipolygon
  65. = mb_substr(
  66. $spatial,
  67. 15,
  68. mb_strlen($spatial) - 18
  69. );
  70. // Separate each polygon
  71. $polygons = explode(')),((', $multipolygon);
  72. foreach ($polygons as $polygon) {
  73. // If the polygon doesn't have an inner ring, use polygon itself
  74. if (mb_strpos($polygon, '),(') === false) {
  75. $ring = $polygon;
  76. } else {
  77. // Separate outer ring and use it to determine min-max
  78. $parts = explode('),(', $polygon);
  79. $ring = $parts[0];
  80. }
  81. $min_max = $this->setMinMax($ring, $min_max);
  82. }
  83. return $min_max;
  84. }
  85. /**
  86. * Adds to the PNG image object, the data related to a row in the GIS dataset.
  87. *
  88. * @param string $spatial GIS POLYGON object
  89. * @param string|null $label Label for the GIS POLYGON object
  90. * @param string $fill_color Color for the GIS POLYGON object
  91. * @param array $scale_data Array containing data related to scaling
  92. * @param resource $image Image object
  93. *
  94. * @return resource the modified image object
  95. *
  96. * @access public
  97. */
  98. public function prepareRowAsPng(
  99. $spatial,
  100. ?string $label,
  101. $fill_color,
  102. array $scale_data,
  103. $image
  104. ) {
  105. // allocate colors
  106. $black = imagecolorallocate($image, 0, 0, 0);
  107. $red = hexdec(mb_substr($fill_color, 1, 2));
  108. $green = hexdec(mb_substr($fill_color, 3, 2));
  109. $blue = hexdec(mb_substr($fill_color, 4, 2));
  110. $color = imagecolorallocate($image, $red, $green, $blue);
  111. // Trim to remove leading 'MULTIPOLYGON(((' and trailing ')))'
  112. $multipolygon
  113. = mb_substr(
  114. $spatial,
  115. 15,
  116. mb_strlen($spatial) - 18
  117. );
  118. // Separate each polygon
  119. $polygons = explode(')),((', $multipolygon);
  120. $first_poly = true;
  121. $points_arr = [];
  122. foreach ($polygons as $polygon) {
  123. // If the polygon doesn't have an inner polygon
  124. if (mb_strpos($polygon, '),(') === false) {
  125. $points_arr = $this->extractPoints($polygon, $scale_data, true);
  126. } else {
  127. // Separate outer and inner polygons
  128. $parts = explode('),(', $polygon);
  129. $outer = $parts[0];
  130. $inner = array_slice($parts, 1);
  131. $points_arr = $this->extractPoints($outer, $scale_data, true);
  132. foreach ($inner as $inner_poly) {
  133. $points_arr = array_merge(
  134. $points_arr,
  135. $this->extractPoints($inner_poly, $scale_data, true)
  136. );
  137. }
  138. }
  139. // draw polygon
  140. imagefilledpolygon($image, $points_arr, count($points_arr) / 2, $color);
  141. // mark label point if applicable
  142. if (isset($label) && trim($label) != '' && $first_poly) {
  143. $label_point = [
  144. $points_arr[2],
  145. $points_arr[3],
  146. ];
  147. }
  148. $first_poly = false;
  149. }
  150. // print label if applicable
  151. if (isset($label_point)) {
  152. imagestring(
  153. $image,
  154. 1,
  155. $points_arr[2],
  156. $points_arr[3],
  157. trim((string) $label),
  158. $black
  159. );
  160. }
  161. return $image;
  162. }
  163. /**
  164. * Adds to the TCPDF instance, the data related to a row in the GIS dataset.
  165. *
  166. * @param string $spatial GIS MULTIPOLYGON object
  167. * @param string|null $label Label for the GIS MULTIPOLYGON object
  168. * @param string $fill_color Color for the GIS MULTIPOLYGON object
  169. * @param array $scale_data Array containing data related to scaling
  170. * @param TCPDF $pdf TCPDF instance
  171. *
  172. * @return TCPDF the modified TCPDF instance
  173. *
  174. * @access public
  175. */
  176. public function prepareRowAsPdf($spatial, ?string $label, $fill_color, array $scale_data, $pdf)
  177. {
  178. // allocate colors
  179. $red = hexdec(mb_substr($fill_color, 1, 2));
  180. $green = hexdec(mb_substr($fill_color, 3, 2));
  181. $blue = hexdec(mb_substr($fill_color, 4, 2));
  182. $color = [
  183. $red,
  184. $green,
  185. $blue,
  186. ];
  187. // Trim to remove leading 'MULTIPOLYGON(((' and trailing ')))'
  188. $multipolygon
  189. = mb_substr(
  190. $spatial,
  191. 15,
  192. mb_strlen($spatial) - 18
  193. );
  194. // Separate each polygon
  195. $polygons = explode(')),((', $multipolygon);
  196. $first_poly = true;
  197. foreach ($polygons as $polygon) {
  198. // If the polygon doesn't have an inner polygon
  199. if (mb_strpos($polygon, '),(') === false) {
  200. $points_arr = $this->extractPoints($polygon, $scale_data, true);
  201. } else {
  202. // Separate outer and inner polygons
  203. $parts = explode('),(', $polygon);
  204. $outer = $parts[0];
  205. $inner = array_slice($parts, 1);
  206. $points_arr = $this->extractPoints($outer, $scale_data, true);
  207. foreach ($inner as $inner_poly) {
  208. $points_arr = array_merge(
  209. $points_arr,
  210. $this->extractPoints($inner_poly, $scale_data, true)
  211. );
  212. }
  213. }
  214. // draw polygon
  215. $pdf->Polygon($points_arr, 'F*', [], $color, true);
  216. // mark label point if applicable
  217. if (isset($label) && trim($label) != '' && $first_poly) {
  218. $label_point = [
  219. $points_arr[2],
  220. $points_arr[3],
  221. ];
  222. }
  223. $first_poly = false;
  224. }
  225. // print label if applicable
  226. if (isset($label_point)) {
  227. $pdf->SetXY($label_point[0], $label_point[1]);
  228. $pdf->SetFontSize(5);
  229. $pdf->Cell(0, 0, trim((string) $label));
  230. }
  231. return $pdf;
  232. }
  233. /**
  234. * Prepares and returns the code related to a row in the GIS dataset as SVG.
  235. *
  236. * @param string $spatial GIS MULTIPOLYGON object
  237. * @param string $label Label for the GIS MULTIPOLYGON object
  238. * @param string $fill_color Color for the GIS MULTIPOLYGON object
  239. * @param array $scale_data Array containing data related to scaling
  240. *
  241. * @return string the code related to a row in the GIS dataset
  242. *
  243. * @access public
  244. */
  245. public function prepareRowAsSvg($spatial, $label, $fill_color, array $scale_data)
  246. {
  247. $polygon_options = [
  248. 'name' => $label,
  249. 'class' => 'multipolygon vector',
  250. 'stroke' => 'black',
  251. 'stroke-width' => 0.5,
  252. 'fill' => $fill_color,
  253. 'fill-rule' => 'evenodd',
  254. 'fill-opacity' => 0.8,
  255. ];
  256. $row = '';
  257. // Trim to remove leading 'MULTIPOLYGON(((' and trailing ')))'
  258. $multipolygon
  259. = mb_substr(
  260. $spatial,
  261. 15,
  262. mb_strlen($spatial) - 18
  263. );
  264. // Separate each polygon
  265. $polygons = explode(')),((', $multipolygon);
  266. foreach ($polygons as $polygon) {
  267. $row .= '<path d="';
  268. // If the polygon doesn't have an inner polygon
  269. if (mb_strpos($polygon, '),(') === false) {
  270. $row .= $this->drawPath($polygon, $scale_data);
  271. } else {
  272. // Separate outer and inner polygons
  273. $parts = explode('),(', $polygon);
  274. $outer = $parts[0];
  275. $inner = array_slice($parts, 1);
  276. $row .= $this->drawPath($outer, $scale_data);
  277. foreach ($inner as $inner_poly) {
  278. $row .= $this->drawPath($inner_poly, $scale_data);
  279. }
  280. }
  281. $polygon_options['id'] = $label . $this->getRandomId();
  282. $row .= '"';
  283. foreach ($polygon_options as $option => $val) {
  284. $row .= ' ' . $option . '="' . trim((string) $val) . '"';
  285. }
  286. $row .= '/>';
  287. }
  288. return $row;
  289. }
  290. /**
  291. * Prepares JavaScript related to a row in the GIS dataset
  292. * to visualize it with OpenLayers.
  293. *
  294. * @param string $spatial GIS MULTIPOLYGON object
  295. * @param int $srid Spatial reference ID
  296. * @param string $label Label for the GIS MULTIPOLYGON object
  297. * @param array $fill_color Color for the GIS MULTIPOLYGON object
  298. * @param array $scale_data Array containing data related to scaling
  299. *
  300. * @return string JavaScript related to a row in the GIS dataset
  301. *
  302. * @access public
  303. */
  304. public function prepareRowAsOl($spatial, $srid, $label, $fill_color, array $scale_data)
  305. {
  306. $fill_opacity = 0.8;
  307. array_push($fill_color, $fill_opacity);
  308. $fill_style = ['color' => $fill_color];
  309. $stroke_style = [
  310. 'color' => [0,0,0],
  311. 'width' => 0.5,
  312. ];
  313. $row = 'var style = new ol.style.Style({'
  314. . 'fill: new ol.style.Fill(' . json_encode($fill_style) . '),'
  315. . 'stroke: new ol.style.Stroke(' . json_encode($stroke_style) . ')';
  316. if ($label) {
  317. $text_style = ['text' => $label];
  318. $row .= ',text: new ol.style.Text(' . json_encode($text_style) . ')';
  319. }
  320. $row .= '});';
  321. if ($srid == 0) {
  322. $srid = 4326;
  323. }
  324. $row .= $this->getBoundsForOl($srid, $scale_data);
  325. // Trim to remove leading 'MULTIPOLYGON(((' and trailing ')))'
  326. $multipolygon
  327. = mb_substr(
  328. $spatial,
  329. 15,
  330. mb_strlen($spatial) - 18
  331. );
  332. // Separate each polygon
  333. $polygons = explode(')),((', $multipolygon);
  334. return $row . $this->getPolygonArrayForOpenLayers($polygons, $srid)
  335. . 'var multiPolygon = new ol.geom.MultiPolygon(polygonArray);'
  336. . 'var feature = new ol.Feature(multiPolygon);'
  337. . 'feature.setStyle(style);'
  338. . 'vectorLayer.addFeature(feature);';
  339. }
  340. /**
  341. * Draws a ring of the polygon using SVG path element.
  342. *
  343. * @param string $polygon The ring
  344. * @param array $scale_data Array containing data related to scaling
  345. *
  346. * @return string the code to draw the ring
  347. *
  348. * @access private
  349. */
  350. private function drawPath($polygon, array $scale_data)
  351. {
  352. $points_arr = $this->extractPoints($polygon, $scale_data);
  353. $row = ' M ' . $points_arr[0][0] . ', ' . $points_arr[0][1];
  354. $other_points = array_slice($points_arr, 1, count($points_arr) - 2);
  355. foreach ($other_points as $point) {
  356. $row .= ' L ' . $point[0] . ', ' . $point[1];
  357. }
  358. $row .= ' Z ';
  359. return $row;
  360. }
  361. /**
  362. * Generate the WKT with the set of parameters passed by the GIS editor.
  363. *
  364. * @param array $gis_data GIS data
  365. * @param int $index Index into the parameter object
  366. * @param string $empty Value for empty points
  367. *
  368. * @return string WKT with the set of parameters passed by the GIS editor
  369. *
  370. * @access public
  371. */
  372. public function generateWkt(array $gis_data, $index, $empty = '')
  373. {
  374. $data_row = $gis_data[$index]['MULTIPOLYGON'];
  375. $no_of_polygons = $data_row['no_of_polygons'] ?? 1;
  376. if ($no_of_polygons < 1) {
  377. $no_of_polygons = 1;
  378. }
  379. $wkt = 'MULTIPOLYGON(';
  380. for ($k = 0; $k < $no_of_polygons; $k++) {
  381. $no_of_lines = $data_row[$k]['no_of_lines'] ?? 1;
  382. if ($no_of_lines < 1) {
  383. $no_of_lines = 1;
  384. }
  385. $wkt .= '(';
  386. for ($i = 0; $i < $no_of_lines; $i++) {
  387. $no_of_points = $data_row[$k][$i]['no_of_points'] ?? 4;
  388. if ($no_of_points < 4) {
  389. $no_of_points = 4;
  390. }
  391. $wkt .= '(';
  392. for ($j = 0; $j < $no_of_points; $j++) {
  393. $wkt .= (isset($data_row[$k][$i][$j]['x'])
  394. && trim((string) $data_row[$k][$i][$j]['x']) != ''
  395. ? $data_row[$k][$i][$j]['x'] : $empty)
  396. . ' ' . (isset($data_row[$k][$i][$j]['y'])
  397. && trim((string) $data_row[$k][$i][$j]['y']) != ''
  398. ? $data_row[$k][$i][$j]['y'] : $empty) . ',';
  399. }
  400. $wkt
  401. = mb_substr(
  402. $wkt,
  403. 0,
  404. mb_strlen($wkt) - 1
  405. );
  406. $wkt .= '),';
  407. }
  408. $wkt
  409. = mb_substr(
  410. $wkt,
  411. 0,
  412. mb_strlen($wkt) - 1
  413. );
  414. $wkt .= '),';
  415. }
  416. $wkt
  417. = mb_substr(
  418. $wkt,
  419. 0,
  420. mb_strlen($wkt) - 1
  421. );
  422. return $wkt . ')';
  423. }
  424. /**
  425. * Generate the WKT for the data from ESRI shape files.
  426. *
  427. * @param array $row_data GIS data
  428. *
  429. * @return string the WKT for the data from ESRI shape files
  430. *
  431. * @access public
  432. */
  433. public function getShape(array $row_data)
  434. {
  435. // Determines whether each line ring is an inner ring or an outer ring.
  436. // If it's an inner ring get a point on the surface which can be used to
  437. // correctly classify inner rings to their respective outer rings.
  438. foreach ($row_data['parts'] as $i => $ring) {
  439. $row_data['parts'][$i]['isOuter']
  440. = GisPolygon::isOuterRing($ring['points']);
  441. }
  442. // Find points on surface for inner rings
  443. foreach ($row_data['parts'] as $i => $ring) {
  444. if ($ring['isOuter']) {
  445. continue;
  446. }
  447. $row_data['parts'][$i]['pointOnSurface']
  448. = GisPolygon::getPointOnSurface($ring['points']);
  449. }
  450. // Classify inner rings to their respective outer rings.
  451. foreach ($row_data['parts'] as $j => $ring1) {
  452. if ($ring1['isOuter']) {
  453. continue;
  454. }
  455. foreach ($row_data['parts'] as $k => $ring2) {
  456. if (! $ring2['isOuter']) {
  457. continue;
  458. }
  459. // If the pointOnSurface of the inner ring
  460. // is also inside the outer ring
  461. if (! GisPolygon::isPointInsidePolygon(
  462. $ring1['pointOnSurface'],
  463. $ring2['points']
  464. )
  465. ) {
  466. continue;
  467. }
  468. if (! isset($ring2['inner'])) {
  469. $row_data['parts'][$k]['inner'] = [];
  470. }
  471. $row_data['parts'][$k]['inner'][] = $j;
  472. }
  473. }
  474. $wkt = 'MULTIPOLYGON(';
  475. // for each polygon
  476. foreach ($row_data['parts'] as $ring) {
  477. if (! $ring['isOuter']) {
  478. continue;
  479. }
  480. $wkt .= '('; // start of polygon
  481. $wkt .= '('; // start of outer ring
  482. foreach ($ring['points'] as $point) {
  483. $wkt .= $point['x'] . ' ' . $point['y'] . ',';
  484. }
  485. $wkt
  486. = mb_substr(
  487. $wkt,
  488. 0,
  489. mb_strlen($wkt) - 1
  490. );
  491. $wkt .= ')'; // end of outer ring
  492. // inner rings if any
  493. if (isset($ring['inner'])) {
  494. foreach ($ring['inner'] as $j) {
  495. $wkt .= ',('; // start of inner ring
  496. foreach ($row_data['parts'][$j]['points'] as $innerPoint) {
  497. $wkt .= $innerPoint['x'] . ' ' . $innerPoint['y'] . ',';
  498. }
  499. $wkt
  500. = mb_substr(
  501. $wkt,
  502. 0,
  503. mb_strlen($wkt) - 1
  504. );
  505. $wkt .= ')'; // end of inner ring
  506. }
  507. }
  508. $wkt .= '),'; // end of polygon
  509. }
  510. $wkt
  511. = mb_substr(
  512. $wkt,
  513. 0,
  514. mb_strlen($wkt) - 1
  515. );
  516. return $wkt . ')';
  517. }
  518. /**
  519. * Generate parameters for the GIS data editor from the value of the GIS column.
  520. *
  521. * @param string $value Value of the GIS column
  522. * @param int $index Index of the geometry
  523. *
  524. * @return array params for the GIS data editor from the value of the GIS column
  525. *
  526. * @access public
  527. */
  528. public function generateParams($value, $index = -1)
  529. {
  530. $params = [];
  531. if ($index == -1) {
  532. $index = 0;
  533. $data = GisGeometry::generateParams($value);
  534. $params['srid'] = $data['srid'];
  535. $wkt = $data['wkt'];
  536. } else {
  537. $params[$index]['gis_type'] = 'MULTIPOLYGON';
  538. $wkt = $value;
  539. }
  540. // Trim to remove leading 'MULTIPOLYGON(((' and trailing ')))'
  541. $multipolygon
  542. = mb_substr(
  543. $wkt,
  544. 15,
  545. mb_strlen($wkt) - 18
  546. );
  547. // Separate each polygon
  548. $polygons = explode(')),((', $multipolygon);
  549. $param_row =& $params[$index]['MULTIPOLYGON'];
  550. $param_row['no_of_polygons'] = count($polygons);
  551. $k = 0;
  552. foreach ($polygons as $polygon) {
  553. // If the polygon doesn't have an inner polygon
  554. if (mb_strpos($polygon, '),(') === false) {
  555. $param_row[$k]['no_of_lines'] = 1;
  556. $points_arr = $this->extractPoints($polygon, null);
  557. $no_of_points = count($points_arr);
  558. $param_row[$k][0]['no_of_points'] = $no_of_points;
  559. for ($i = 0; $i < $no_of_points; $i++) {
  560. $param_row[$k][0][$i]['x'] = $points_arr[$i][0];
  561. $param_row[$k][0][$i]['y'] = $points_arr[$i][1];
  562. }
  563. } else {
  564. // Separate outer and inner polygons
  565. $parts = explode('),(', $polygon);
  566. $param_row[$k]['no_of_lines'] = count($parts);
  567. $j = 0;
  568. foreach ($parts as $ring) {
  569. $points_arr = $this->extractPoints($ring, null);
  570. $no_of_points = count($points_arr);
  571. $param_row[$k][$j]['no_of_points'] = $no_of_points;
  572. for ($i = 0; $i < $no_of_points; $i++) {
  573. $param_row[$k][$j][$i]['x'] = $points_arr[$i][0];
  574. $param_row[$k][$j][$i]['y'] = $points_arr[$i][1];
  575. }
  576. $j++;
  577. }
  578. }
  579. $k++;
  580. }
  581. return $params;
  582. }
  583. }