PageRenderTime 26ms CodeModel.GetById 14ms RepoModel.GetById 0ms app.codeStats 0ms

/libraries/gis/pma_gis_geometrycollection.php

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