GisLineString.php 11 KB

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