GisPoint.php 11 KB

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