GisMultiPoint.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393
  1. <?php
  2. /**
  3. * Handles actions related to GIS MULTIPOINT 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 MULTIPOINT objects
  17. */
  18. class GisMultiPoint 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 GisMultiPoint the singleton
  32. */
  33. public static function singleton()
  34. {
  35. if (! isset(self::$instance)) {
  36. self::$instance = new GisMultiPoint();
  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 'MULTIPOINT(' and trailing ')'
  51. $multipoint = mb_substr($spatial, 11, -1);
  52. return $this->setMinMax($multipoint, 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 $point_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. $point_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($point_color, 1, 2));
  72. $green = (int) hexdec(mb_substr($point_color, 3, 2));
  73. $blue = (int) hexdec(mb_substr($point_color, 4, 2));
  74. $color = $image->colorAllocate($red, $green, $blue);
  75. // Trim to remove leading 'MULTIPOINT(' and trailing ')'
  76. $multipoint = mb_substr($spatial, 11, -1);
  77. $points_arr = $this->extractPoints($multipoint, $scale_data);
  78. foreach ($points_arr as $point) {
  79. // draw a small circle to mark the point
  80. if ($point[0] == '' || $point[1] == '') {
  81. continue;
  82. }
  83. $image->arc(
  84. (int) round($point[0]),
  85. (int) round($point[1]),
  86. 7,
  87. 7,
  88. 0,
  89. 360,
  90. $color
  91. );
  92. }
  93. // print label for each point
  94. if ((isset($label) && trim($label) != '') && ($points_arr[0][0] != '' && $points_arr[0][1] != '')) {
  95. $image->string(
  96. 1,
  97. (int) round($points_arr[0][0]),
  98. (int) round($points_arr[0][1]),
  99. trim($label),
  100. $black
  101. );
  102. }
  103. return $image;
  104. }
  105. /**
  106. * Adds to the TCPDF instance, the data related to a row in the GIS dataset.
  107. *
  108. * @param string $spatial GIS MULTIPOINT object
  109. * @param string|null $label Label for the GIS MULTIPOINT object
  110. * @param string $point_color Color for the GIS MULTIPOINT object
  111. * @param array $scale_data Array containing data related to scaling
  112. * @param TCPDF $pdf TCPDF instance
  113. *
  114. * @return TCPDF the modified TCPDF instance
  115. */
  116. public function prepareRowAsPdf(
  117. $spatial,
  118. ?string $label,
  119. $point_color,
  120. array $scale_data,
  121. $pdf
  122. ) {
  123. // allocate colors
  124. $red = hexdec(mb_substr($point_color, 1, 2));
  125. $green = hexdec(mb_substr($point_color, 3, 2));
  126. $blue = hexdec(mb_substr($point_color, 4, 2));
  127. $line = [
  128. 'width' => 1.25,
  129. 'color' => [
  130. $red,
  131. $green,
  132. $blue,
  133. ],
  134. ];
  135. // Trim to remove leading 'MULTIPOINT(' and trailing ')'
  136. $multipoint = mb_substr($spatial, 11, -1);
  137. $points_arr = $this->extractPoints($multipoint, $scale_data);
  138. foreach ($points_arr as $point) {
  139. // draw a small circle to mark the point
  140. if ($point[0] == '' || $point[1] == '') {
  141. continue;
  142. }
  143. $pdf->Circle($point[0], $point[1], 2, 0, 360, 'D', $line);
  144. }
  145. // print label for each point
  146. if ((isset($label) && trim($label) != '') && ($points_arr[0][0] != '' && $points_arr[0][1] != '')) {
  147. $pdf->setXY($points_arr[0][0], $points_arr[0][1]);
  148. $pdf->setFontSize(5);
  149. $pdf->Cell(0, 0, trim($label));
  150. }
  151. return $pdf;
  152. }
  153. /**
  154. * Prepares and returns the code related to a row in the GIS dataset as SVG.
  155. *
  156. * @param string $spatial GIS MULTIPOINT object
  157. * @param string $label Label for the GIS MULTIPOINT object
  158. * @param string $point_color Color for the GIS MULTIPOINT object
  159. * @param array $scale_data Array containing data related to scaling
  160. *
  161. * @return string the code related to a row in the GIS dataset
  162. */
  163. public function prepareRowAsSvg($spatial, $label, $point_color, array $scale_data)
  164. {
  165. $point_options = [
  166. 'data-label' => $label,
  167. 'class' => 'multipoint vector',
  168. 'fill' => 'white',
  169. 'stroke' => $point_color,
  170. 'stroke-width' => 2,
  171. ];
  172. // Trim to remove leading 'MULTIPOINT(' and trailing ')'
  173. $multipoint = mb_substr($spatial, 11, -1);
  174. $points_arr = $this->extractPoints($multipoint, $scale_data);
  175. $row = '';
  176. foreach ($points_arr as $point) {
  177. if (((float) $point[0]) === 0.0 || ((float) $point[1]) === 0.0) {
  178. continue;
  179. }
  180. $row .= '<circle cx="' . $point[0] . '" cy="'
  181. . $point[1] . '" r="3"';
  182. $point_options['id'] = $label . $this->getRandomId();
  183. foreach ($point_options as $option => $val) {
  184. $row .= ' ' . $option . '="' . trim((string) $val) . '"';
  185. }
  186. $row .= '/>';
  187. }
  188. return $row;
  189. }
  190. /**
  191. * Prepares JavaScript related to a row in the GIS dataset
  192. * to visualize it with OpenLayers.
  193. *
  194. * @param string $spatial GIS MULTIPOINT object
  195. * @param int $srid Spatial reference ID
  196. * @param string $label Label for the GIS MULTIPOINT object
  197. * @param array $point_color Color for the GIS MULTIPOINT object
  198. * @param array $scale_data Array containing data related to scaling
  199. *
  200. * @return string JavaScript related to a row in the GIS dataset
  201. */
  202. public function prepareRowAsOl(
  203. $spatial,
  204. int $srid,
  205. $label,
  206. $point_color,
  207. array $scale_data
  208. ) {
  209. $fill_style = ['color' => 'white'];
  210. $stroke_style = [
  211. 'color' => $point_color,
  212. 'width' => 2,
  213. ];
  214. $result = 'var fill = new ol.style.Fill(' . json_encode($fill_style) . ');'
  215. . 'var stroke = new ol.style.Stroke(' . json_encode($stroke_style) . ');'
  216. . 'var style = new ol.style.Style({'
  217. . 'image: new ol.style.Circle({'
  218. . 'fill: fill,'
  219. . 'stroke: stroke,'
  220. . 'radius: 3'
  221. . '}),'
  222. . 'fill: fill,'
  223. . 'stroke: stroke';
  224. if (trim($label) !== '') {
  225. $text_style = [
  226. 'text' => trim($label),
  227. 'offsetY' => -9,
  228. ];
  229. $result .= ',text: new ol.style.Text(' . json_encode($text_style) . ')';
  230. }
  231. $result .= '});';
  232. if ($srid === 0) {
  233. $srid = 4326;
  234. }
  235. $result .= $this->getBoundsForOl($srid, $scale_data);
  236. // Trim to remove leading 'MULTIPOINT(' and trailing ')'
  237. $multipoint = mb_substr($spatial, 11, -1);
  238. $points_arr = $this->extractPoints($multipoint, null);
  239. return $result . 'var multiPoint = new ol.geom.MultiPoint('
  240. . $this->getPointsArrayForOpenLayers($points_arr, $srid) . ');'
  241. . 'var feature = new ol.Feature({geometry: multiPoint});'
  242. . 'feature.setStyle(style);'
  243. . 'vectorLayer.addFeature(feature);';
  244. }
  245. /**
  246. * Generate the WKT with the set of parameters passed by the GIS editor.
  247. *
  248. * @param array $gis_data GIS data
  249. * @param int $index Index into the parameter object
  250. * @param string|null $empty Multipoint does not adhere to this
  251. *
  252. * @return string WKT with the set of parameters passed by the GIS editor
  253. */
  254. public function generateWkt(array $gis_data, $index, $empty = '')
  255. {
  256. $no_of_points = $gis_data[$index]['MULTIPOINT']['no_of_points'] ?? 1;
  257. if ($no_of_points < 1) {
  258. $no_of_points = 1;
  259. }
  260. $wkt = 'MULTIPOINT(';
  261. for ($i = 0; $i < $no_of_points; $i++) {
  262. $wkt .= (isset($gis_data[$index]['MULTIPOINT'][$i]['x'])
  263. && trim((string) $gis_data[$index]['MULTIPOINT'][$i]['x']) != ''
  264. ? $gis_data[$index]['MULTIPOINT'][$i]['x'] : '')
  265. . ' ' . (isset($gis_data[$index]['MULTIPOINT'][$i]['y'])
  266. && trim((string) $gis_data[$index]['MULTIPOINT'][$i]['y']) != ''
  267. ? $gis_data[$index]['MULTIPOINT'][$i]['y'] : '') . ',';
  268. }
  269. $wkt = mb_substr($wkt, 0, -1);
  270. return $wkt . ')';
  271. }
  272. /**
  273. * Generate the WKT for the data from ESRI shape files.
  274. *
  275. * @param array $row_data GIS data
  276. *
  277. * @return string the WKT for the data from ESRI shape files
  278. */
  279. public function getShape(array $row_data)
  280. {
  281. $wkt = 'MULTIPOINT(';
  282. for ($i = 0; $i < $row_data['numpoints']; $i++) {
  283. $wkt .= $row_data['points'][$i]['x'] . ' '
  284. . $row_data['points'][$i]['y'] . ',';
  285. }
  286. $wkt = mb_substr($wkt, 0, -1);
  287. return $wkt . ')';
  288. }
  289. /**
  290. * Generate parameters for the GIS data editor from the value of the GIS column.
  291. *
  292. * @param string $value Value of the GIS column
  293. * @param int $index Index of the geometry
  294. *
  295. * @return array params for the GIS data editor from the value of the GIS column
  296. */
  297. public function generateParams($value, $index = -1)
  298. {
  299. $params = [];
  300. if ($index == -1) {
  301. $index = 0;
  302. $data = GisGeometry::generateParams($value);
  303. $params['srid'] = $data['srid'];
  304. $wkt = $data['wkt'];
  305. } else {
  306. $params[$index]['gis_type'] = 'MULTIPOINT';
  307. $wkt = $value;
  308. }
  309. // Trim to remove leading 'MULTIPOINT(' and trailing ')'
  310. $points = mb_substr($wkt, 11, -1);
  311. $points_arr = $this->extractPoints($points, null);
  312. $no_of_points = count($points_arr);
  313. $params[$index]['MULTIPOINT']['no_of_points'] = $no_of_points;
  314. for ($i = 0; $i < $no_of_points; $i++) {
  315. $params[$index]['MULTIPOINT'][$i]['x'] = $points_arr[$i][0];
  316. $params[$index]['MULTIPOINT'][$i]['y'] = $points_arr[$i][1];
  317. }
  318. return $params;
  319. }
  320. /**
  321. * Overridden to make sure that only the points having valid values
  322. * for x and y coordinates are added.
  323. *
  324. * @param array $points_arr x and y coordinates for each point
  325. * @param int $srid spatial reference id
  326. *
  327. * @return string JavaScript for adding an array of points to OpenLayers
  328. */
  329. protected function getPointsArrayForOpenLayers(array $points_arr, int $srid)
  330. {
  331. $ol_array = 'new Array(';
  332. foreach ($points_arr as $point) {
  333. if ($point[0] == '' || $point[1] == '') {
  334. continue;
  335. }
  336. $ol_array .= $this->getPointForOpenLayers($point, $srid) . '.getCoordinates(), ';
  337. }
  338. if (mb_substr($ol_array, -2) === ', ') {
  339. $ol_array = mb_substr($ol_array, 0, -2);
  340. }
  341. return $ol_array . ')';
  342. }
  343. }