GIS_Point.class.php 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294
  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. if (! defined('PHPMYADMIN')) {
  9. exit;
  10. }
  11. /**
  12. * Handles actions related to GIS POINT objects
  13. *
  14. * @package PhpMyAdmin-GIS
  15. */
  16. class PMA_GIS_Point extends PMA_GIS_Geometry
  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 PMA_GIS_Point the singleton
  32. * @access public
  33. */
  34. public static function singleton()
  35. {
  36. if (!isset(self::$_instance)) {
  37. $class = __CLASS__;
  38. self::$_instance = new $class;
  39. }
  40. return self::$_instance;
  41. }
  42. /**
  43. * Scales each row.
  44. *
  45. * @param string $spatial spatial data of a row
  46. *
  47. * @return array an array containing the min, max values for x and y cordinates
  48. * @access public
  49. */
  50. public function scaleRow($spatial)
  51. {
  52. // Trim to remove leading 'POINT(' and trailing ')'
  53. $point = substr($spatial, 6, (strlen($spatial) - 7));
  54. return $this->setMinMax($point, array());
  55. }
  56. /**
  57. * Adds to the PNG image object, the data related to a row in the GIS dataset.
  58. *
  59. * @param string $spatial GIS POINT object
  60. * @param string $label Label for the GIS POINT object
  61. * @param string $point_color Color for the GIS POINT object
  62. * @param array $scale_data Array containing data related to scaling
  63. * @param resource $image Image object
  64. *
  65. * @return object the modified image object
  66. * @access public
  67. */
  68. public function prepareRowAsPng($spatial, $label, $point_color,
  69. $scale_data, $image
  70. ) {
  71. // allocate colors
  72. $black = imagecolorallocate($image, 0, 0, 0);
  73. $red = hexdec(substr($point_color, 1, 2));
  74. $green = hexdec(substr($point_color, 3, 2));
  75. $blue = hexdec(substr($point_color, 4, 2));
  76. $color = imagecolorallocate($image, $red, $green, $blue);
  77. // Trim to remove leading 'POINT(' and trailing ')'
  78. $point = substr($spatial, 6, (strlen($spatial) - 7));
  79. $points_arr = $this->extractPoints($point, $scale_data);
  80. // draw a small circle to mark the point
  81. if ($points_arr[0][0] != '' && $points_arr[0][1] != '') {
  82. imagearc(
  83. $image, $points_arr[0][0], $points_arr[0][1], 7, 7, 0, 360, $color
  84. );
  85. // print label if applicable
  86. if (isset($label) && trim($label) != '') {
  87. imagestring(
  88. $image, 1, $points_arr[0][0],
  89. $points_arr[0][1], trim($label), $black
  90. );
  91. }
  92. }
  93. return $image;
  94. }
  95. /**
  96. * Adds to the TCPDF instance, the data related to a row in the GIS dataset.
  97. *
  98. * @param string $spatial GIS POINT object
  99. * @param string $label Label for the GIS POINT object
  100. * @param string $point_color Color for the GIS POINT object
  101. * @param array $scale_data Array containing data related to scaling
  102. * @param TCPDF $pdf TCPDF instance
  103. *
  104. * @return TCPDF the modified TCPDF instance
  105. * @access public
  106. */
  107. public function prepareRowAsPdf($spatial, $label, $point_color,
  108. $scale_data, $pdf
  109. ) {
  110. // allocate colors
  111. $red = hexdec(substr($point_color, 1, 2));
  112. $green = hexdec(substr($point_color, 3, 2));
  113. $blue = hexdec(substr($point_color, 4, 2));
  114. $line = array('width' => 1.25, 'color' => array($red, $green, $blue));
  115. // Trim to remove leading 'POINT(' and trailing ')'
  116. $point = substr($spatial, 6, (strlen($spatial) - 7));
  117. $points_arr = $this->extractPoints($point, $scale_data);
  118. // draw a small circle to mark the point
  119. if ($points_arr[0][0] != '' && $points_arr[0][1] != '') {
  120. $pdf->Circle(
  121. $points_arr[0][0], $points_arr[0][1], 2, 0, 360, 'D', $line
  122. );
  123. // print label if applicable
  124. if (isset($label) && trim($label) != '') {
  125. $pdf->SetXY($points_arr[0][0], $points_arr[0][1]);
  126. $pdf->SetFontSize(5);
  127. $pdf->Cell(0, 0, trim($label));
  128. }
  129. }
  130. return $pdf;
  131. }
  132. /**
  133. * Prepares and returns the code related to a row in the GIS dataset as SVG.
  134. *
  135. * @param string $spatial GIS POINT object
  136. * @param string $label Label for the GIS POINT object
  137. * @param string $point_color Color for the GIS POINT object
  138. * @param array $scale_data Array containing data related to scaling
  139. *
  140. * @return string the code related to a row in the GIS dataset
  141. * @access public
  142. */
  143. public function prepareRowAsSvg($spatial, $label, $point_color, $scale_data)
  144. {
  145. $point_options = array(
  146. 'name' => $label,
  147. 'id' => $label . rand(),
  148. 'class' => 'point vector',
  149. 'fill' => 'white',
  150. 'stroke' => $point_color,
  151. 'stroke-width'=> 2,
  152. );
  153. // Trim to remove leading 'POINT(' and trailing ')'
  154. $point = substr($spatial, 6, (strlen($spatial) - 7));
  155. $points_arr = $this->extractPoints($point, $scale_data);
  156. $row = '';
  157. if ($points_arr[0][0] != '' && $points_arr[0][1] != '') {
  158. $row .= '<circle cx="' . $points_arr[0][0]
  159. . '" cy="' . $points_arr[0][1] . '" r="3"';
  160. foreach ($point_options as $option => $val) {
  161. $row .= ' ' . $option . '="' . trim($val) . '"';
  162. }
  163. $row .= '/>';
  164. }
  165. return $row;
  166. }
  167. /**
  168. * Prepares JavaScript related to a row in the GIS dataset
  169. * to visualize it with OpenLayers.
  170. *
  171. * @param string $spatial GIS POINT object
  172. * @param int $srid Spatial reference ID
  173. * @param string $label Label for the GIS POINT object
  174. * @param string $point_color Color for the GIS POINT object
  175. * @param array $scale_data Array containing data related to scaling
  176. *
  177. * @return string JavaScript related to a row in the GIS dataset
  178. * @access public
  179. */
  180. public function prepareRowAsOl($spatial, $srid, $label,
  181. $point_color, $scale_data
  182. ) {
  183. $style_options = array(
  184. 'pointRadius' => 3,
  185. 'fillColor' => '#ffffff',
  186. 'strokeColor' => $point_color,
  187. 'strokeWidth' => 2,
  188. 'label' => $label,
  189. 'labelYOffset' => -8,
  190. 'fontSize' => 10,
  191. );
  192. if ($srid == 0) {
  193. $srid = 4326;
  194. }
  195. $result = $this->getBoundsForOl($srid, $scale_data);
  196. // Trim to remove leading 'POINT(' and trailing ')'
  197. $point = substr($spatial, 6, (strlen($spatial) - 7));
  198. $points_arr = $this->extractPoints($point, null);
  199. if ($points_arr[0][0] != '' && $points_arr[0][1] != '') {
  200. $result .= 'vectorLayer.addFeatures(new OpenLayers.Feature.Vector('
  201. . $this->getPointForOpenLayers($points_arr[0], $srid) . ', null, '
  202. . json_encode($style_options) . '));';
  203. }
  204. return $result;
  205. }
  206. /**
  207. * Generate the WKT with the set of parameters passed by the GIS editor.
  208. *
  209. * @param array $gis_data GIS data
  210. * @param int $index Index into the parameter object
  211. * @param string $empty Point deos not adhere to this parameter
  212. *
  213. * @return string WKT with the set of parameters passed by the GIS editor
  214. * @access public
  215. */
  216. public function generateWkt($gis_data, $index, $empty = '')
  217. {
  218. return 'POINT('
  219. . ((isset($gis_data[$index]['POINT']['x'])
  220. && trim($gis_data[$index]['POINT']['x']) != '')
  221. ? $gis_data[$index]['POINT']['x'] : '')
  222. . ' '
  223. . ((isset($gis_data[$index]['POINT']['y'])
  224. && trim($gis_data[$index]['POINT']['y']) != '')
  225. ? $gis_data[$index]['POINT']['y'] : '') . ')';
  226. }
  227. /**
  228. * Generate the WKT for the data from ESRI shape files.
  229. *
  230. * @param array $row_data GIS data
  231. *
  232. * @return string the WKT for the data from ESRI shape files
  233. * @access public
  234. */
  235. public function getShape($row_data)
  236. {
  237. return 'POINT(' . (isset($row_data['x']) ? $row_data['x'] : '')
  238. . ' ' . (isset($row_data['y']) ? $row_data['y'] : '') . ')';
  239. }
  240. /**
  241. * Generate parameters for the GIS data editor from the value of the GIS column.
  242. *
  243. * @param string $value of the GIS column
  244. * @param int $index of the geometry
  245. *
  246. * @return array params for the GIS data editor from the value of the GIS column
  247. * @access public
  248. */
  249. public function generateParams($value, $index = -1)
  250. {
  251. $params = array();
  252. if ($index == -1) {
  253. $index = 0;
  254. $data = PMA_GIS_Geometry::generateParams($value);
  255. $params['srid'] = $data['srid'];
  256. $wkt = $data['wkt'];
  257. } else {
  258. $params[$index]['gis_type'] = 'POINT';
  259. $wkt = $value;
  260. }
  261. // Trim to remove leading 'POINT(' and trailing ')'
  262. $point = substr($wkt, 6, (strlen($wkt) - 7));
  263. $points_arr = $this->extractPoints($point, null);
  264. $params[$index]['POINT']['x'] = $points_arr[0][0];
  265. $params[$index]['POINT']['y'] = $points_arr[0][1];
  266. return $params;
  267. }
  268. }
  269. ?>