GisGeometry.php 13 KB

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