GisGeometry.php 13 KB

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