GisPoint.php 11 KB

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