PageRenderTime 49ms CodeModel.GetById 16ms RepoModel.GetById 0ms app.codeStats 0ms

/pma/libraries/gis/pma_gis_multilinestring.php

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