Minor improvement to prior commit.
[openemr.git] / phpmyadmin / libraries / gis / GIS_Geometrycollection.class.php
blobc972dd729b3cf9c349a1f96e81fb7f45501169ed
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 PMA_GIS_Geometrycollection 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 coordinates
54 * @access public
56 public function scaleRow($spatial)
58 $min_max = array();
60 // Trim to remove leading 'GEOMETRYCOLLECTION(' and trailing ')'
61 $goem_col = /*overload*/mb_substr(
62 $spatial,
63 19,
64 /*overload*/mb_strlen($spatial) - 20
67 // Split the geometry collection object to get its constituents.
68 $sub_parts = $this->_explodeGeomCol($goem_col);
70 foreach ($sub_parts as $sub_part) {
71 $type_pos = /*overload*/mb_stripos($sub_part, '(');
72 $type = /*overload*/mb_substr($sub_part, 0, $type_pos);
74 $gis_obj = PMA_GIS_Factory::factory($type);
75 if (! $gis_obj) {
76 continue;
78 $scale_data = $gis_obj->scaleRow($sub_part);
80 // Update minimum/maximum values for x and y coordinates.
81 $c_maxX = (float) $scale_data['maxX'];
82 if (! isset($min_max['maxX']) || $c_maxX > $min_max['maxX']) {
83 $min_max['maxX'] = $c_maxX;
86 $c_minX = (float) $scale_data['minX'];
87 if (! isset($min_max['minX']) || $c_minX < $min_max['minX']) {
88 $min_max['minX'] = $c_minX;
91 $c_maxY = (float) $scale_data['maxY'];
92 if (! isset($min_max['maxY']) || $c_maxY > $min_max['maxY']) {
93 $min_max['maxY'] = $c_maxY;
96 $c_minY = (float) $scale_data['minY'];
97 if (! isset($min_max['minY']) || $c_minY < $min_max['minY']) {
98 $min_max['minY'] = $c_minY;
101 return $min_max;
105 * Adds to the PNG image object, the data related to a row in the GIS dataset.
107 * @param string $spatial GIS GEOMETRYCOLLECTION object
108 * @param string $label label for the GIS GEOMETRYCOLLECTION object
109 * @param string $color color for the GIS GEOMETRYCOLLECTION object
110 * @param array $scale_data array containing data related to scaling
111 * @param object $image image object
113 * @return resource the modified image object
114 * @access public
116 public function prepareRowAsPng($spatial, $label, $color, $scale_data, $image)
118 // Trim to remove leading 'GEOMETRYCOLLECTION(' and trailing ')'
119 $goem_col = /*overload*/mb_substr(
120 $spatial,
122 /*overload*/mb_strlen($spatial) - 20
124 // Split the geometry collection object to get its constituents.
125 $sub_parts = $this->_explodeGeomCol($goem_col);
127 foreach ($sub_parts as $sub_part) {
128 $type_pos = /*overload*/mb_stripos($sub_part, '(');
129 $type = /*overload*/mb_substr($sub_part, 0, $type_pos);
131 $gis_obj = PMA_GIS_Factory::factory($type);
132 if (! $gis_obj) {
133 continue;
135 $image = $gis_obj->prepareRowAsPng(
136 $sub_part, $label, $color, $scale_data, $image
139 return $image;
143 * Adds to the TCPDF instance, the data related to a row in the GIS dataset.
145 * @param string $spatial GIS GEOMETRYCOLLECTION object
146 * @param string $label label for the GIS GEOMETRYCOLLECTION object
147 * @param string $color color for the GIS GEOMETRYCOLLECTION object
148 * @param array $scale_data array containing data related to scaling
149 * @param TCPDF $pdf TCPDF instance
151 * @return TCPDF the modified TCPDF instance
152 * @access public
154 public function prepareRowAsPdf($spatial, $label, $color, $scale_data, $pdf)
156 // Trim to remove leading 'GEOMETRYCOLLECTION(' and trailing ')'
157 $goem_col = /*overload*/mb_substr(
158 $spatial,
160 /*overload*/mb_strlen($spatial) - 20
162 // Split the geometry collection object to get its constituents.
163 $sub_parts = $this->_explodeGeomCol($goem_col);
165 foreach ($sub_parts as $sub_part) {
166 $type_pos = /*overload*/mb_stripos($sub_part, '(');
167 $type = /*overload*/mb_substr($sub_part, 0, $type_pos);
169 $gis_obj = PMA_GIS_Factory::factory($type);
170 if (! $gis_obj) {
171 continue;
173 $pdf = $gis_obj->prepareRowAsPdf(
174 $sub_part, $label, $color, $scale_data, $pdf
177 return $pdf;
181 * Prepares and returns the code related to a row in the GIS dataset as SVG.
183 * @param string $spatial GIS GEOMETRYCOLLECTION object
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
188 * @return string the code related to a row in the GIS dataset
189 * @access public
191 public function prepareRowAsSvg($spatial, $label, $color, $scale_data)
193 $row = '';
195 // Trim to remove leading 'GEOMETRYCOLLECTION(' and trailing ')'
196 $goem_col = /*overload*/mb_substr(
197 $spatial,
199 /*overload*/mb_strlen($spatial) - 20
201 // Split the geometry collection object to get its constituents.
202 $sub_parts = $this->_explodeGeomCol($goem_col);
204 foreach ($sub_parts as $sub_part) {
205 $type_pos = /*overload*/mb_stripos($sub_part, '(');
206 $type = /*overload*/mb_substr($sub_part, 0, $type_pos);
208 $gis_obj = PMA_GIS_Factory::factory($type);
209 if (! $gis_obj) {
210 continue;
212 $row .= $gis_obj->prepareRowAsSvg(
213 $sub_part, $label, $color, $scale_data
216 return $row;
220 * Prepares JavaScript related to a row in the GIS dataset
221 * to visualize it with OpenLayers.
223 * @param string $spatial GIS GEOMETRYCOLLECTION object
224 * @param int $srid spatial reference ID
225 * @param string $label label for the GIS GEOMETRYCOLLECTION object
226 * @param string $color color for the GIS GEOMETRYCOLLECTION object
227 * @param array $scale_data array containing data related to scaling
229 * @return string JavaScript related to a row in the GIS dataset
230 * @access public
232 public function prepareRowAsOl($spatial, $srid, $label, $color, $scale_data)
234 $row = '';
236 // Trim to remove leading 'GEOMETRYCOLLECTION(' and trailing ')'
237 $goem_col = /*overload*/mb_substr(
238 $spatial,
240 /*overload*/mb_strlen($spatial) - 20
242 // Split the geometry collection object to get its constituents.
243 $sub_parts = $this->_explodeGeomCol($goem_col);
245 foreach ($sub_parts as $sub_part) {
246 $type_pos = /*overload*/mb_stripos($sub_part, '(');
247 $type = /*overload*/mb_substr($sub_part, 0, $type_pos);
249 $gis_obj = PMA_GIS_Factory::factory($type);
250 if (! $gis_obj) {
251 continue;
253 $row .= $gis_obj->prepareRowAsOl(
254 $sub_part, $srid, $label, $color, $scale_data
257 return $row;
261 * Splits the GEOMETRYCOLLECTION object and get its constituents.
263 * @param string $geom_col geometry collection string
265 * @return array the constituents of the geometry collection object
266 * @access private
268 private function _explodeGeomCol($geom_col)
270 $sub_parts = array();
271 $br_count = 0;
272 $start = 0;
273 $count = 0;
274 foreach (str_split($geom_col) as $char) {
275 if ($char == '(') {
276 $br_count++;
277 } elseif ($char == ')') {
278 $br_count--;
279 if ($br_count == 0) {
280 $sub_parts[] = /*overload*/mb_substr(
281 $geom_col,
282 $start,
283 ($count + 1 - $start)
285 $start = $count + 2;
288 $count++;
290 return $sub_parts;
294 * Generates the WKT with the set of parameters passed by the GIS editor.
296 * @param array $gis_data GIS data
297 * @param int $index index into the parameter object
298 * @param string $empty value for empty points
300 * @return string WKT with the set of parameters passed by the GIS editor
301 * @access public
303 public function generateWkt($gis_data, $index, $empty = '')
305 $geom_count = (isset($gis_data['GEOMETRYCOLLECTION']['geom_count']))
306 ? $gis_data['GEOMETRYCOLLECTION']['geom_count'] : 1;
307 $wkt = 'GEOMETRYCOLLECTION(';
308 for ($i = 0; $i < $geom_count; $i++) {
309 if (isset($gis_data[$i]['gis_type'])) {
310 $type = $gis_data[$i]['gis_type'];
311 $gis_obj = PMA_GIS_Factory::factory($type);
312 if (! $gis_obj) {
313 continue;
315 $wkt .= $gis_obj->generateWkt($gis_data, $i, $empty) . ',';
318 if (isset($gis_data[0]['gis_type'])) {
319 $wkt = /*overload*/mb_substr($wkt, 0, /*overload*/mb_strlen($wkt) - 1);
321 $wkt .= ')';
322 return $wkt;
326 * Generates parameters for the GIS data editor from the value of the GIS column.
328 * @param string $value of the GIS column
330 * @return array parameters for the GIS editor from the value of the GIS column
331 * @access public
333 public function generateParams($value)
335 $params = array();
336 $data = PMA_GIS_Geometry::generateParams($value);
337 $params['srid'] = $data['srid'];
338 $wkt = $data['wkt'];
340 // Trim to remove leading 'GEOMETRYCOLLECTION(' and trailing ')'
341 $goem_col = /*overload*/mb_substr(
342 $wkt,
344 /*overload*/mb_strlen($wkt) - 20
346 // Split the geometry collection object to get its constituents.
347 $sub_parts = $this->_explodeGeomCol($goem_col);
348 $params['GEOMETRYCOLLECTION']['geom_count'] = count($sub_parts);
350 $i = 0;
351 foreach ($sub_parts as $sub_part) {
352 $type_pos = /*overload*/mb_stripos($sub_part, '(');
353 $type = /*overload*/mb_substr($sub_part, 0, $type_pos);
354 $gis_obj = PMA_GIS_Factory::factory($type);
355 if (! $gis_obj) {
356 continue;
358 $params = array_merge($params, $gis_obj->generateParams($sub_part, $i));
359 $i++;
361 return $params;