GisGeometry.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399
  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. // Extract coordinates of the point
  204. $cordinates = explode(" ", $point);
  205. if (isset($cordinates[0]) && trim($cordinates[0]) != ''
  206. && isset($cordinates[1])
  207. && trim($cordinates[1]) != ''
  208. ) {
  209. if ($scale_data != null) {
  210. $x = ($cordinates[0] - $scale_data['x']) * $scale_data['scale'];
  211. $y = $scale_data['height']
  212. - ($cordinates[1] - $scale_data['y']) * $scale_data['scale'];
  213. } else {
  214. $x = floatval(trim($cordinates[0]));
  215. $y = floatval(trim($cordinates[1]));
  216. }
  217. } else {
  218. $x = 0;
  219. $y = 0;
  220. }
  221. if (!$linear) {
  222. $points_arr[] = array($x, $y);
  223. } else {
  224. $points_arr[] = $x;
  225. $points_arr[] = $y;
  226. }
  227. }
  228. return $points_arr;
  229. }
  230. /**
  231. * Generates JavaScript for adding an array of polygons to OpenLayers.
  232. *
  233. * @param array $polygons x and y coordinates for each polygon
  234. * @param string $srid spatial reference id
  235. *
  236. * @return string JavaScript for adding an array of polygons to OpenLayers
  237. * @access protected
  238. */
  239. protected function getPolygonArrayForOpenLayers(array $polygons, $srid)
  240. {
  241. $ol_array = 'new Array(';
  242. foreach ($polygons as $polygon) {
  243. $rings = explode("),(", $polygon);
  244. $ol_array .= $this->getPolygonForOpenLayers($rings, $srid) . ', ';
  245. }
  246. $ol_array
  247. = mb_substr(
  248. $ol_array,
  249. 0,
  250. mb_strlen($ol_array) - 2
  251. );
  252. $ol_array .= ')';
  253. return $ol_array;
  254. }
  255. /**
  256. * Generates JavaScript for adding points for OpenLayers polygon.
  257. *
  258. * @param array $polygon x and y coordinates for each line
  259. * @param string $srid spatial reference id
  260. *
  261. * @return string JavaScript for adding points for OpenLayers polygon
  262. * @access protected
  263. */
  264. protected function getPolygonForOpenLayers(array $polygon, $srid)
  265. {
  266. return 'new OpenLayers.Geometry.Polygon('
  267. . $this->getLineArrayForOpenLayers($polygon, $srid, false)
  268. . ')';
  269. }
  270. /**
  271. * Generates JavaScript for adding an array of LineString
  272. * or LineRing to OpenLayers.
  273. *
  274. * @param array $lines x and y coordinates for each line
  275. * @param string $srid spatial reference id
  276. * @param bool $is_line_string whether it's an array of LineString
  277. *
  278. * @return string JavaScript for adding an array of LineString
  279. * or LineRing to OpenLayers
  280. * @access protected
  281. */
  282. protected function getLineArrayForOpenLayers(
  283. array $lines,
  284. $srid,
  285. $is_line_string = true
  286. ) {
  287. $ol_array = 'new Array(';
  288. foreach ($lines as $line) {
  289. $points_arr = $this->extractPoints($line, null);
  290. $ol_array .= $this->getLineForOpenLayers(
  291. $points_arr,
  292. $srid,
  293. $is_line_string
  294. );
  295. $ol_array .= ', ';
  296. }
  297. $ol_array
  298. = mb_substr(
  299. $ol_array,
  300. 0,
  301. mb_strlen($ol_array) - 2
  302. );
  303. $ol_array .= ')';
  304. return $ol_array;
  305. }
  306. /**
  307. * Generates JavaScript for adding a LineString or LineRing to OpenLayers.
  308. *
  309. * @param array $points_arr x and y coordinates for each point
  310. * @param string $srid spatial reference id
  311. * @param bool $is_line_string whether it's a LineString
  312. *
  313. * @return string JavaScript for adding a LineString or LineRing to OpenLayers
  314. * @access protected
  315. */
  316. protected function getLineForOpenLayers(
  317. array $points_arr,
  318. $srid,
  319. $is_line_string = true
  320. ) {
  321. return 'new OpenLayers.Geometry.'
  322. . ($is_line_string ? 'LineString' : 'LinearRing') . '('
  323. . $this->getPointsArrayForOpenLayers($points_arr, $srid)
  324. . ')';
  325. }
  326. /**
  327. * Generates JavaScript for adding an array of points to OpenLayers.
  328. *
  329. * @param array $points_arr x and y coordinates for each point
  330. * @param string $srid spatial reference id
  331. *
  332. * @return string JavaScript for adding an array of points to OpenLayers
  333. * @access protected
  334. */
  335. protected function getPointsArrayForOpenLayers(array $points_arr, $srid)
  336. {
  337. $ol_array = 'new Array(';
  338. foreach ($points_arr as $point) {
  339. $ol_array .= $this->getPointForOpenLayers($point, $srid) . ', ';
  340. }
  341. $ol_array
  342. = mb_substr(
  343. $ol_array,
  344. 0,
  345. mb_strlen($ol_array) - 2
  346. );
  347. $ol_array .= ')';
  348. return $ol_array;
  349. }
  350. /**
  351. * Generates JavaScript for adding a point to OpenLayers.
  352. *
  353. * @param array $point array containing the x and y coordinates of the point
  354. * @param string $srid spatial reference id
  355. *
  356. * @return string JavaScript for adding points to OpenLayers
  357. * @access protected
  358. */
  359. protected function getPointForOpenLayers(array $point, $srid)
  360. {
  361. return '(new OpenLayers.Geometry.Point(' . $point[0] . ',' . $point[1] . '))'
  362. . '.transform(new OpenLayers.Projection("EPSG:'
  363. . intval($srid) . '"), map.getProjectionObject())';
  364. }
  365. }