Upgraded phpmyadmin to 4.0.4 (All Languages) - No modifications yet
[openemr.git] / phpmyadmin / libraries / gis / pma_gis_geometrycollection.php
blob91c5a588c9ecc78d78ff40f73b587fcc2db72b11
1 <?php
2 /* vim: set expandtab sw=4 ts=4 sts=4: */
3 /**
4 * Handles actions related to GIS GEOMETRYCOLLECTION objects
6 * @package PhpMyAdmin-GIS
7 */
9 if (! defined('PHPMYADMIN')) {
10 exit;
13 /**
14 * Handles actions related to GIS GEOMETRYCOLLECTION objects
16 * @package PhpMyAdmin-GIS
18 class PMA_GIS_Geometrycollection extends PMA_GIS_Geometry
20 // Hold the singleton instance of the class
21 private static $_instance;
23 /**
24 * A private constructor; prevents direct creation of object.
26 * @access private
28 private function __construct()
32 /**
33 * Returns the singleton.
35 * @return the singleton
36 * @access public
38 public static function singleton()
40 if (!isset(self::$_instance)) {
41 $class = __CLASS__;
42 self::$_instance = new $class;
45 return self::$_instance;
48 /**
49 * Scales each row.
51 * @param string $spatial spatial data of a row
53 * @return array array containing the min, max values for x and y cordinates
54 * @access public
56 public function scaleRow($spatial)
58 $min_max = array();
60 // Trim to remove leading 'GEOMETRYCOLLECTION(' and trailing ')'
61 $goem_col = substr($spatial, 19, (strlen($spatial) - 20));
63 // Split the geometry collection object to get its constituents.
64 $sub_parts = $this->_explodeGeomCol($goem_col);
66 foreach ($sub_parts as $sub_part) {
67 $type_pos = stripos($sub_part, '(');
68 $type = substr($sub_part, 0, $type_pos);
70 $gis_obj = PMA_GIS_Factory::factory($type);
71 if (! $gis_obj) {
72 continue;
74 $scale_data = $gis_obj->scaleRow($sub_part);
76 // Upadate minimum/maximum values for x and y cordinates.
77 $c_maxX = (float) $scale_data['maxX'];
78 if (! isset($min_max['maxX']) || $c_maxX > $min_max['maxX']) {
79 $min_max['maxX'] = $c_maxX;
82 $c_minX = (float) $scale_data['minX'];
83 if (! isset($min_max['minX']) || $c_minX < $min_max['minX']) {
84 $min_max['minX'] = $c_minX;
87 $c_maxY = (float) $scale_data['maxY'];
88 if (! isset($min_max['maxY']) || $c_maxY > $min_max['maxY']) {
89 $min_max['maxY'] = $c_maxY;
92 $c_minY = (float) $scale_data['minY'];
93 if (! isset($min_max['minY']) || $c_minY < $min_max['minY']) {
94 $min_max['minY'] = $c_minY;
97 return $min_max;
101 * Adds to the PNG image object, the data related to a row in the GIS dataset.
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 object $image image object
109 * @return object the modified image object
110 * @access public
112 public function prepareRowAsPng($spatial, $label, $color, $scale_data, $image)
114 // Trim to remove leading 'GEOMETRYCOLLECTION(' and trailing ')'
115 $goem_col = substr($spatial, 19, (strlen($spatial) - 20));
116 // Split the geometry collection object to get its constituents.
117 $sub_parts = $this->_explodeGeomCol($goem_col);
119 foreach ($sub_parts as $sub_part) {
120 $type_pos = stripos($sub_part, '(');
121 $type = substr($sub_part, 0, $type_pos);
123 $gis_obj = PMA_GIS_Factory::factory($type);
124 if (! $gis_obj) {
125 continue;
127 $image = $gis_obj->prepareRowAsPng(
128 $sub_part, $label, $color, $scale_data, $image
131 return $image;
135 * Adds to the TCPDF instance, the data related to a row in the GIS dataset.
137 * @param string $spatial GIS GEOMETRYCOLLECTION object
138 * @param string $label label for the GIS GEOMETRYCOLLECTION object
139 * @param string $color color for the GIS GEOMETRYCOLLECTION object
140 * @param array $scale_data array containing data related to scaling
141 * @param object $pdf TCPDF instance
143 * @return object the modified TCPDF instance
144 * @access pubilc
146 public function prepareRowAsPdf($spatial, $label, $color, $scale_data, $pdf)
148 // Trim to remove leading 'GEOMETRYCOLLECTION(' and trailing ')'
149 $goem_col = substr($spatial, 19, (strlen($spatial) - 20));
150 // Split the geometry collection object to get its constituents.
151 $sub_parts = $this->_explodeGeomCol($goem_col);
153 foreach ($sub_parts as $sub_part) {
154 $type_pos = stripos($sub_part, '(');
155 $type = substr($sub_part, 0, $type_pos);
157 $gis_obj = PMA_GIS_Factory::factory($type);
158 if (! $gis_obj) {
159 continue;
161 $pdf = $gis_obj->prepareRowAsPdf(
162 $sub_part, $label, $color, $scale_data, $pdf
165 return $pdf;
169 * Prepares and returns the code related to a row in the GIS dataset as SVG.
171 * @param string $spatial GIS GEOMETRYCOLLECTION object
172 * @param string $label label for the GIS GEOMETRYCOLLECTION object
173 * @param string $color color for the GIS GEOMETRYCOLLECTION object
174 * @param array $scale_data array containing data related to scaling
176 * @return string the code related to a row in the GIS dataset
177 * @access public
179 public function prepareRowAsSvg($spatial, $label, $color, $scale_data)
181 $row = '';
183 // Trim to remove leading 'GEOMETRYCOLLECTION(' and trailing ')'
184 $goem_col = substr($spatial, 19, (strlen($spatial) - 20));
185 // Split the geometry collection object to get its constituents.
186 $sub_parts = $this->_explodeGeomCol($goem_col);
188 foreach ($sub_parts as $sub_part) {
189 $type_pos = stripos($sub_part, '(');
190 $type = substr($sub_part, 0, $type_pos);
192 $gis_obj = PMA_GIS_Factory::factory($type);
193 if (! $gis_obj) {
194 continue;
196 $row .= $gis_obj->prepareRowAsSvg(
197 $sub_part, $label, $color, $scale_data
200 return $row;
204 * Prepares JavaScript related to a row in the GIS dataset
205 * to visualize it with OpenLayers.
207 * @param string $spatial GIS GEOMETRYCOLLECTION object
208 * @param int $srid spatial reference ID
209 * @param string $label label for the GIS GEOMETRYCOLLECTION object
210 * @param string $color color for the GIS GEOMETRYCOLLECTION object
211 * @param array $scale_data array containing data related to scaling
213 * @return string JavaScript related to a row in the GIS dataset
214 * @access public
216 public function prepareRowAsOl($spatial, $srid, $label, $color, $scale_data)
218 $row = '';
220 // Trim to remove leading 'GEOMETRYCOLLECTION(' and trailing ')'
221 $goem_col = substr($spatial, 19, (strlen($spatial) - 20));
222 // Split the geometry collection object to get its constituents.
223 $sub_parts = $this->_explodeGeomCol($goem_col);
225 foreach ($sub_parts as $sub_part) {
226 $type_pos = stripos($sub_part, '(');
227 $type = substr($sub_part, 0, $type_pos);
229 $gis_obj = PMA_GIS_Factory::factory($type);
230 if (! $gis_obj) {
231 continue;
233 $row .= $gis_obj->prepareRowAsOl(
234 $sub_part, $srid, $label, $color, $scale_data
237 return $row;
241 * Splits the GEOMETRYCOLLECTION object and get its constituents.
243 * @param string $goem_col geometry collection string
245 * @return array the constituents of the geometry collection object
246 * @access private
248 private function _explodeGeomCol($goem_col)
250 $sub_parts = array();
251 $br_count = 0;
252 $start = 0;
253 $count = 0;
254 foreach (str_split($goem_col) as $char) {
255 if ($char == '(') {
256 $br_count++;
257 } elseif ($char == ')') {
258 $br_count--;
259 if ($br_count == 0) {
260 $sub_parts[] = substr($goem_col, $start, ($count + 1 - $start));
261 $start = $count + 2;
264 $count++;
266 return $sub_parts;
270 * Generates the WKT with the set of parameters passed by the GIS editor.
272 * @param array $gis_data GIS data
273 * @param int $index index into the parameter object
274 * @param string $empty value for empty points
276 * @return string WKT with the set of parameters passed by the GIS editor
277 * @access public
279 public function generateWkt($gis_data, $index, $empty = '')
281 $geom_count = (isset($gis_data['GEOMETRYCOLLECTION']['geom_count']))
282 ? $gis_data['GEOMETRYCOLLECTION']['geom_count'] : 1;
283 $wkt = 'GEOMETRYCOLLECTION(';
284 for ($i = 0; $i < $geom_count; $i++) {
285 if (isset($gis_data[$i]['gis_type'])) {
286 $type = $gis_data[$i]['gis_type'];
287 $gis_obj = PMA_GIS_Factory::factory($type);
288 if (! $gis_obj) {
289 continue;
291 $wkt .= $gis_obj->generateWkt($gis_data, $i, $empty) . ',';
294 if (isset($gis_data[0]['gis_type'])) {
295 $wkt = substr($wkt, 0, strlen($wkt) - 1);
297 $wkt .= ')';
298 return $wkt;
302 * Generates parameters for the GIS data editor from the value of the GIS column.
304 * @param string $value of the GIS column
306 * @return array parameters for the GIS editor from the value of the GIS column
307 * @access public
309 public function generateParams($value)
311 $params = array();
312 $data = PMA_GIS_Geometry::generateParams($value);
313 $params['srid'] = $data['srid'];
314 $wkt = $data['wkt'];
316 // Trim to remove leading 'GEOMETRYCOLLECTION(' and trailing ')'
317 $goem_col = substr($wkt, 19, (strlen($wkt) - 20));
318 // Split the geometry collection object to get its constituents.
319 $sub_parts = $this->_explodeGeomCol($goem_col);
320 $params['GEOMETRYCOLLECTION']['geom_count'] = count($sub_parts);
322 $i = 0;
323 foreach ($sub_parts as $sub_part) {
324 $type_pos = stripos($sub_part, '(');
325 $type = substr($sub_part, 0, $type_pos);
326 $gis_obj = PMA_GIS_Factory::factory($type);
327 if (! $gis_obj) {
328 continue;
330 $params = array_merge($params, $gis_obj->generateParams($sub_part, $i));
331 $i++;
333 return $params;