GisMultiLineString.php 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467
  1. <?php
  2. /**
  3. * Handles actions related to GIS MULTILINESTRING objects
  4. */
  5. declare(strict_types=1);
  6. namespace PhpMyAdmin\Gis;
  7. use TCPDF;
  8. use function count;
  9. use function explode;
  10. use function hexdec;
  11. use function imagecolorallocate;
  12. use function imageline;
  13. use function imagestring;
  14. use function json_encode;
  15. use function mb_strlen;
  16. use function mb_substr;
  17. use function trim;
  18. /**
  19. * Handles actions related to GIS MULTILINESTRING objects
  20. */
  21. class GisMultiLineString extends GisGeometry
  22. {
  23. /** @var self */
  24. private static $instance;
  25. /**
  26. * A private constructor; prevents direct creation of object.
  27. *
  28. * @access private
  29. */
  30. private function __construct()
  31. {
  32. }
  33. /**
  34. * Returns the singleton.
  35. *
  36. * @return GisMultiLineString the singleton
  37. *
  38. * @access public
  39. */
  40. public static function singleton()
  41. {
  42. if (! isset(self::$instance)) {
  43. self::$instance = new GisMultiLineString();
  44. }
  45. return self::$instance;
  46. }
  47. /**
  48. * Scales each row.
  49. *
  50. * @param string $spatial spatial data of a row
  51. *
  52. * @return array an array containing the min, max values for x and y coordinates
  53. *
  54. * @access public
  55. */
  56. public function scaleRow($spatial)
  57. {
  58. $min_max = [];
  59. // Trim to remove leading 'MULTILINESTRING((' and trailing '))'
  60. $multilinestirng
  61. = mb_substr(
  62. $spatial,
  63. 17,
  64. mb_strlen($spatial) - 19
  65. );
  66. // Separate each linestring
  67. $linestirngs = explode('),(', $multilinestirng);
  68. foreach ($linestirngs as $linestring) {
  69. $min_max = $this->setMinMax($linestring, $min_max);
  70. }
  71. return $min_max;
  72. }
  73. /**
  74. * Adds to the PNG image object, the data related to a row in the GIS dataset.
  75. *
  76. * @param string $spatial GIS POLYGON object
  77. * @param string|null $label Label for the GIS POLYGON object
  78. * @param string $line_color Color for the GIS POLYGON object
  79. * @param array $scale_data Array containing data related to scaling
  80. * @param resource $image Image object
  81. *
  82. * @return resource the modified image object
  83. *
  84. * @access public
  85. */
  86. public function prepareRowAsPng(
  87. $spatial,
  88. ?string $label,
  89. $line_color,
  90. array $scale_data,
  91. $image
  92. ) {
  93. // allocate colors
  94. $black = imagecolorallocate($image, 0, 0, 0);
  95. $red = hexdec(mb_substr($line_color, 1, 2));
  96. $green = hexdec(mb_substr($line_color, 3, 2));
  97. $blue = hexdec(mb_substr($line_color, 4, 2));
  98. $color = imagecolorallocate($image, $red, $green, $blue);
  99. // Trim to remove leading 'MULTILINESTRING((' and trailing '))'
  100. $multilinestirng
  101. = mb_substr(
  102. $spatial,
  103. 17,
  104. mb_strlen($spatial) - 19
  105. );
  106. // Separate each linestring
  107. $linestirngs = explode('),(', $multilinestirng);
  108. $first_line = true;
  109. foreach ($linestirngs as $linestring) {
  110. $points_arr = $this->extractPoints($linestring, $scale_data);
  111. foreach ($points_arr as $point) {
  112. if (! isset($temp_point)) {
  113. $temp_point = $point;
  114. } else {
  115. // draw line section
  116. imageline(
  117. $image,
  118. (int) $temp_point[0],
  119. (int) $temp_point[1],
  120. (int) $point[0],
  121. (int) $point[1],
  122. $color
  123. );
  124. $temp_point = $point;
  125. }
  126. }
  127. unset($temp_point);
  128. // print label if applicable
  129. if (isset($label) && trim($label) != '' && $first_line) {
  130. imagestring(
  131. $image,
  132. 1,
  133. $points_arr[1][0],
  134. $points_arr[1][1],
  135. trim($label),
  136. $black
  137. );
  138. }
  139. $first_line = false;
  140. }
  141. return $image;
  142. }
  143. /**
  144. * Adds to the TCPDF instance, the data related to a row in the GIS dataset.
  145. *
  146. * @param string $spatial GIS MULTILINESTRING object
  147. * @param string|null $label Label for the GIS MULTILINESTRING object
  148. * @param string $line_color Color for the GIS MULTILINESTRING object
  149. * @param array $scale_data Array containing data related to scaling
  150. * @param TCPDF $pdf TCPDF instance
  151. *
  152. * @return TCPDF the modified TCPDF instance
  153. *
  154. * @access public
  155. */
  156. public function prepareRowAsPdf($spatial, ?string $label, $line_color, array $scale_data, $pdf)
  157. {
  158. // allocate colors
  159. $red = hexdec(mb_substr($line_color, 1, 2));
  160. $green = hexdec(mb_substr($line_color, 3, 2));
  161. $blue = hexdec(mb_substr($line_color, 4, 2));
  162. $line = [
  163. 'width' => 1.5,
  164. 'color' => [
  165. $red,
  166. $green,
  167. $blue,
  168. ],
  169. ];
  170. // Trim to remove leading 'MULTILINESTRING((' and trailing '))'
  171. $multilinestirng
  172. = mb_substr(
  173. $spatial,
  174. 17,
  175. mb_strlen($spatial) - 19
  176. );
  177. // Separate each linestring
  178. $linestirngs = explode('),(', $multilinestirng);
  179. $first_line = true;
  180. foreach ($linestirngs as $linestring) {
  181. $points_arr = $this->extractPoints($linestring, $scale_data);
  182. foreach ($points_arr as $point) {
  183. if (! isset($temp_point)) {
  184. $temp_point = $point;
  185. } else {
  186. // draw line section
  187. $pdf->Line(
  188. $temp_point[0],
  189. $temp_point[1],
  190. $point[0],
  191. $point[1],
  192. $line
  193. );
  194. $temp_point = $point;
  195. }
  196. }
  197. unset($temp_point);
  198. // print label
  199. if (isset($label) && trim($label) != '' && $first_line) {
  200. $pdf->SetXY($points_arr[1][0], $points_arr[1][1]);
  201. $pdf->SetFontSize(5);
  202. $pdf->Cell(0, 0, trim($label));
  203. }
  204. $first_line = false;
  205. }
  206. return $pdf;
  207. }
  208. /**
  209. * Prepares and returns the code related to a row in the GIS dataset as SVG.
  210. *
  211. * @param string $spatial GIS MULTILINESTRING object
  212. * @param string $label Label for the GIS MULTILINESTRING object
  213. * @param string $line_color Color for the GIS MULTILINESTRING object
  214. * @param array $scale_data Array containing data related to scaling
  215. *
  216. * @return string the code related to a row in the GIS dataset
  217. *
  218. * @access public
  219. */
  220. public function prepareRowAsSvg($spatial, $label, $line_color, array $scale_data)
  221. {
  222. $line_options = [
  223. 'name' => $label,
  224. 'class' => 'linestring vector',
  225. 'fill' => 'none',
  226. 'stroke' => $line_color,
  227. 'stroke-width' => 2,
  228. ];
  229. // Trim to remove leading 'MULTILINESTRING((' and trailing '))'
  230. $multilinestirng
  231. = mb_substr(
  232. $spatial,
  233. 17,
  234. mb_strlen($spatial) - 19
  235. );
  236. // Separate each linestring
  237. $linestirngs = explode('),(', $multilinestirng);
  238. $row = '';
  239. foreach ($linestirngs as $linestring) {
  240. $points_arr = $this->extractPoints($linestring, $scale_data);
  241. $row .= '<polyline points="';
  242. foreach ($points_arr as $point) {
  243. $row .= $point[0] . ',' . $point[1] . ' ';
  244. }
  245. $row .= '"';
  246. $line_options['id'] = $label . $this->getRandomId();
  247. foreach ($line_options as $option => $val) {
  248. $row .= ' ' . $option . '="' . trim((string) $val) . '"';
  249. }
  250. $row .= '/>';
  251. }
  252. return $row;
  253. }
  254. /**
  255. * Prepares JavaScript related to a row in the GIS dataset
  256. * to visualize it with OpenLayers.
  257. *
  258. * @param string $spatial GIS MULTILINESTRING object
  259. * @param int $srid Spatial reference ID
  260. * @param string $label Label for the GIS MULTILINESTRING object
  261. * @param array $line_color Color for the GIS MULTILINESTRING object
  262. * @param array $scale_data Array containing data related to scaling
  263. *
  264. * @return string JavaScript related to a row in the GIS dataset
  265. *
  266. * @access public
  267. */
  268. public function prepareRowAsOl($spatial, $srid, $label, $line_color, array $scale_data)
  269. {
  270. $stroke_style = [
  271. 'color' => $line_color,
  272. 'width' => 2,
  273. ];
  274. $row = 'var style = new ol.style.Style({'
  275. . 'stroke: new ol.style.Stroke(' . json_encode($stroke_style) . ')';
  276. if ($label) {
  277. $text_style = ['text' => $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 'MULTILINESTRING((' and trailing '))'
  286. $multilinestirng
  287. = mb_substr(
  288. $spatial,
  289. 17,
  290. mb_strlen($spatial) - 19
  291. );
  292. // Separate each linestring
  293. $linestirngs = explode('),(', $multilinestirng);
  294. return $row . $this->getLineArrayForOpenLayers($linestirngs, $srid)
  295. . 'var multiLineString = new ol.geom.MultiLineString(arr);'
  296. . 'var feature = new ol.Feature({geometry: multiLineString});'
  297. . 'feature.setStyle(style);'
  298. . 'vectorLayer.addFeature(feature);';
  299. }
  300. /**
  301. * Generate the WKT with the set of parameters passed by the GIS editor.
  302. *
  303. * @param array $gis_data GIS data
  304. * @param int $index Index into the parameter object
  305. * @param string $empty Value for empty points
  306. *
  307. * @return string WKT with the set of parameters passed by the GIS editor
  308. *
  309. * @access public
  310. */
  311. public function generateWkt(array $gis_data, $index, $empty = '')
  312. {
  313. $data_row = $gis_data[$index]['MULTILINESTRING'];
  314. $no_of_lines = $data_row['no_of_lines'] ?? 1;
  315. if ($no_of_lines < 1) {
  316. $no_of_lines = 1;
  317. }
  318. $wkt = 'MULTILINESTRING(';
  319. for ($i = 0; $i < $no_of_lines; $i++) {
  320. $no_of_points = $data_row[$i]['no_of_points'] ?? 2;
  321. if ($no_of_points < 2) {
  322. $no_of_points = 2;
  323. }
  324. $wkt .= '(';
  325. for ($j = 0; $j < $no_of_points; $j++) {
  326. $wkt .= (isset($data_row[$i][$j]['x'])
  327. && trim((string) $data_row[$i][$j]['x']) != ''
  328. ? $data_row[$i][$j]['x'] : $empty)
  329. . ' ' . (isset($data_row[$i][$j]['y'])
  330. && trim((string) $data_row[$i][$j]['y']) != ''
  331. ? $data_row[$i][$j]['y'] : $empty) . ',';
  332. }
  333. $wkt
  334. = mb_substr(
  335. $wkt,
  336. 0,
  337. mb_strlen($wkt) - 1
  338. );
  339. $wkt .= '),';
  340. }
  341. $wkt
  342. = mb_substr(
  343. $wkt,
  344. 0,
  345. mb_strlen($wkt) - 1
  346. );
  347. return $wkt . ')';
  348. }
  349. /**
  350. * Generate the WKT for the data from ESRI shape files.
  351. *
  352. * @param array $row_data GIS data
  353. *
  354. * @return string the WKT for the data from ESRI shape files
  355. *
  356. * @access public
  357. */
  358. public function getShape(array $row_data)
  359. {
  360. $wkt = 'MULTILINESTRING(';
  361. for ($i = 0; $i < $row_data['numparts']; $i++) {
  362. $wkt .= '(';
  363. foreach ($row_data['parts'][$i]['points'] as $point) {
  364. $wkt .= $point['x'] . ' ' . $point['y'] . ',';
  365. }
  366. $wkt
  367. = mb_substr(
  368. $wkt,
  369. 0,
  370. mb_strlen($wkt) - 1
  371. );
  372. $wkt .= '),';
  373. }
  374. $wkt
  375. = mb_substr(
  376. $wkt,
  377. 0,
  378. mb_strlen($wkt) - 1
  379. );
  380. return $wkt . ')';
  381. }
  382. /**
  383. * Generate parameters for the GIS data editor from the value of the GIS column.
  384. *
  385. * @param string $value Value of the GIS column
  386. * @param int $index Index of the geometry
  387. *
  388. * @return array params for the GIS data editor from the value of the GIS column
  389. *
  390. * @access public
  391. */
  392. public function generateParams($value, $index = -1)
  393. {
  394. $params = [];
  395. if ($index == -1) {
  396. $index = 0;
  397. $data = GisGeometry::generateParams($value);
  398. $params['srid'] = $data['srid'];
  399. $wkt = $data['wkt'];
  400. } else {
  401. $params[$index]['gis_type'] = 'MULTILINESTRING';
  402. $wkt = $value;
  403. }
  404. // Trim to remove leading 'MULTILINESTRING((' and trailing '))'
  405. $multilinestirng
  406. = mb_substr(
  407. $wkt,
  408. 17,
  409. mb_strlen($wkt) - 19
  410. );
  411. // Separate each linestring
  412. $linestirngs = explode('),(', $multilinestirng);
  413. $params[$index]['MULTILINESTRING']['no_of_lines'] = count($linestirngs);
  414. $j = 0;
  415. foreach ($linestirngs as $linestring) {
  416. $points_arr = $this->extractPoints($linestring, null);
  417. $no_of_points = count($points_arr);
  418. $params[$index]['MULTILINESTRING'][$j]['no_of_points'] = $no_of_points;
  419. for ($i = 0; $i < $no_of_points; $i++) {
  420. $params[$index]['MULTILINESTRING'][$j][$i]['x'] = $points_arr[$i][0];
  421. $params[$index]['MULTILINESTRING'][$j][$i]['y'] = $points_arr[$i][1];
  422. }
  423. $j++;
  424. }
  425. return $params;
  426. }
  427. }