GisGeometry.php 13 KB

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