GIS_Multilinestring.class.php 12 KB

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