/dbadmin.com/public/libraries/gis/pma_gis_geometry.php

https://github.com/slikk66/DbAdmin · PHP · 361 lines · 155 code · 28 blank · 178 comment · 22 complexity · 4f7fa273ddfd4b0e1b9eeb8ceb045d4e MD5 · raw file

  1. <?php
  2. /* vim: set expandtab sw=4 ts=4 sts=4: */
  3. /**
  4. * Base class for all GIS data type classes
  5. *
  6. * @package PhpMyAdmin-GIS
  7. */
  8. if (! defined('PHPMYADMIN')) {
  9. exit;
  10. }
  11. /**
  12. * Base class for all GIS data type classes.
  13. *
  14. * @package PhpMyAdmin-GIS
  15. */
  16. abstract class PMA_GIS_Geometry
  17. {
  18. /**
  19. * Prepares and returns the code related to a row in the GIS dataset as SVG.
  20. *
  21. * @param string $spatial GIS data object
  22. * @param string $label label for the GIS data object
  23. * @param string $color color for the GIS data object
  24. * @param array $scale_data data related to scaling
  25. *
  26. * @return string the code related to a row in the GIS dataset
  27. * @access public
  28. */
  29. public abstract function prepareRowAsSvg($spatial, $label, $color, $scale_data);
  30. /**
  31. * Adds to the PNG image object, the data related to a row in the GIS dataset.
  32. *
  33. * @param string $spatial GIS data object
  34. * @param string $label label for the GIS data object
  35. * @param string $color color for the GIS data object
  36. * @param array $scale_data array containing data related to scaling
  37. * @param object $image image object
  38. *
  39. * @return object the modified image object
  40. * @access public
  41. */
  42. public abstract function prepareRowAsPng($spatial, $label, $color,
  43. $scale_data, $image
  44. );
  45. /**
  46. * Adds to the TCPDF instance, the data related to a row in the GIS dataset.
  47. *
  48. * @param string $spatial GIS data object
  49. * @param string $label label for the GIS data object
  50. * @param string $color color for the GIS data object
  51. * @param array $scale_data array containing data related to scaling
  52. * @param object $pdf TCPDF instance
  53. *
  54. * @return object the modified TCPDF instance
  55. * @access public
  56. */
  57. public abstract function prepareRowAsPdf($spatial, $label, $color,
  58. $scale_data, $pdf
  59. );
  60. /**
  61. * Prepares the JavaScript related to a row in the GIS dataset
  62. * to visualize it with OpenLayers.
  63. *
  64. * @param string $spatial GIS data object
  65. * @param int $srid spatial reference ID
  66. * @param string $label label for the GIS data object
  67. * @param string $color color for the GIS data object
  68. * @param array $scale_data array containing data related to scaling
  69. *
  70. * @return string the JavaScript related to a row in the GIS dataset
  71. * @access public
  72. */
  73. public abstract function prepareRowAsOl($spatial, $srid, $label,
  74. $color, $scale_data
  75. );
  76. /**
  77. * Scales each row.
  78. *
  79. * @param string $spatial spatial data of a row
  80. *
  81. * @return array array containing the min, max values for x and y cordinates
  82. * @access public
  83. */
  84. public abstract function scaleRow($spatial);
  85. /**
  86. * Generates the WKT with the set of parameters passed by the GIS editor.
  87. *
  88. * @param array $gis_data GIS data
  89. * @param int $index index into the parameter object
  90. * @param string $empty value for empty points
  91. *
  92. * @return string WKT with the set of parameters passed by the GIS editor
  93. * @access public
  94. */
  95. public abstract function generateWkt($gis_data, $index, $empty = '');
  96. /**
  97. * Returns OpenLayers.Bounds object that correspond to the bounds of GIS data.
  98. *
  99. * @param string $srid spatial reference ID
  100. * @param array $scale_data data related to scaling
  101. *
  102. * @return string OpenLayers.Bounds object that
  103. * correspond to the bounds of GIS data
  104. * @access protected
  105. */
  106. protected function getBoundsForOl($srid, $scale_data)
  107. {
  108. return 'bound = new OpenLayers.Bounds(); '
  109. . 'bound.extend(new OpenLayers.LonLat('
  110. . $scale_data['minX'] . ', ' . $scale_data['minY']
  111. . ').transform(new OpenLayers.Projection("EPSG:'
  112. . $srid . '"), map.getProjectionObject())); '
  113. . 'bound.extend(new OpenLayers.LonLat('
  114. . $scale_data['maxX'] . ', ' . $scale_data['maxY']
  115. . ').transform(new OpenLayers.Projection("EPSG:'
  116. . $srid . '"), map.getProjectionObject()));';
  117. }
  118. /**
  119. * Updates the min, max values with the given point set.
  120. *
  121. * @param string $point_set point set
  122. * @param array $min_max existing min, max values
  123. *
  124. * @return array the updated min, max values
  125. * @access protected
  126. */
  127. protected function setMinMax($point_set, $min_max)
  128. {
  129. // Seperate each point
  130. $points = explode(",", $point_set);
  131. foreach ($points as $point) {
  132. // Extract cordinates of the point
  133. $cordinates = explode(" ", $point);
  134. $x = (float) $cordinates[0];
  135. if (! isset($min_max['maxX']) || $x > $min_max['maxX']) {
  136. $min_max['maxX'] = $x;
  137. }
  138. if (! isset($min_max['minX']) || $x < $min_max['minX']) {
  139. $min_max['minX'] = $x;
  140. }
  141. $y = (float) $cordinates[1];
  142. if (! isset($min_max['maxY']) || $y > $min_max['maxY']) {
  143. $min_max['maxY'] = $y;
  144. }
  145. if (! isset($min_max['minY']) || $y < $min_max['minY']) {
  146. $min_max['minY'] = $y;
  147. }
  148. }
  149. return $min_max;
  150. }
  151. /**
  152. * Generates parameters for the GIS data editor from the value of the GIS column.
  153. * This method performs common work.
  154. * More specific work is performed by each of the geom classes.
  155. *
  156. * @param string $value value of the GIS column
  157. *
  158. * @return array parameters for the GIS editor from the value of the GIS column
  159. * @access protected
  160. */
  161. protected function generateParams($value)
  162. {
  163. $geom_types = '(POINT|MULTIPOINT|LINESTRING|MULTILINESTRING'
  164. . '|POLYGON|MULTIPOLYGON|GEOMETRYCOLLECTION)';
  165. $srid = 0;
  166. $wkt = '';
  167. if (preg_match("/^'" . $geom_types . "\(.*\)',[0-9]*$/i", $value)) {
  168. $last_comma = strripos($value, ",");
  169. $srid = trim(substr($value, $last_comma + 1));
  170. $wkt = trim(substr($value, 1, $last_comma - 2));
  171. } elseif (preg_match("/^" . $geom_types . "\(.*\)$/i", $value)) {
  172. $wkt = $value;
  173. }
  174. return array('srid' => $srid, 'wkt' => $wkt);
  175. }
  176. /**
  177. * Extracts points, scales and returns them as an array.
  178. *
  179. * @param string $point_set string of comma sperated points
  180. * @param array $scale_data data related to scaling
  181. * @param boolean $linear if true, as a 1D array, else as a 2D array
  182. *
  183. * @return array scaled points
  184. * @access protected
  185. */
  186. protected function extractPoints($point_set, $scale_data, $linear = false)
  187. {
  188. $points_arr = array();
  189. // Seperate each point
  190. $points = explode(",", $point_set);
  191. foreach ($points as $point) {
  192. // Extract cordinates of the point
  193. $cordinates = explode(" ", $point);
  194. if (isset($cordinates[0]) && trim($cordinates[0]) != ''
  195. && isset($cordinates[1]) && trim($cordinates[1]) != ''
  196. ) {
  197. if ($scale_data != null) {
  198. $x = ($cordinates[0] - $scale_data['x']) * $scale_data['scale'];
  199. $y = $scale_data['height']
  200. - ($cordinates[1] - $scale_data['y']) * $scale_data['scale'];
  201. } else {
  202. $x = trim($cordinates[0]);
  203. $y = trim($cordinates[1]);
  204. }
  205. } else {
  206. $x = '';
  207. $y = '';
  208. }
  209. if (! $linear) {
  210. $points_arr[] = array($x, $y);
  211. } else {
  212. $points_arr[] = $x;
  213. $points_arr[] = $y;
  214. }
  215. }
  216. return $points_arr;
  217. }
  218. /**
  219. * Generates JavaScript for adding an array of polygons to OpenLayers.
  220. *
  221. * @param array $polygons x and y coordinates for each polygon
  222. * @param string $srid spatial reference id
  223. *
  224. * @return string JavaScript for adding an array of polygons to OpenLayers
  225. * @access protected
  226. */
  227. protected function getPolygonArrayForOpenLayers($polygons, $srid)
  228. {
  229. $ol_array = 'new Array(';
  230. foreach ($polygons as $polygon) {
  231. $rings = explode("),(", $polygon);
  232. $ol_array .= $this->getPolygonForOpenLayers($rings, $srid) . ', ';
  233. }
  234. $ol_array = substr($ol_array, 0, strlen($ol_array) - 2);
  235. $ol_array .= ')';
  236. return $ol_array;
  237. }
  238. /**
  239. * Generates JavaScript for adding points for OpenLayers polygon.
  240. *
  241. * @param array $polygon x and y coordinates for each line
  242. * @param string $srid spatial reference id
  243. *
  244. * @return string JavaScript for adding points for OpenLayers polygon
  245. * @access protected
  246. */
  247. protected function getPolygonForOpenLayers($polygon, $srid)
  248. {
  249. return 'new OpenLayers.Geometry.Polygon('
  250. . $this->getLineArrayForOpenLayers($polygon, $srid, false)
  251. . ')';
  252. }
  253. /**
  254. * Generates JavaScript for adding an array of LineString
  255. * or LineRing to OpenLayers.
  256. *
  257. * @param array $lines x and y coordinates for each line
  258. * @param string $srid spatial reference id
  259. * @param bool $is_line_string whether it's an array of LineString
  260. *
  261. * @return string JavaScript for adding an array of LineString
  262. * or LineRing to OpenLayers
  263. * @access protected
  264. */
  265. protected function getLineArrayForOpenLayers($lines, $srid,
  266. $is_line_string = true
  267. ) {
  268. $ol_array = 'new Array(';
  269. foreach ($lines as $line) {
  270. $points_arr = $this->extractPoints($line, null);
  271. $ol_array .= $this->getLineForOpenLayers(
  272. $points_arr, $srid, $is_line_string
  273. );
  274. $ol_array .= ', ';
  275. }
  276. $ol_array = substr($ol_array, 0, strlen($ol_array) - 2);
  277. $ol_array .= ')';
  278. return $ol_array;
  279. }
  280. /**
  281. * Generates JavaScript for adding a LineString or LineRing to OpenLayers.
  282. *
  283. * @param array $points_arr x and y coordinates for each point
  284. * @param string $srid spatial reference id
  285. * @param bool $is_line_string whether it's a LineString
  286. *
  287. * @return string JavaScript for adding a LineString or LineRing to OpenLayers
  288. * @access protected
  289. */
  290. protected function getLineForOpenLayers($points_arr, $srid,
  291. $is_line_string = true
  292. ) {
  293. return 'new OpenLayers.Geometry.'
  294. . ($is_line_string ? 'LineString' : 'LinearRing') . '('
  295. . $this->getPointsArrayForOpenLayers($points_arr, $srid)
  296. . ')';
  297. }
  298. /**
  299. * Generates JavaScript for adding an array of points to OpenLayers.
  300. *
  301. * @param array $points_arr x and y coordinates for each point
  302. * @param string $srid spatial reference id
  303. *
  304. * @return string JavaScript for adding an array of points to OpenLayers
  305. * @access protected
  306. */
  307. protected function getPointsArrayForOpenLayers($points_arr, $srid)
  308. {
  309. $ol_array = 'new Array(';
  310. foreach ($points_arr as $point) {
  311. $ol_array .= $this->getPointForOpenLayers($point, $srid) . ', ';
  312. }
  313. $ol_array = substr($ol_array, 0, strlen($ol_array) - 2);
  314. $ol_array .= ')';
  315. return $ol_array;
  316. }
  317. /**
  318. * Generates JavaScript for adding a point to OpenLayers.
  319. *
  320. * @param array $point array containing the x and y coordinates of the point
  321. * @param string $srid spatial reference id
  322. *
  323. * @return string JavaScript for adding points to OpenLayers
  324. * @access protected
  325. */
  326. protected function getPointForOpenLayers($point, $srid)
  327. {
  328. return '(new OpenLayers.Geometry.Point(' . $point[0] . ',' . $point[1] . '))'
  329. . '.transform(new OpenLayers.Projection("EPSG:'
  330. . $srid . '"), map.getProjectionObject())';
  331. }
  332. }
  333. ?>