GisPoint.php 10 KB

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