PageRenderTime 46ms CodeModel.GetById 17ms RepoModel.GetById 1ms app.codeStats 0ms

/pma/libraries/gis/GIS_Linestring.class.php

https://bitbucket.org/hrayrpapikyan/stahlbaum
PHP | 298 lines | 164 code | 24 blank | 110 comment | 24 complexity | 186a3455650729dc75ad5571fd8d4de0 MD5 | raw file
Possible License(s): GPL-2.0
  1. <?php
  2. /* vim: set expandtab sw=4 ts=4 sts=4: */
  3. /**
  4. * Handles actions related to GIS LINESTRING objects
  5. *
  6. * @package PhpMyAdmin-GIS
  7. */
  8. if (! defined('PHPMYADMIN')) {
  9. exit;
  10. }
  11. /**
  12. * Handles actions related to GIS LINESTRING objects
  13. *
  14. * @package PhpMyAdmin-GIS
  15. */
  16. class PMA_GIS_Linestring 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_Linestring 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 'LINESTRING(' and trailing ')'
  53. $linesrting = substr($spatial, 11, (strlen($spatial) - 12));
  54. return $this->setMinMax($linesrting, 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 LINESTRING object
  60. * @param string $label Label for the GIS LINESTRING object
  61. * @param string $line_color Color for the GIS LINESTRING object
  62. * @param array $scale_data Array containing data related to scaling
  63. * @param object $image Image object
  64. *
  65. * @return resource the modified image object
  66. * @access public
  67. */
  68. public function prepareRowAsPng($spatial, $label, $line_color,
  69. $scale_data, $image
  70. ) {
  71. // allocate colors
  72. $black = imagecolorallocate($image, 0, 0, 0);
  73. $red = hexdec(substr($line_color, 1, 2));
  74. $green = hexdec(substr($line_color, 3, 2));
  75. $blue = hexdec(substr($line_color, 4, 2));
  76. $color = imagecolorallocate($image, $red, $green, $blue);
  77. // Trim to remove leading 'LINESTRING(' and trailing ')'
  78. $linesrting = substr($spatial, 11, (strlen($spatial) - 12));
  79. $points_arr = $this->extractPoints($linesrting, $scale_data);
  80. foreach ($points_arr as $point) {
  81. if (! isset($temp_point)) {
  82. $temp_point = $point;
  83. } else {
  84. // draw line section
  85. imageline(
  86. $image, $temp_point[0], $temp_point[1],
  87. $point[0], $point[1], $color
  88. );
  89. $temp_point = $point;
  90. }
  91. }
  92. // print label if applicable
  93. if (isset($label) && trim($label) != '') {
  94. imagestring(
  95. $image, 1, $points_arr[1][0],
  96. $points_arr[1][1], trim($label), $black
  97. );
  98. }
  99. return $image;
  100. }
  101. /**
  102. * Adds to the TCPDF instance, the data related to a row in the GIS dataset.
  103. *
  104. * @param string $spatial GIS LINESTRING object
  105. * @param string $label Label for the GIS LINESTRING object
  106. * @param string $line_color Color for the GIS LINESTRING object
  107. * @param array $scale_data Array containing data related to scaling
  108. * @param TCPDF $pdf TCPDF instance
  109. *
  110. * @return TCPDF the modified TCPDF instance
  111. * @access public
  112. */
  113. public function prepareRowAsPdf($spatial, $label, $line_color, $scale_data, $pdf)
  114. {
  115. // allocate colors
  116. $red = hexdec(substr($line_color, 1, 2));
  117. $green = hexdec(substr($line_color, 3, 2));
  118. $blue = hexdec(substr($line_color, 4, 2));
  119. $line = array('width' => 1.5, 'color' => array($red, $green, $blue));
  120. // Trim to remove leading 'LINESTRING(' and trailing ')'
  121. $linesrting = substr($spatial, 11, (strlen($spatial) - 12));
  122. $points_arr = $this->extractPoints($linesrting, $scale_data);
  123. foreach ($points_arr as $point) {
  124. if (! isset($temp_point)) {
  125. $temp_point = $point;
  126. } else {
  127. // draw line section
  128. $pdf->Line(
  129. $temp_point[0], $temp_point[1],
  130. $point[0], $point[1], $line
  131. );
  132. $temp_point = $point;
  133. }
  134. }
  135. // print label
  136. if (isset($label) && trim($label) != '') {
  137. $pdf->SetXY($points_arr[1][0], $points_arr[1][1]);
  138. $pdf->SetFontSize(5);
  139. $pdf->Cell(0, 0, trim($label));
  140. }
  141. return $pdf;
  142. }
  143. /**
  144. * Prepares and returns the code related to a row in the GIS dataset as SVG.
  145. *
  146. * @param string $spatial GIS LINESTRING object
  147. * @param string $label Label for the GIS LINESTRING object
  148. * @param string $line_color Color for the GIS LINESTRING object
  149. * @param array $scale_data Array containing data related to scaling
  150. *
  151. * @return string the code related to a row in the GIS dataset
  152. * @access public
  153. */
  154. public function prepareRowAsSvg($spatial, $label, $line_color, $scale_data)
  155. {
  156. $line_options = array(
  157. 'name' => $label,
  158. 'id' => $label . rand(),
  159. 'class' => 'linestring vector',
  160. 'fill' => 'none',
  161. 'stroke' => $line_color,
  162. 'stroke-width'=> 2,
  163. );
  164. // Trim to remove leading 'LINESTRING(' and trailing ')'
  165. $linesrting = substr($spatial, 11, (strlen($spatial) - 12));
  166. $points_arr = $this->extractPoints($linesrting, $scale_data);
  167. $row = '<polyline points="';
  168. foreach ($points_arr as $point) {
  169. $row .= $point[0] . ',' . $point[1] . ' ';
  170. }
  171. $row .= '"';
  172. foreach ($line_options as $option => $val) {
  173. $row .= ' ' . $option . '="' . trim($val) . '"';
  174. }
  175. $row .= '/>';
  176. return $row;
  177. }
  178. /**
  179. * Prepares JavaScript related to a row in the GIS dataset
  180. * to visualize it with OpenLayers.
  181. *
  182. * @param string $spatial GIS LINESTRING object
  183. * @param int $srid Spatial reference ID
  184. * @param string $label Label for the GIS LINESTRING object
  185. * @param string $line_color Color for the GIS LINESTRING object
  186. * @param array $scale_data Array containing data related to scaling
  187. *
  188. * @return string JavaScript related to a row in the GIS dataset
  189. * @access public
  190. */
  191. public function prepareRowAsOl($spatial, $srid, $label, $line_color, $scale_data)
  192. {
  193. $style_options = array(
  194. 'strokeColor' => $line_color,
  195. 'strokeWidth' => 2,
  196. 'label' => $label,
  197. 'fontSize' => 10,
  198. );
  199. if ($srid == 0) {
  200. $srid = 4326;
  201. }
  202. $result = $this->getBoundsForOl($srid, $scale_data);
  203. // Trim to remove leading 'LINESTRING(' and trailing ')'
  204. $linesrting = substr($spatial, 11, (strlen($spatial) - 12));
  205. $points_arr = $this->extractPoints($linesrting, null);
  206. $result .= 'vectorLayer.addFeatures(new OpenLayers.Feature.Vector('
  207. . $this->getLineForOpenLayers($points_arr, $srid)
  208. . ', null, ' . json_encode($style_options) . '));';
  209. return $result;
  210. }
  211. /**
  212. * Generate the WKT with the set of parameters passed by the GIS editor.
  213. *
  214. * @param array $gis_data GIS data
  215. * @param int $index Index into the parameter object
  216. * @param string $empty Value for empty points
  217. *
  218. * @return string WKT with the set of parameters passed by the GIS editor
  219. * @access public
  220. */
  221. public function generateWkt($gis_data, $index, $empty = '')
  222. {
  223. $no_of_points = isset($gis_data[$index]['LINESTRING']['no_of_points'])
  224. ? $gis_data[$index]['LINESTRING']['no_of_points'] : 2;
  225. if ($no_of_points < 2) {
  226. $no_of_points = 2;
  227. }
  228. $wkt = 'LINESTRING(';
  229. for ($i = 0; $i < $no_of_points; $i++) {
  230. $wkt .= ((isset($gis_data[$index]['LINESTRING'][$i]['x'])
  231. && trim($gis_data[$index]['LINESTRING'][$i]['x']) != '')
  232. ? $gis_data[$index]['LINESTRING'][$i]['x'] : $empty)
  233. . ' ' . ((isset($gis_data[$index]['LINESTRING'][$i]['y'])
  234. && trim($gis_data[$index]['LINESTRING'][$i]['y']) != '')
  235. ? $gis_data[$index]['LINESTRING'][$i]['y'] : $empty) . ',';
  236. }
  237. $wkt = substr($wkt, 0, strlen($wkt) - 1);
  238. $wkt .= ')';
  239. return $wkt;
  240. }
  241. /**
  242. * Generate parameters for the GIS data editor from the value of the GIS column.
  243. *
  244. * @param string $value of the GIS column
  245. * @param int $index of the geometry
  246. *
  247. * @return array params for the GIS data editor from the value of the GIS column
  248. * @access public
  249. */
  250. public function generateParams($value, $index = -1)
  251. {
  252. if ($index == -1) {
  253. $index = 0;
  254. $params = array();
  255. $data = PMA_GIS_Geometry::generateParams($value);
  256. $params['srid'] = $data['srid'];
  257. $wkt = $data['wkt'];
  258. } else {
  259. $params[$index]['gis_type'] = 'LINESTRING';
  260. $wkt = $value;
  261. }
  262. // Trim to remove leading 'LINESTRING(' and trailing ')'
  263. $linestring = substr($wkt, 11, (strlen($wkt) - 12));
  264. $points_arr = $this->extractPoints($linestring, null);
  265. $no_of_points = count($points_arr);
  266. $params[$index]['LINESTRING']['no_of_points'] = $no_of_points;
  267. for ($i = 0; $i < $no_of_points; $i++) {
  268. $params[$index]['LINESTRING'][$i]['x'] = $points_arr[$i][0];
  269. $params[$index]['LINESTRING'][$i]['y'] = $points_arr[$i][1];
  270. }
  271. return $params;
  272. }
  273. }
  274. ?>