GisPoint.php 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356
  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. namespace PhpMyAdmin\Gis;
  9. use TCPDF;
  10. /**
  11. * Handles actions related to GIS POINT objects
  12. *
  13. * @package PhpMyAdmin-GIS
  14. */
  15. class GisPoint extends GisGeometry
  16. {
  17. // Hold the singleton instance of the class
  18. private static $_instance;
  19. /**
  20. * A private constructor; prevents direct creation of object.
  21. *
  22. * @access private
  23. */
  24. private function __construct()
  25. {
  26. }
  27. /**
  28. * Returns the singleton.
  29. *
  30. * @return GisPoint the singleton
  31. * @access public
  32. */
  33. public static function singleton()
  34. {
  35. if (!isset(self::$_instance)) {
  36. $class = __CLASS__;
  37. self::$_instance = new $class;
  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, array());
  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 POINT object
  64. * @param string $label Label for the GIS POINT object
  65. * @param string $point_color Color for the GIS POINT 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. $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 $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. $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 = array('width' => 1.25, 'color' => array($red, $green, $blue));
  143. // Trim to remove leading 'POINT(' and trailing ')'
  144. $point
  145. = mb_substr(
  146. $spatial,
  147. 6,
  148. mb_strlen($spatial) - 7
  149. );
  150. $points_arr = $this->extractPoints($point, $scale_data);
  151. // draw a small circle to mark the point
  152. if ($points_arr[0][0] != '' && $points_arr[0][1] != '') {
  153. $pdf->Circle(
  154. $points_arr[0][0],
  155. $points_arr[0][1],
  156. 2,
  157. 0,
  158. 360,
  159. 'D',
  160. $line
  161. );
  162. // print label if applicable
  163. if (isset($label) && trim($label) != '') {
  164. $pdf->SetXY($points_arr[0][0], $points_arr[0][1]);
  165. $pdf->SetFontSize(5);
  166. $pdf->Cell(0, 0, trim($label));
  167. }
  168. }
  169. return $pdf;
  170. }
  171. /**
  172. * Prepares and returns the code related to a row in the GIS dataset as SVG.
  173. *
  174. * @param string $spatial GIS POINT object
  175. * @param string $label Label for the GIS POINT object
  176. * @param string $point_color Color for the GIS POINT object
  177. * @param array $scale_data Array containing data related to scaling
  178. *
  179. * @return string the code related to a row in the GIS dataset
  180. * @access public
  181. */
  182. public function prepareRowAsSvg($spatial, $label, $point_color, array $scale_data)
  183. {
  184. $point_options = array(
  185. 'name' => $label,
  186. 'id' => $label . rand(),
  187. 'class' => 'point vector',
  188. 'fill' => 'white',
  189. 'stroke' => $point_color,
  190. 'stroke-width' => 2,
  191. );
  192. // Trim to remove leading 'POINT(' and trailing ')'
  193. $point
  194. = mb_substr(
  195. $spatial,
  196. 6,
  197. mb_strlen($spatial) - 7
  198. );
  199. $points_arr = $this->extractPoints($point, $scale_data);
  200. $row = '';
  201. if ($points_arr[0][0] != '' && $points_arr[0][1] != '') {
  202. $row .= '<circle cx="' . $points_arr[0][0]
  203. . '" cy="' . $points_arr[0][1] . '" r="3"';
  204. foreach ($point_options as $option => $val) {
  205. $row .= ' ' . $option . '="' . trim($val) . '"';
  206. }
  207. $row .= '/>';
  208. }
  209. return $row;
  210. }
  211. /**
  212. * Prepares JavaScript related to a row in the GIS dataset
  213. * to visualize it with OpenLayers.
  214. *
  215. * @param string $spatial GIS POINT object
  216. * @param int $srid Spatial reference ID
  217. * @param string $label Label for the GIS POINT object
  218. * @param string $point_color Color for the GIS POINT object
  219. * @param array $scale_data Array containing data related to scaling
  220. *
  221. * @return string JavaScript related to a row in the GIS dataset
  222. * @access public
  223. */
  224. public function prepareRowAsOl(
  225. $spatial,
  226. $srid,
  227. $label,
  228. $point_color,
  229. array $scale_data
  230. ) {
  231. $style_options = array(
  232. 'pointRadius' => 3,
  233. 'fillColor' => '#ffffff',
  234. 'strokeColor' => $point_color,
  235. 'strokeWidth' => 2,
  236. 'label' => $label,
  237. 'labelYOffset' => -8,
  238. 'fontSize' => 10,
  239. );
  240. if ($srid == 0) {
  241. $srid = 4326;
  242. }
  243. $result = $this->getBoundsForOl($srid, $scale_data);
  244. // Trim to remove leading 'POINT(' and trailing ')'
  245. $point
  246. = mb_substr(
  247. $spatial,
  248. 6,
  249. mb_strlen($spatial) - 7
  250. );
  251. $points_arr = $this->extractPoints($point, null);
  252. if ($points_arr[0][0] != '' && $points_arr[0][1] != '') {
  253. $result .= 'vectorLayer.addFeatures(new OpenLayers.Feature.Vector('
  254. . $this->getPointForOpenLayers($points_arr[0], $srid) . ', null, '
  255. . json_encode($style_options) . '));';
  256. }
  257. return $result;
  258. }
  259. /**
  260. * Generate the WKT with the set of parameters passed by the GIS editor.
  261. *
  262. * @param array $gis_data GIS data
  263. * @param int $index Index into the parameter object
  264. * @param string $empty Point does not adhere to this parameter
  265. *
  266. * @return string WKT with the set of parameters passed by the GIS editor
  267. * @access public
  268. */
  269. public function generateWkt(array $gis_data, $index, $empty = '')
  270. {
  271. return 'POINT('
  272. . ((isset($gis_data[$index]['POINT']['x'])
  273. && trim($gis_data[$index]['POINT']['x']) != '')
  274. ? $gis_data[$index]['POINT']['x'] : '')
  275. . ' '
  276. . ((isset($gis_data[$index]['POINT']['y'])
  277. && trim($gis_data[$index]['POINT']['y']) != '')
  278. ? $gis_data[$index]['POINT']['y'] : '') . ')';
  279. }
  280. /**
  281. * Generate the WKT for the data from ESRI shape files.
  282. *
  283. * @param array $row_data GIS data
  284. *
  285. * @return string the WKT for the data from ESRI shape files
  286. * @access public
  287. */
  288. public function getShape(array $row_data)
  289. {
  290. return 'POINT(' . (isset($row_data['x']) ? $row_data['x'] : '')
  291. . ' ' . (isset($row_data['y']) ? $row_data['y'] : '') . ')';
  292. }
  293. /**
  294. * Generate parameters for the GIS data editor from the value of the GIS column.
  295. *
  296. * @param string $value of the GIS column
  297. * @param int $index of the geometry
  298. *
  299. * @return array params for the GIS data editor from the value of the GIS column
  300. * @access public
  301. */
  302. public function generateParams($value, $index = -1)
  303. {
  304. $params = array();
  305. if ($index == -1) {
  306. $index = 0;
  307. $data = GisGeometry::generateParams($value);
  308. $params['srid'] = $data['srid'];
  309. $wkt = $data['wkt'];
  310. } else {
  311. $params[$index]['gis_type'] = 'POINT';
  312. $wkt = $value;
  313. }
  314. // Trim to remove leading 'POINT(' and trailing ')'
  315. $point
  316. = mb_substr(
  317. $wkt,
  318. 6,
  319. mb_strlen($wkt) - 7
  320. );
  321. $points_arr = $this->extractPoints($point, null);
  322. $params[$index]['POINT']['x'] = $points_arr[0][0];
  323. $params[$index]['POINT']['y'] = $points_arr[0][1];
  324. return $params;
  325. }
  326. }