GisMultiPoint.php 13 KB

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