GisGeometry.php 12 KB

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