PageRenderTime 40ms CodeModel.GetById 17ms RepoModel.GetById 0ms app.codeStats 0ms

/phpMyAdmin/libraries/gis/pma_gis_geometrycollection.php

https://bitbucket.org/izubizarreta/https-bitbucket.org-bityvip
PHP | 307 lines | 174 code | 32 blank | 101 comment | 24 complexity | 02aa2f087feccc1ca005a51bde040f3b MD5 | raw file
Possible License(s): LGPL-3.0, LGPL-2.0, JSON, GPL-2.0, BSD-3-Clause, LGPL-2.1, MIT
  1. <?php
  2. /**
  3. * Handles the visualization of GIS GEOMETRYCOLLECTION objects.
  4. *
  5. * @package PhpMyAdmin-GIS
  6. */
  7. class PMA_GIS_Geometrycollection extends PMA_GIS_Geometry
  8. {
  9. // Hold the singleton instance of the class
  10. private static $_instance;
  11. /**
  12. * A private constructor; prevents direct creation of object.
  13. */
  14. private function __construct()
  15. {
  16. }
  17. /**
  18. * Returns the singleton.
  19. *
  20. * @return the singleton
  21. */
  22. public static function singleton()
  23. {
  24. if (!isset(self::$_instance)) {
  25. $class = __CLASS__;
  26. self::$_instance = new $class;
  27. }
  28. return self::$_instance;
  29. }
  30. /**
  31. * Scales each row.
  32. *
  33. * @param string $spatial spatial data of a row
  34. *
  35. * @return array containing the min, max values for x and y cordinates
  36. */
  37. public function scaleRow($spatial)
  38. {
  39. $min_max = array();
  40. // Trim to remove leading 'GEOMETRYCOLLECTION(' and trailing ')'
  41. $goem_col = substr($spatial, 19, (strlen($spatial) - 20));
  42. // Split the geometry collection object to get its constituents.
  43. $sub_parts = $this->_explodeGeomCol($goem_col);
  44. foreach ($sub_parts as $sub_part) {
  45. $type_pos = stripos($sub_part, '(');
  46. $type = substr($sub_part, 0, $type_pos);
  47. $gis_obj = PMA_GIS_Factory::factory($type);
  48. if (! $gis_obj) {
  49. continue;
  50. }
  51. $scale_data = $gis_obj->scaleRow($sub_part);
  52. // Upadate minimum/maximum values for x and y cordinates.
  53. $c_maxX = (float) $scale_data['maxX'];
  54. if (! isset($min_max['maxX']) || $c_maxX > $min_max['maxX']) {
  55. $min_max['maxX'] = $c_maxX;
  56. }
  57. $c_minX = (float) $scale_data['minX'];
  58. if (! isset($min_max['minX']) || $c_minX < $min_max['minX']) {
  59. $min_max['minX'] = $c_minX;
  60. }
  61. $c_maxY = (float) $scale_data['maxY'];
  62. if (! isset($min_max['maxY']) || $c_maxY > $min_max['maxY']) {
  63. $min_max['maxY'] = $c_maxY;
  64. }
  65. $c_minY = (float) $scale_data['minY'];
  66. if (! isset($min_max['minY']) || $c_minY < $min_max['minY']) {
  67. $min_max['minY'] = $c_minY;
  68. }
  69. }
  70. return $min_max;
  71. }
  72. /**
  73. * Adds to the PNG image object, the data related to a row in the GIS dataset.
  74. *
  75. * @param string $spatial GIS GEOMETRYCOLLECTION object
  76. * @param string $label Label for the GIS GEOMETRYCOLLECTION object
  77. * @param string $color Color for the GIS GEOMETRYCOLLECTION object
  78. * @param array $scale_data Array containing data related to scaling
  79. * @param image $image Image object
  80. *
  81. * @return the modified image object
  82. */
  83. public function prepareRowAsPng($spatial, $label, $color, $scale_data, $image)
  84. {
  85. // Trim to remove leading 'GEOMETRYCOLLECTION(' and trailing ')'
  86. $goem_col = substr($spatial, 19, (strlen($spatial) - 20));
  87. // Split the geometry collection object to get its constituents.
  88. $sub_parts = $this->_explodeGeomCol($goem_col);
  89. foreach ($sub_parts as $sub_part) {
  90. $type_pos = stripos($sub_part, '(');
  91. $type = substr($sub_part, 0, $type_pos);
  92. $gis_obj = PMA_GIS_Factory::factory($type);
  93. if (! $gis_obj) {
  94. continue;
  95. }
  96. $image = $gis_obj->prepareRowAsPng($sub_part, $label, $color, $scale_data, $image);
  97. }
  98. return $image;
  99. }
  100. /**
  101. * Adds to the TCPDF instance, the data related to a row in the GIS dataset.
  102. *
  103. * @param string $spatial GIS GEOMETRYCOLLECTION object
  104. * @param string $label Label for the GIS GEOMETRYCOLLECTION object
  105. * @param string $color Color for the GIS GEOMETRYCOLLECTION object
  106. * @param array $scale_data Array containing data related to scaling
  107. * @param image $pdf TCPDF instance
  108. *
  109. * @return the modified TCPDF instance
  110. */
  111. public function prepareRowAsPdf($spatial, $label, $color, $scale_data, $pdf)
  112. {
  113. // Trim to remove leading 'GEOMETRYCOLLECTION(' and trailing ')'
  114. $goem_col = substr($spatial, 19, (strlen($spatial) - 20));
  115. // Split the geometry collection object to get its constituents.
  116. $sub_parts = $this->_explodeGeomCol($goem_col);
  117. foreach ($sub_parts as $sub_part) {
  118. $type_pos = stripos($sub_part, '(');
  119. $type = substr($sub_part, 0, $type_pos);
  120. $gis_obj = PMA_GIS_Factory::factory($type);
  121. if (! $gis_obj) {
  122. continue;
  123. }
  124. $pdf = $gis_obj->prepareRowAsPdf($sub_part, $label, $color, $scale_data, $pdf);
  125. }
  126. return $pdf;
  127. }
  128. /**
  129. * Prepares and returns the code related to a row in the GIS dataset as SVG.
  130. *
  131. * @param string $spatial GIS GEOMETRYCOLLECTION object
  132. * @param string $label Label for the GIS GEOMETRYCOLLECTION object
  133. * @param string $color Color for the GIS GEOMETRYCOLLECTION object
  134. * @param array $scale_data Array containing data related to scaling
  135. *
  136. * @return the code related to a row in the GIS dataset
  137. */
  138. public function prepareRowAsSvg($spatial, $label, $color, $scale_data)
  139. {
  140. $row = '';
  141. // Trim to remove leading 'GEOMETRYCOLLECTION(' and trailing ')'
  142. $goem_col = substr($spatial, 19, (strlen($spatial) - 20));
  143. // Split the geometry collection object to get its constituents.
  144. $sub_parts = $this->_explodeGeomCol($goem_col);
  145. foreach ($sub_parts as $sub_part) {
  146. $type_pos = stripos($sub_part, '(');
  147. $type = substr($sub_part, 0, $type_pos);
  148. $gis_obj = PMA_GIS_Factory::factory($type);
  149. if (! $gis_obj) {
  150. continue;
  151. }
  152. $row .= $gis_obj->prepareRowAsSvg($sub_part, $label, $color, $scale_data);
  153. }
  154. return $row;
  155. }
  156. /**
  157. * Prepares JavaScript related to a row in the GIS dataset
  158. * to visualize it with OpenLayers.
  159. *
  160. * @param string $spatial GIS GEOMETRYCOLLECTION object
  161. * @param int $srid Spatial reference ID
  162. * @param string $label Label for the GIS GEOMETRYCOLLECTION object
  163. * @param string $color Color for the GIS GEOMETRYCOLLECTION object
  164. * @param array $scale_data Array containing data related to scaling
  165. *
  166. * @return JavaScript related to a row in the GIS dataset
  167. */
  168. public function prepareRowAsOl($spatial, $srid, $label, $color, $scale_data)
  169. {
  170. $row = '';
  171. // Trim to remove leading 'GEOMETRYCOLLECTION(' and trailing ')'
  172. $goem_col = substr($spatial, 19, (strlen($spatial) - 20));
  173. // Split the geometry collection object to get its constituents.
  174. $sub_parts = $this->_explodeGeomCol($goem_col);
  175. foreach ($sub_parts as $sub_part) {
  176. $type_pos = stripos($sub_part, '(');
  177. $type = substr($sub_part, 0, $type_pos);
  178. $gis_obj = PMA_GIS_Factory::factory($type);
  179. if (! $gis_obj) {
  180. continue;
  181. }
  182. $row .= $gis_obj->prepareRowAsOl($sub_part, $srid, $label, $color, $scale_data);
  183. }
  184. return $row;
  185. }
  186. /**
  187. * Split the GEOMETRYCOLLECTION object and get its constituents.
  188. *
  189. * @param string $goem_col Geometry collection string
  190. *
  191. * @return the constituents of the geometry collection object
  192. */
  193. private function _explodeGeomCol($goem_col)
  194. {
  195. $sub_parts = array();
  196. $br_count = 0;
  197. $start = 0;
  198. $count = 0;
  199. foreach (str_split($goem_col) as $char) {
  200. if ($char == '(') {
  201. $br_count++;
  202. } elseif ($char == ')') {
  203. $br_count--;
  204. if ($br_count == 0) {
  205. $sub_parts[] = substr($goem_col, $start, ($count + 1 - $start));
  206. $start = $count + 2;
  207. }
  208. }
  209. $count++;
  210. }
  211. return $sub_parts;
  212. }
  213. /**
  214. * Generate the WKT with the set of parameters passed by the GIS editor.
  215. *
  216. * @param array $gis_data GIS data
  217. * @param int $index Index into the parameter object
  218. * @param string $empty Value for empty points
  219. *
  220. * @return WKT with the set of parameters passed by the GIS editor
  221. */
  222. public function generateWkt($gis_data, $index, $empty = '')
  223. {
  224. $geom_count = (isset($gis_data['GEOMETRYCOLLECTION']['geom_count']))
  225. ? $gis_data['GEOMETRYCOLLECTION']['geom_count'] : 1;
  226. $wkt = 'GEOMETRYCOLLECTION(';
  227. for ($i = 0; $i < $geom_count; $i++) {
  228. if (isset($gis_data[$i]['gis_type'])) {
  229. $type = $gis_data[$i]['gis_type'];
  230. $gis_obj = PMA_GIS_Factory::factory($type);
  231. if (! $gis_obj) {
  232. continue;
  233. }
  234. $wkt .= $gis_obj->generateWkt($gis_data, $i, $empty) . ',';
  235. }
  236. }
  237. if (isset($gis_data[0]['gis_type'])) {
  238. $wkt = substr($wkt, 0, strlen($wkt) - 1);
  239. }
  240. $wkt .= ')';
  241. return $wkt;
  242. }
  243. /** Generate parameters for the GIS data editor from the value of the GIS column.
  244. *
  245. * @param string $value of the GIS column
  246. * @param index $index of the geometry
  247. *
  248. * @return parameters for the GIS data editor from the value of the GIS column
  249. */
  250. public function generateParams($value)
  251. {
  252. $params = array();
  253. $data = PMA_GIS_Geometry::generateParams($value);
  254. $params['srid'] = $data['srid'];
  255. $wkt = $data['wkt'];
  256. // Trim to remove leading 'GEOMETRYCOLLECTION(' and trailing ')'
  257. $goem_col = substr($wkt, 19, (strlen($wkt) - 20));
  258. // Split the geometry collection object to get its constituents.
  259. $sub_parts = $this->_explodeGeomCol($goem_col);
  260. $params['GEOMETRYCOLLECTION']['geom_count'] = count($sub_parts);
  261. $i = 0;
  262. foreach ($sub_parts as $sub_part) {
  263. $type_pos = stripos($sub_part, '(');
  264. $type = substr($sub_part, 0, $type_pos);
  265. $gis_obj = PMA_GIS_Factory::factory($type);
  266. if (! $gis_obj) {
  267. continue;
  268. }
  269. $params = array_merge($params, $gis_obj->generateParams($sub_part, $i));
  270. $i++;
  271. }
  272. return $params;
  273. }
  274. }
  275. ?>