GisLineString.php 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322
  1. <?php
  2. /**
  3. * Handles actions related to GIS LINESTRING objects
  4. */
  5. declare(strict_types=1);
  6. namespace PhpMyAdmin\Gis;
  7. use PhpMyAdmin\Image\ImageWrapper;
  8. use TCPDF;
  9. use function count;
  10. use function hexdec;
  11. use function json_encode;
  12. use function mb_substr;
  13. use function round;
  14. use function trim;
  15. /**
  16. * Handles actions related to GIS LINESTRING objects
  17. */
  18. class GisLineString extends GisGeometry
  19. {
  20. /** @var self */
  21. private static $instance;
  22. /**
  23. * A private constructor; prevents direct creation of object.
  24. */
  25. private function __construct()
  26. {
  27. }
  28. /**
  29. * Returns the singleton.
  30. *
  31. * @return GisLineString the singleton
  32. */
  33. public static function singleton()
  34. {
  35. if (! isset(self::$instance)) {
  36. self::$instance = new GisLineString();
  37. }
  38. return self::$instance;
  39. }
  40. /**
  41. * Scales each row.
  42. *
  43. * @param string $spatial spatial data of a row
  44. *
  45. * @return array an array containing the min, max values for x and y coordinates
  46. * @psalm-return array{minX:float,minY:float,maxX:float,maxY:float}
  47. */
  48. public function scaleRow($spatial)
  49. {
  50. // Trim to remove leading 'LINESTRING(' and trailing ')'
  51. $linestring = mb_substr($spatial, 11, -1);
  52. return $this->setMinMax($linestring, GisGeometry::EMPTY_EXTENT);
  53. }
  54. /**
  55. * Adds to the PNG image object, the data related to a row in the GIS dataset.
  56. *
  57. * @param string $spatial GIS POLYGON object
  58. * @param string|null $label Label for the GIS POLYGON object
  59. * @param string $line_color Color for the GIS POLYGON object
  60. * @param array $scale_data Array containing data related to scaling
  61. */
  62. public function prepareRowAsPng(
  63. $spatial,
  64. ?string $label,
  65. $line_color,
  66. array $scale_data,
  67. ImageWrapper $image
  68. ): ImageWrapper {
  69. // allocate colors
  70. $black = $image->colorAllocate(0, 0, 0);
  71. $red = (int) hexdec(mb_substr($line_color, 1, 2));
  72. $green = (int) hexdec(mb_substr($line_color, 3, 2));
  73. $blue = (int) hexdec(mb_substr($line_color, 4, 2));
  74. $color = $image->colorAllocate($red, $green, $blue);
  75. $label = trim($label ?? '');
  76. // Trim to remove leading 'LINESTRING(' and trailing ')'
  77. $lineString = mb_substr($spatial, 11, -1);
  78. $points_arr = $this->extractPoints($lineString, $scale_data);
  79. foreach ($points_arr as $point) {
  80. if (isset($temp_point)) {
  81. // draw line section
  82. $image->line(
  83. (int) round($temp_point[0]),
  84. (int) round($temp_point[1]),
  85. (int) round($point[0]),
  86. (int) round($point[1]),
  87. $color
  88. );
  89. }
  90. $temp_point = $point;
  91. }
  92. // print label if applicable
  93. if ($label !== '') {
  94. $image->string(
  95. 1,
  96. (int) round($points_arr[1][0]),
  97. (int) round($points_arr[1][1]),
  98. $label,
  99. $black
  100. );
  101. }
  102. return $image;
  103. }
  104. /**
  105. * Adds to the TCPDF instance, the data related to a row in the GIS dataset.
  106. *
  107. * @param string $spatial GIS LINESTRING object
  108. * @param string|null $label Label for the GIS LINESTRING object
  109. * @param string $line_color Color for the GIS LINESTRING object
  110. * @param array $scale_data Array containing data related to scaling
  111. * @param TCPDF $pdf TCPDF instance
  112. *
  113. * @return TCPDF the modified TCPDF instance
  114. */
  115. public function prepareRowAsPdf($spatial, ?string $label, $line_color, array $scale_data, $pdf)
  116. {
  117. // allocate colors
  118. $red = hexdec(mb_substr($line_color, 1, 2));
  119. $green = hexdec(mb_substr($line_color, 3, 2));
  120. $blue = hexdec(mb_substr($line_color, 4, 2));
  121. $line = [
  122. 'width' => 1.5,
  123. 'color' => [
  124. $red,
  125. $green,
  126. $blue,
  127. ],
  128. ];
  129. $label = trim($label ?? '');
  130. // Trim to remove leading 'LINESTRING(' and trailing ')'
  131. $linesrting = mb_substr($spatial, 11, -1);
  132. $points_arr = $this->extractPoints($linesrting, $scale_data);
  133. foreach ($points_arr as $point) {
  134. if (isset($temp_point)) {
  135. // draw line section
  136. $pdf->Line($temp_point[0], $temp_point[1], $point[0], $point[1], $line);
  137. }
  138. $temp_point = $point;
  139. }
  140. // print label
  141. if ($label !== '') {
  142. $pdf->setXY($points_arr[1][0], $points_arr[1][1]);
  143. $pdf->setFontSize(5);
  144. $pdf->Cell(0, 0, $label);
  145. }
  146. return $pdf;
  147. }
  148. /**
  149. * Prepares and returns the code related to a row in the GIS dataset as SVG.
  150. *
  151. * @param string $spatial GIS LINESTRING object
  152. * @param string $label Label for the GIS LINESTRING object
  153. * @param string $line_color Color for the GIS LINESTRING object
  154. * @param array $scale_data Array containing data related to scaling
  155. *
  156. * @return string the code related to a row in the GIS dataset
  157. */
  158. public function prepareRowAsSvg($spatial, $label, $line_color, array $scale_data)
  159. {
  160. $line_options = [
  161. 'data-label' => $label,
  162. 'id' => $label . $this->getRandomId(),
  163. 'class' => 'linestring vector',
  164. 'fill' => 'none',
  165. 'stroke' => $line_color,
  166. 'stroke-width' => 2,
  167. ];
  168. // Trim to remove leading 'LINESTRING(' and trailing ')'
  169. $linesrting = mb_substr($spatial, 11, -1);
  170. $points_arr = $this->extractPoints($linesrting, $scale_data);
  171. $row = '<polyline points="';
  172. foreach ($points_arr as $point) {
  173. $row .= $point[0] . ',' . $point[1] . ' ';
  174. }
  175. $row .= '"';
  176. foreach ($line_options as $option => $val) {
  177. $row .= ' ' . $option . '="' . trim((string) $val) . '"';
  178. }
  179. $row .= '/>';
  180. return $row;
  181. }
  182. /**
  183. * Prepares JavaScript related to a row in the GIS dataset
  184. * to visualize it with OpenLayers.
  185. *
  186. * @param string $spatial GIS LINESTRING object
  187. * @param int $srid Spatial reference ID
  188. * @param string $label Label for the GIS LINESTRING object
  189. * @param array $line_color Color for the GIS LINESTRING object
  190. * @param array $scale_data Array containing data related to scaling
  191. *
  192. * @return string JavaScript related to a row in the GIS dataset
  193. */
  194. public function prepareRowAsOl($spatial, int $srid, $label, $line_color, array $scale_data)
  195. {
  196. $stroke_style = [
  197. 'color' => $line_color,
  198. 'width' => 2,
  199. ];
  200. $result = 'var style = new ol.style.Style({'
  201. . 'stroke: new ol.style.Stroke(' . json_encode($stroke_style) . ')';
  202. if (trim($label) !== '') {
  203. $text_style = ['text' => trim($label)];
  204. $result .= ', text: new ol.style.Text(' . json_encode($text_style) . ')';
  205. }
  206. $result .= '});';
  207. if ($srid === 0) {
  208. $srid = 4326;
  209. }
  210. $result .= $this->getBoundsForOl($srid, $scale_data);
  211. // Trim to remove leading 'LINESTRING(' and trailing ')'
  212. $linesrting = mb_substr($spatial, 11, -1);
  213. $points_arr = $this->extractPoints($linesrting, null);
  214. return $result . 'var line = new ol.Feature({geometry: '
  215. . $this->getLineForOpenLayers($points_arr, $srid) . '});'
  216. . 'line.setStyle(style);'
  217. . 'vectorLayer.addFeature(line);';
  218. }
  219. /**
  220. * Generate the WKT with the set of parameters passed by the GIS editor.
  221. *
  222. * @param array $gis_data GIS data
  223. * @param int $index Index into the parameter object
  224. * @param string|null $empty Value for empty points
  225. *
  226. * @return string WKT with the set of parameters passed by the GIS editor
  227. */
  228. public function generateWkt(array $gis_data, $index, $empty = '')
  229. {
  230. $no_of_points = $gis_data[$index]['LINESTRING']['no_of_points'] ?? 2;
  231. if ($no_of_points < 2) {
  232. $no_of_points = 2;
  233. }
  234. $wkt = 'LINESTRING(';
  235. for ($i = 0; $i < $no_of_points; $i++) {
  236. $wkt .= (isset($gis_data[$index]['LINESTRING'][$i]['x'])
  237. && trim((string) $gis_data[$index]['LINESTRING'][$i]['x']) != ''
  238. ? $gis_data[$index]['LINESTRING'][$i]['x'] : $empty)
  239. . ' ' . (isset($gis_data[$index]['LINESTRING'][$i]['y'])
  240. && trim((string) $gis_data[$index]['LINESTRING'][$i]['y']) != ''
  241. ? $gis_data[$index]['LINESTRING'][$i]['y'] : $empty) . ',';
  242. }
  243. $wkt = mb_substr($wkt, 0, -1);
  244. return $wkt . ')';
  245. }
  246. /**
  247. * Generate parameters for the GIS data editor from the value of the GIS column.
  248. *
  249. * @param string $value of the GIS column
  250. * @param int $index of the geometry
  251. *
  252. * @return array params for the GIS data editor from the value of the GIS column
  253. */
  254. public function generateParams($value, $index = -1)
  255. {
  256. $params = [];
  257. if ($index == -1) {
  258. $index = 0;
  259. $data = GisGeometry::generateParams($value);
  260. $params['srid'] = $data['srid'];
  261. $wkt = $data['wkt'];
  262. } else {
  263. $params[$index]['gis_type'] = 'LINESTRING';
  264. $wkt = $value;
  265. }
  266. // Trim to remove leading 'LINESTRING(' and trailing ')'
  267. $linestring = mb_substr($wkt, 11, -1);
  268. $points_arr = $this->extractPoints($linestring, null);
  269. $no_of_points = count($points_arr);
  270. $params[$index]['LINESTRING']['no_of_points'] = $no_of_points;
  271. for ($i = 0; $i < $no_of_points; $i++) {
  272. $params[$index]['LINESTRING'][$i]['x'] = $points_arr[$i][0];
  273. $params[$index]['LINESTRING'][$i]['y'] = $points_arr[$i][1];
  274. }
  275. return $params;
  276. }
  277. }