GisMultiPolygon.php 19 KB

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