updated adodb package to work with php 7.1
[openemr.git] / phpmyadmin / libraries / gis / GIS_Polygon.class.php
blob843a8b530f58088ce309782debd605ecfbe28634
1 <?php
2 /* vim: set expandtab sw=4 ts=4 sts=4: */
3 /**
4 * Handles actions related to GIS POLYGON objects
6 * @package PhpMyAdmin-GIS
7 */
9 if (! defined('PHPMYADMIN')) {
10 exit;
13 /**
14 * Handles actions related to GIS POLYGON objects
16 * @package PhpMyAdmin-GIS
18 class PMA_GIS_Polygon 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_Polygon 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 // Trim to remove leading 'POLYGON((' and trailing '))'
59 $polygon = /*overload*/mb_substr(
60 $spatial,
62 /*overload*/mb_strlen($spatial) - 11
65 // If the polygon doesn't have an inner ring, use polygon itself
66 if (/*overload*/mb_strpos($polygon, "),(") === false) {
67 $ring = $polygon;
68 } else {
69 // Separate outer ring and use it to determine min-max
70 $parts = explode("),(", $polygon);
71 $ring = $parts[0];
73 return $this->setMinMax($ring, array());
76 /**
77 * Adds to the PNG image object, the data related to a row in the GIS dataset.
79 * @param string $spatial GIS POLYGON object
80 * @param string $label Label for the GIS POLYGON object
81 * @param string $fill_color Color for the GIS POLYGON object
82 * @param array $scale_data Array containing data related to scaling
83 * @param object $image Image object
85 * @return object the modified image object
86 * @access public
88 public function prepareRowAsPng($spatial, $label, $fill_color,
89 $scale_data, $image
90 ) {
91 // allocate colors
92 $black = imagecolorallocate($image, 0, 0, 0);
93 $red = hexdec(/*overload*/mb_substr($fill_color, 1, 2));
94 $green = hexdec(/*overload*/mb_substr($fill_color, 3, 2));
95 $blue = hexdec(/*overload*/mb_substr($fill_color, 4, 2));
96 $color = imagecolorallocate($image, $red, $green, $blue);
98 // Trim to remove leading 'POLYGON((' and trailing '))'
99 $polygon = /*overload*/mb_substr(
100 $spatial,
102 /*overload*/mb_strlen($spatial) - 11
105 // If the polygon doesn't have an inner polygon
106 if (/*overload*/mb_strpos($polygon, "),(") === false) {
107 $points_arr = $this->extractPoints($polygon, $scale_data, true);
108 } else {
109 // Separate outer and inner polygons
110 $parts = explode("),(", $polygon);
111 $outer = $parts[0];
112 $inner = array_slice($parts, 1);
114 $points_arr = $this->extractPoints($outer, $scale_data, true);
116 foreach ($inner as $inner_poly) {
117 $points_arr = array_merge(
118 $points_arr, $this->extractPoints($inner_poly, $scale_data, true)
123 // draw polygon
124 imagefilledpolygon($image, $points_arr, sizeof($points_arr) / 2, $color);
125 // print label if applicable
126 if (isset($label) && trim($label) != '') {
127 imagestring(
128 $image, 1, $points_arr[2], $points_arr[3], trim($label), $black
131 return $image;
135 * Adds to the TCPDF instance, the data related to a row in the GIS dataset.
137 * @param string $spatial GIS POLYGON object
138 * @param string $label Label for the GIS POLYGON object
139 * @param string $fill_color Color for the GIS POLYGON object
140 * @param array $scale_data Array containing data related to scaling
141 * @param TCPDF $pdf TCPDF instance
143 * @return TCPDF the modified TCPDF instance
144 * @access public
146 public function prepareRowAsPdf($spatial, $label, $fill_color, $scale_data, $pdf)
148 // allocate colors
149 $red = hexdec(/*overload*/mb_substr($fill_color, 1, 2));
150 $green = hexdec(/*overload*/mb_substr($fill_color, 3, 2));
151 $blue = hexdec(/*overload*/mb_substr($fill_color, 4, 2));
152 $color = array($red, $green, $blue);
154 // Trim to remove leading 'POLYGON((' and trailing '))'
155 $polygon = /*overload*/mb_substr(
156 $spatial,
158 /*overload*/mb_strlen($spatial) - 11
161 // If the polygon doesn't have an inner polygon
162 if (/*overload*/mb_strpos($polygon, "),(") === false) {
163 $points_arr = $this->extractPoints($polygon, $scale_data, true);
164 } else {
165 // Separate outer and inner polygons
166 $parts = explode("),(", $polygon);
167 $outer = $parts[0];
168 $inner = array_slice($parts, 1);
170 $points_arr = $this->extractPoints($outer, $scale_data, true);
172 foreach ($inner as $inner_poly) {
173 $points_arr = array_merge(
174 $points_arr, $this->extractPoints($inner_poly, $scale_data, true)
179 // draw polygon
180 $pdf->Polygon($points_arr, 'F*', array(), $color, true);
181 // print label if applicable
182 if (isset($label) && trim($label) != '') {
183 $pdf->SetXY($points_arr[2], $points_arr[3]);
184 $pdf->SetFontSize(5);
185 $pdf->Cell(0, 0, trim($label));
187 return $pdf;
191 * Prepares and returns the code related to a row in the GIS dataset as SVG.
193 * @param string $spatial GIS POLYGON object
194 * @param string $label Label for the GIS POLYGON object
195 * @param string $fill_color Color for the GIS POLYGON object
196 * @param array $scale_data Array containing data related to scaling
198 * @return string the code related to a row in the GIS dataset
199 * @access public
201 public function prepareRowAsSvg($spatial, $label, $fill_color, $scale_data)
203 $polygon_options = array(
204 'name' => $label,
205 'id' => $label . rand(),
206 'class' => 'polygon vector',
207 'stroke' => 'black',
208 'stroke-width'=> 0.5,
209 'fill' => $fill_color,
210 'fill-rule' => 'evenodd',
211 'fill-opacity'=> 0.8,
214 // Trim to remove leading 'POLYGON((' and trailing '))'
215 $polygon = /*overload*/mb_substr(
216 $spatial,
218 /*overload*/mb_strlen($spatial) - 11
221 $row = '<path d="';
223 // If the polygon doesn't have an inner polygon
224 if (/*overload*/mb_strpos($polygon, "),(") === false) {
225 $row .= $this->_drawPath($polygon, $scale_data);
226 } else {
227 // Separate outer and inner polygons
228 $parts = explode("),(", $polygon);
229 $outer = $parts[0];
230 $inner = array_slice($parts, 1);
232 $row .= $this->_drawPath($outer, $scale_data);
234 foreach ($inner as $inner_poly) {
235 $row .= $this->_drawPath($inner_poly, $scale_data);
239 $row .= '"';
240 foreach ($polygon_options as $option => $val) {
241 $row .= ' ' . $option . '="' . trim($val) . '"';
243 $row .= '/>';
244 return $row;
248 * Prepares JavaScript related to a row in the GIS dataset
249 * to visualize it with OpenLayers.
251 * @param string $spatial GIS POLYGON object
252 * @param int $srid Spatial reference ID
253 * @param string $label Label for the GIS POLYGON object
254 * @param string $fill_color Color for the GIS POLYGON object
255 * @param array $scale_data Array containing data related to scaling
257 * @return string JavaScript related to a row in the GIS dataset
258 * @access public
260 public function prepareRowAsOl($spatial, $srid, $label, $fill_color, $scale_data)
262 $style_options = array(
263 'strokeColor' => '#000000',
264 'strokeWidth' => 0.5,
265 'fillColor' => $fill_color,
266 'fillOpacity' => 0.8,
267 'label' => $label,
268 'fontSize' => 10,
270 if ($srid == 0) {
271 $srid = 4326;
273 $row = $this->getBoundsForOl($srid, $scale_data);
275 // Trim to remove leading 'POLYGON((' and trailing '))'
276 $polygon = /*overload*/mb_substr(
277 $spatial,
279 /*overload*/mb_strlen($spatial) - 11
282 // Separate outer and inner polygons
283 $parts = explode("),(", $polygon);
284 $row .= 'vectorLayer.addFeatures(new OpenLayers.Feature.Vector('
285 . $this->getPolygonForOpenLayers($parts, $srid)
286 . ', null, ' . json_encode($style_options) . '));';
287 return $row;
291 * Draws a ring of the polygon using SVG path element.
293 * @param string $polygon The ring
294 * @param array $scale_data Array containing data related to scaling
296 * @return string the code to draw the ring
297 * @access private
299 private function _drawPath($polygon, $scale_data)
301 $points_arr = $this->extractPoints($polygon, $scale_data);
303 $row = ' M ' . $points_arr[0][0] . ', ' . $points_arr[0][1];
304 $other_points = array_slice($points_arr, 1, count($points_arr) - 2);
305 foreach ($other_points as $point) {
306 $row .= ' L ' . $point[0] . ', ' . $point[1];
308 $row .= ' Z ';
310 return $row;
314 * Generate the WKT with the set of parameters passed by the GIS editor.
316 * @param array $gis_data GIS data
317 * @param int $index Index into the parameter object
318 * @param string $empty Value for empty points
320 * @return string WKT with the set of parameters passed by the GIS editor
321 * @access public
323 public function generateWkt($gis_data, $index, $empty = '')
325 $no_of_lines = isset($gis_data[$index]['POLYGON']['no_of_lines'])
326 ? $gis_data[$index]['POLYGON']['no_of_lines'] : 1;
327 if ($no_of_lines < 1) {
328 $no_of_lines = 1;
331 $wkt = 'POLYGON(';
332 for ($i = 0; $i < $no_of_lines; $i++) {
333 $no_of_points = isset($gis_data[$index]['POLYGON'][$i]['no_of_points'])
334 ? $gis_data[$index]['POLYGON'][$i]['no_of_points'] : 4;
335 if ($no_of_points < 4) {
336 $no_of_points = 4;
338 $wkt .= '(';
339 for ($j = 0; $j < $no_of_points; $j++) {
340 $wkt .= ((isset($gis_data[$index]['POLYGON'][$i][$j]['x'])
341 && trim($gis_data[$index]['POLYGON'][$i][$j]['x']) != '')
342 ? $gis_data[$index]['POLYGON'][$i][$j]['x'] : $empty)
343 . ' ' . ((isset($gis_data[$index]['POLYGON'][$i][$j]['y'])
344 && trim($gis_data[$index]['POLYGON'][$i][$j]['y']) != '')
345 ? $gis_data[$index]['POLYGON'][$i][$j]['y'] : $empty) . ',';
347 $wkt = /*overload*/mb_substr($wkt, 0, /*overload*/mb_strlen($wkt) - 1);
348 $wkt .= '),';
350 $wkt = /*overload*/mb_substr($wkt, 0, /*overload*/mb_strlen($wkt) - 1);
351 $wkt .= ')';
352 return $wkt;
356 * Calculates the area of a closed simple polygon.
358 * @param array $ring array of points forming the ring
360 * @return float the area of a closed simple polygon
361 * @access public
362 * @static
364 public static function area($ring)
367 $no_of_points = count($ring);
369 // If the last point is same as the first point ignore it
370 $last = count($ring) - 1;
371 if (($ring[0]['x'] == $ring[$last]['x'])
372 && ($ring[0]['y'] == $ring[$last]['y'])
374 $no_of_points--;
377 // _n-1
378 // A = _1_ \ (X(i) * Y(i+1)) - (Y(i) * X(i+1))
379 // 2 /__
380 // i=0
381 $area = 0;
382 for ($i = 0; $i < $no_of_points; $i++) {
383 $j = ($i + 1) % $no_of_points;
384 $area += $ring[$i]['x'] * $ring[$j]['y'];
385 $area -= $ring[$i]['y'] * $ring[$j]['x'];
387 $area /= 2.0;
389 return $area;
393 * Determines whether a set of points represents an outer ring.
394 * If points are in clockwise orientation then, they form an outer ring.
396 * @param array $ring array of points forming the ring
398 * @return bool whether a set of points represents an outer ring
399 * @access public
400 * @static
402 public static function isOuterRing($ring)
404 // If area is negative then it's in clockwise orientation,
405 // i.e. it's an outer ring
406 if (PMA_GIS_Polygon::area($ring) < 0) {
407 return true;
409 return false;
413 * Determines whether a given point is inside a given polygon.
415 * @param array $point x, y coordinates of the point
416 * @param array $polygon array of points forming the ring
418 * @return bool whether a given point is inside a given polygon
419 * @access public
420 * @static
422 public static function isPointInsidePolygon($point, $polygon)
424 // If first point is repeated at the end remove it
425 $last = count($polygon) - 1;
426 if (($polygon[0]['x'] == $polygon[$last]['x'])
427 && ($polygon[0]['y'] == $polygon[$last]['y'])
429 $polygon = array_slice($polygon, 0, $last);
432 $no_of_points = count($polygon);
433 $counter = 0;
435 // Use ray casting algorithm
436 $p1 = $polygon[0];
437 for ($i = 1; $i <= $no_of_points; $i++) {
438 $p2 = $polygon[$i % $no_of_points];
439 if ($point['y'] <= min(array($p1['y'], $p2['y']))) {
440 $p1 = $p2;
441 continue;
444 if ($point['y'] > max(array($p1['y'], $p2['y']))) {
445 $p1 = $p2;
446 continue;
449 if ($point['x'] > max(array($p1['x'], $p2['x']))) {
450 $p1 = $p2;
451 continue;
454 if ($p1['y'] != $p2['y']) {
455 $xinters = ($point['y'] - $p1['y'])
456 * ($p2['x'] - $p1['x'])
457 / ($p2['y'] - $p1['y']) + $p1['x'];
458 if ($p1['x'] == $p2['x'] || $point['x'] <= $xinters) {
459 $counter++;
463 $p1 = $p2;
466 if ($counter % 2 == 0) {
467 return false;
468 } else {
469 return true;
474 * Returns a point that is guaranteed to be on the surface of the ring.
475 * (for simple closed rings)
477 * @param array $ring array of points forming the ring
479 * @return array|void a point on the surface of the ring
480 * @access public
481 * @static
483 public static function getPointOnSurface($ring)
485 // Find two consecutive distinct points.
486 for ($i = 0, $nb = count($ring) - 1; $i < $nb; $i++) {
487 if ($ring[$i]['y'] != $ring[$i + 1]['y']) {
488 $x0 = $ring[$i]['x'];
489 $x1 = $ring[$i + 1]['x'];
490 $y0 = $ring[$i]['y'];
491 $y1 = $ring[$i + 1]['y'];
492 break;
496 if (! isset($x0)) {
497 return false;
500 // Find the mid point
501 $x2 = ($x0 + $x1) / 2;
502 $y2 = ($y0 + $y1) / 2;
504 // Always keep $epsilon < 1 to go with the reduction logic down here
505 $epsilon = 0.1;
506 $denominator = sqrt(
507 PMA_Util::pow(($y1 - $y0), 2)
508 + PMA_Util::pow(($x0 - $x1), 2)
510 $pointA = array(); $pointB = array();
512 while (true) {
513 // Get the points on either sides of the line
514 // with a distance of epsilon to the mid point
515 $pointA['x'] = $x2 + ($epsilon * ($y1 - $y0)) / $denominator;
516 $pointA['y'] = $y2 + ($pointA['x'] - $x2) * ($x0 - $x1) / ($y1 - $y0);
518 $pointB['x'] = $x2 + ($epsilon * ($y1 - $y0)) / (0 - $denominator);
519 $pointB['y'] = $y2 + ($pointB['x'] - $x2) * ($x0 - $x1) / ($y1 - $y0);
521 // One of the points should be inside the polygon,
522 // unless epsilon chosen is too large
523 if (PMA_GIS_Polygon::isPointInsidePolygon($pointA, $ring)) {
524 return $pointA;
527 if (PMA_GIS_Polygon::isPointInsidePolygon($pointB, $ring)) {
528 return $pointB;
531 //If both are outside the polygon reduce the epsilon and
532 //recalculate the points(reduce exponentially for faster convergence)
533 $epsilon = PMA_Util::pow($epsilon, 2);
534 if ($epsilon == 0) {
535 return false;
540 /** Generate parameters for the GIS data editor from the value of the GIS column.
542 * @param string $value Value of the GIS column
543 * @param int $index Index of the geometry
545 * @return array params for the GIS data editor from the value of the GIS column
546 * @access public
548 public function generateParams($value, $index = -1)
550 $params = array();
551 if ($index == -1) {
552 $index = 0;
553 $data = PMA_GIS_Geometry::generateParams($value);
554 $params['srid'] = $data['srid'];
555 $wkt = $data['wkt'];
556 } else {
557 $params[$index]['gis_type'] = 'POLYGON';
558 $wkt = $value;
561 // Trim to remove leading 'POLYGON((' and trailing '))'
562 $polygon = /*overload*/mb_substr($wkt, 9, /*overload*/mb_strlen($wkt) - 11);
563 // Separate each linestring
564 $linerings = explode("),(", $polygon);
565 $params[$index]['POLYGON']['no_of_lines'] = count($linerings);
567 $j = 0;
568 foreach ($linerings as $linering) {
569 $points_arr = $this->extractPoints($linering, null);
570 $no_of_points = count($points_arr);
571 $params[$index]['POLYGON'][$j]['no_of_points'] = $no_of_points;
572 for ($i = 0; $i < $no_of_points; $i++) {
573 $params[$index]['POLYGON'][$j][$i]['x'] = $points_arr[$i][0];
574 $params[$index]['POLYGON'][$j][$i]['y'] = $points_arr[$i][1];
576 $j++;
578 return $params;