1. Check existence of mb_string, mysql and xml extensions before installation.
[openemr.git] / phpmyadmin / libraries / gis / GIS_Multipolygon.class.php
blob4247b1c55107392899167490cf7581731cdbfd68
1 <?php
2 /* vim: set expandtab sw=4 ts=4 sts=4: */
3 /**
4 * Handles actions related to GIS MULTIPOLYGON objects
6 * @package PhpMyAdmin-GIS
7 */
9 if (! defined('PHPMYADMIN')) {
10 exit;
13 /**
14 * Handles actions related to GIS MULTIPOLYGON objects
16 * @package PhpMyAdmin-GIS
18 class PMA_GIS_Multipolygon 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_Multipolygon 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 an 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 'MULTIPOLYGON(((' and trailing ')))'
61 $multipolygon = /*overload*/mb_substr(
62 $spatial,
63 15,
64 /*overload*/mb_strlen($spatial) - 18
66 // Separate each polygon
67 $polygons = explode(")),((", $multipolygon);
69 foreach ($polygons as $polygon) {
70 // If the polygon doesn't have an inner ring, use polygon itself
71 if (/*overload*/mb_strpos($polygon, "),(") === false) {
72 $ring = $polygon;
73 } else {
74 // Separate outer ring and use it to determine min-max
75 $parts = explode("),(", $polygon);
76 $ring = $parts[0];
78 $min_max = $this->setMinMax($ring, $min_max);
81 return $min_max;
84 /**
85 * Adds to the PNG image object, the data related to a row in the GIS dataset.
87 * @param string $spatial GIS MULTIPOLYGON object
88 * @param string $label Label for the GIS MULTIPOLYGON object
89 * @param string $fill_color Color for the GIS MULTIPOLYGON object
90 * @param array $scale_data Array containing data related to scaling
91 * @param object $image Image object
93 * @return object the modified image object
94 * @access public
96 public function prepareRowAsPng($spatial, $label, $fill_color,
97 $scale_data, $image
98 ) {
99 // allocate colors
100 $black = imagecolorallocate($image, 0, 0, 0);
101 $red = hexdec(/*overload*/mb_substr($fill_color, 1, 2));
102 $green = hexdec(/*overload*/mb_substr($fill_color, 3, 2));
103 $blue = hexdec(/*overload*/mb_substr($fill_color, 4, 2));
104 $color = imagecolorallocate($image, $red, $green, $blue);
106 // Trim to remove leading 'MULTIPOLYGON(((' and trailing ')))'
107 $multipolygon = /*overload*/mb_substr(
108 $spatial,
110 /*overload*/mb_strlen($spatial) - 18
112 // Separate each polygon
113 $polygons = explode(")),((", $multipolygon);
115 $first_poly = true;
116 foreach ($polygons as $polygon) {
117 // If the polygon doesn't have an inner polygon
118 if (/*overload*/mb_strpos($polygon, "),(") === false) {
119 $points_arr = $this->extractPoints($polygon, $scale_data, true);
120 } else {
121 // Separate outer and inner polygons
122 $parts = explode("),(", $polygon);
123 $outer = $parts[0];
124 $inner = array_slice($parts, 1);
126 $points_arr = $this->extractPoints($outer, $scale_data, true);
128 foreach ($inner as $inner_poly) {
129 $points_arr = array_merge(
130 $points_arr,
131 $this->extractPoints($inner_poly, $scale_data, true)
135 // draw polygon
136 imagefilledpolygon($image, $points_arr, sizeof($points_arr) / 2, $color);
137 // mark label point if applicable
138 if (isset($label) && trim($label) != '' && $first_poly) {
139 $label_point = array($points_arr[2], $points_arr[3]);
141 $first_poly = false;
143 // print label if applicable
144 if (isset($label_point)) {
145 imagestring(
146 $image, 1, $points_arr[2], $points_arr[3], trim($label), $black
149 return $image;
153 * Adds to the TCPDF instance, the data related to a row in the GIS dataset.
155 * @param string $spatial GIS MULTIPOLYGON object
156 * @param string $label Label for the GIS MULTIPOLYGON object
157 * @param string $fill_color Color for the GIS MULTIPOLYGON object
158 * @param array $scale_data Array containing data related to scaling
159 * @param TCPDF $pdf TCPDF instance
161 * @return TCPDF the modified TCPDF instance
162 * @access public
164 public function prepareRowAsPdf($spatial, $label, $fill_color, $scale_data, $pdf)
166 // allocate colors
167 $red = hexdec(/*overload*/mb_substr($fill_color, 1, 2));
168 $green = hexdec(/*overload*/mb_substr($fill_color, 3, 2));
169 $blue = hexdec(/*overload*/mb_substr($fill_color, 4, 2));
170 $color = array($red, $green, $blue);
172 // Trim to remove leading 'MULTIPOLYGON(((' and trailing ')))'
173 $multipolygon = /*overload*/mb_substr(
174 $spatial,
176 /*overload*/mb_strlen($spatial) - 18
178 // Separate each polygon
179 $polygons = explode(")),((", $multipolygon);
181 $first_poly = true;
182 foreach ($polygons as $polygon) {
183 // If the polygon doesn't have an inner polygon
184 if (/*overload*/mb_strpos($polygon, "),(") === false) {
185 $points_arr = $this->extractPoints($polygon, $scale_data, true);
186 } else {
187 // Separate outer and inner polygons
188 $parts = explode("),(", $polygon);
189 $outer = $parts[0];
190 $inner = array_slice($parts, 1);
192 $points_arr = $this->extractPoints($outer, $scale_data, true);
194 foreach ($inner as $inner_poly) {
195 $points_arr = array_merge(
196 $points_arr,
197 $this->extractPoints($inner_poly, $scale_data, true)
201 // draw polygon
202 $pdf->Polygon($points_arr, 'F*', array(), $color, true);
203 // mark label point if applicable
204 if (isset($label) && trim($label) != '' && $first_poly) {
205 $label_point = array($points_arr[2], $points_arr[3]);
207 $first_poly = false;
210 // print label if applicable
211 if (isset($label_point)) {
212 $pdf->SetXY($label_point[0], $label_point[1]);
213 $pdf->SetFontSize(5);
214 $pdf->Cell(0, 0, trim($label));
216 return $pdf;
220 * Prepares and returns the code related to a row in the GIS dataset as SVG.
222 * @param string $spatial GIS MULTIPOLYGON object
223 * @param string $label Label for the GIS MULTIPOLYGON object
224 * @param string $fill_color Color for the GIS MULTIPOLYGON object
225 * @param array $scale_data Array containing data related to scaling
227 * @return string the code related to a row in the GIS dataset
228 * @access public
230 public function prepareRowAsSvg($spatial, $label, $fill_color, $scale_data)
232 $polygon_options = array(
233 'name' => $label,
234 'class' => 'multipolygon vector',
235 'stroke' => 'black',
236 'stroke-width'=> 0.5,
237 'fill' => $fill_color,
238 'fill-rule' => 'evenodd',
239 'fill-opacity'=> 0.8,
242 $row = '';
244 // Trim to remove leading 'MULTIPOLYGON(((' and trailing ')))'
245 $multipolygon = /*overload*/mb_substr(
246 $spatial,
248 /*overload*/mb_strlen($spatial) - 18
250 // Separate each polygon
251 $polygons = explode(")),((", $multipolygon);
253 foreach ($polygons as $polygon) {
254 $row .= '<path d="';
256 // If the polygon doesn't have an inner polygon
257 if (/*overload*/mb_strpos($polygon, "),(") === false) {
258 $row .= $this->_drawPath($polygon, $scale_data);
259 } else {
260 // Separate outer and inner polygons
261 $parts = explode("),(", $polygon);
262 $outer = $parts[0];
263 $inner = array_slice($parts, 1);
265 $row .= $this->_drawPath($outer, $scale_data);
267 foreach ($inner as $inner_poly) {
268 $row .= $this->_drawPath($inner_poly, $scale_data);
271 $polygon_options['id'] = $label . rand();
272 $row .= '"';
273 foreach ($polygon_options as $option => $val) {
274 $row .= ' ' . $option . '="' . trim($val) . '"';
276 $row .= '/>';
279 return $row;
283 * Prepares JavaScript related to a row in the GIS dataset
284 * to visualize it with OpenLayers.
286 * @param string $spatial GIS MULTIPOLYGON object
287 * @param int $srid Spatial reference ID
288 * @param string $label Label for the GIS MULTIPOLYGON object
289 * @param string $fill_color Color for the GIS MULTIPOLYGON object
290 * @param array $scale_data Array containing data related to scaling
292 * @return string JavaScript related to a row in the GIS dataset
293 * @access public
295 public function prepareRowAsOl($spatial, $srid, $label, $fill_color, $scale_data)
297 $style_options = array(
298 'strokeColor' => '#000000',
299 'strokeWidth' => 0.5,
300 'fillColor' => $fill_color,
301 'fillOpacity' => 0.8,
302 'label' => $label,
303 'fontSize' => 10,
305 if ($srid == 0) {
306 $srid = 4326;
308 $row = $this->getBoundsForOl($srid, $scale_data);
310 // Trim to remove leading 'MULTIPOLYGON(((' and trailing ')))'
311 $multipolygon = /*overload*/mb_substr(
312 $spatial,
314 /*overload*/mb_strlen($spatial) - 18
316 // Separate each polygon
317 $polygons = explode(")),((", $multipolygon);
319 $row .= 'vectorLayer.addFeatures(new OpenLayers.Feature.Vector('
320 . 'new OpenLayers.Geometry.MultiPolygon('
321 . $this->getPolygonArrayForOpenLayers($polygons, $srid)
322 . '), null, ' . json_encode($style_options) . '));';
323 return $row;
327 * Draws a ring of the polygon using SVG path element.
329 * @param string $polygon The ring
330 * @param array $scale_data Array containing data related to scaling
332 * @return string the code to draw the ring
333 * @access private
335 private function _drawPath($polygon, $scale_data)
337 $points_arr = $this->extractPoints($polygon, $scale_data);
339 $row = ' M ' . $points_arr[0][0] . ', ' . $points_arr[0][1];
340 $other_points = array_slice($points_arr, 1, count($points_arr) - 2);
341 foreach ($other_points as $point) {
342 $row .= ' L ' . $point[0] . ', ' . $point[1];
344 $row .= ' Z ';
346 return $row;
350 * Generate the WKT with the set of parameters passed by the GIS editor.
352 * @param array $gis_data GIS data
353 * @param int $index Index into the parameter object
354 * @param string $empty Value for empty points
356 * @return string WKT with the set of parameters passed by the GIS editor
357 * @access public
359 public function generateWkt($gis_data, $index, $empty = '')
361 $data_row = $gis_data[$index]['MULTIPOLYGON'];
363 $no_of_polygons = isset($data_row['no_of_polygons'])
364 ? $data_row['no_of_polygons'] : 1;
365 if ($no_of_polygons < 1) {
366 $no_of_polygons = 1;
369 $wkt = 'MULTIPOLYGON(';
370 for ($k = 0; $k < $no_of_polygons; $k++) {
371 $no_of_lines = isset($data_row[$k]['no_of_lines'])
372 ? $data_row[$k]['no_of_lines'] : 1;
373 if ($no_of_lines < 1) {
374 $no_of_lines = 1;
376 $wkt .= '(';
377 for ($i = 0; $i < $no_of_lines; $i++) {
378 $no_of_points = isset($data_row[$k][$i]['no_of_points'])
379 ? $data_row[$k][$i]['no_of_points'] : 4;
380 if ($no_of_points < 4) {
381 $no_of_points = 4;
383 $wkt .= '(';
384 for ($j = 0; $j < $no_of_points; $j++) {
385 $wkt .= ((isset($data_row[$k][$i][$j]['x'])
386 && trim($data_row[$k][$i][$j]['x']) != '')
387 ? $data_row[$k][$i][$j]['x'] : $empty)
388 . ' ' . ((isset($data_row[$k][$i][$j]['y'])
389 && trim($data_row[$k][$i][$j]['y']) != '')
390 ? $data_row[$k][$i][$j]['y'] : $empty) . ',';
392 $wkt = /*overload*/mb_substr(
393 $wkt,
395 /*overload*/mb_strlen($wkt) - 1
397 $wkt .= '),';
399 $wkt = /*overload*/mb_substr($wkt, 0, /*overload*/mb_strlen($wkt) - 1);
400 $wkt .= '),';
402 $wkt = /*overload*/mb_substr($wkt, 0, /*overload*/mb_strlen($wkt) - 1);
403 $wkt .= ')';
404 return $wkt;
408 * Generate the WKT for the data from ESRI shape files.
410 * @param array $row_data GIS data
412 * @return string the WKT for the data from ESRI shape files
413 * @access public
415 public function getShape($row_data)
417 // Determines whether each line ring is an inner ring or an outer ring.
418 // If it's an inner ring get a point on the surface which can be used to
419 // correctly classify inner rings to their respective outer rings.
420 include_once './libraries/gis/GIS_Polygon.class.php';
421 foreach ($row_data['parts'] as $i => $ring) {
422 $row_data['parts'][$i]['isOuter']
423 = PMA_GIS_Polygon::isOuterRing($ring['points']);
426 // Find points on surface for inner rings
427 foreach ($row_data['parts'] as $i => $ring) {
428 if (! $ring['isOuter']) {
429 $row_data['parts'][$i]['pointOnSurface']
430 = PMA_GIS_Polygon::getPointOnSurface($ring['points']);
434 // Classify inner rings to their respective outer rings.
435 foreach ($row_data['parts'] as $j => $ring1) {
436 if ($ring1['isOuter']) {
437 continue;
439 foreach ($row_data['parts'] as $k => $ring2) {
440 if (!$ring2['isOuter']) {
441 continue;
444 // If the pointOnSurface of the inner ring
445 // is also inside the outer ring
446 if (PMA_GIS_Polygon::isPointInsidePolygon(
447 $ring1['pointOnSurface'], $ring2['points']
448 )) {
449 if (! isset($ring2['inner'])) {
450 $row_data['parts'][$k]['inner'] = array();
452 $row_data['parts'][$k]['inner'][] = $j;
457 $wkt = 'MULTIPOLYGON(';
458 // for each polygon
459 foreach ($row_data['parts'] as $ring) {
460 if (!$ring['isOuter']) {
461 continue;
464 $wkt .= '('; // start of polygon
466 $wkt .= '('; // start of outer ring
467 foreach ($ring['points'] as $point) {
468 $wkt .= $point['x'] . ' ' . $point['y'] . ',';
470 $wkt = /*overload*/mb_substr($wkt, 0, /*overload*/mb_strlen($wkt) - 1);
471 $wkt .= ')'; // end of outer ring
473 // inner rings if any
474 if (isset($ring['inner'])) {
475 foreach ($ring['inner'] as $j) {
476 $wkt .= ',('; // start of inner ring
477 foreach ($row_data['parts'][$j]['points'] as $innerPoint) {
478 $wkt .= $innerPoint['x'] . ' ' . $innerPoint['y'] . ',';
480 $wkt = /*overload*/mb_substr(
481 $wkt,
483 /*overload*/mb_strlen($wkt) - 1
485 $wkt .= ')'; // end of inner ring
489 $wkt .= '),'; // end of polygon
491 $wkt = /*overload*/mb_substr($wkt, 0, /*overload*/mb_strlen($wkt) - 1);
493 $wkt .= ')'; // end of multipolygon
494 return $wkt;
498 * Generate parameters for the GIS data editor from the value of the GIS column.
500 * @param string $value Value of the GIS column
501 * @param int $index Index of the geometry
503 * @return array params for the GIS data editor from the value of the GIS column
504 * @access public
506 public function generateParams($value, $index = -1)
508 $params = array();
509 if ($index == -1) {
510 $index = 0;
511 $data = PMA_GIS_Geometry::generateParams($value);
512 $params['srid'] = $data['srid'];
513 $wkt = $data['wkt'];
514 } else {
515 $params[$index]['gis_type'] = 'MULTIPOLYGON';
516 $wkt = $value;
519 // Trim to remove leading 'MULTIPOLYGON(((' and trailing ')))'
520 $multipolygon = /*overload*/mb_substr(
521 $wkt,
523 /*overload*/mb_strlen($wkt) - 18
525 // Separate each polygon
526 $polygons = explode(")),((", $multipolygon);
528 $param_row =& $params[$index]['MULTIPOLYGON'];
529 $param_row['no_of_polygons'] = count($polygons);
531 $k = 0;
532 foreach ($polygons as $polygon) {
533 // If the polygon doesn't have an inner polygon
534 if (/*overload*/mb_strpos($polygon, "),(") === false) {
535 $param_row[$k]['no_of_lines'] = 1;
536 $points_arr = $this->extractPoints($polygon, null);
537 $no_of_points = count($points_arr);
538 $param_row[$k][0]['no_of_points'] = $no_of_points;
539 for ($i = 0; $i < $no_of_points; $i++) {
540 $param_row[$k][0][$i]['x'] = $points_arr[$i][0];
541 $param_row[$k][0][$i]['y'] = $points_arr[$i][1];
543 } else {
544 // Separate outer and inner polygons
545 $parts = explode("),(", $polygon);
546 $param_row[$k]['no_of_lines'] = count($parts);
547 $j = 0;
548 foreach ($parts as $ring) {
549 $points_arr = $this->extractPoints($ring, null);
550 $no_of_points = count($points_arr);
551 $param_row[$k][$j]['no_of_points'] = $no_of_points;
552 for ($i = 0; $i < $no_of_points; $i++) {
553 $param_row[$k][$j][$i]['x'] = $points_arr[$i][0];
554 $param_row[$k][$j][$i]['y'] = $points_arr[$i][1];
556 $j++;
559 $k++;
561 return $params;