Upgraded phpmyadmin to 4.0.4 (All Languages) - No modifications yet
[openemr.git] / phpmyadmin / libraries / gis / pma_gis_polygon.php
blob47dde132fbccb3e84d349359f9b0b713fc2080f2
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 object 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 cordinates
54 * @access public
56 public function scaleRow($spatial)
58 // Trim to remove leading 'POLYGON((' and trailing '))'
59 $polygon = substr($spatial, 9, (strlen($spatial) - 11));
61 // If the polygon doesn't have an inner ring, use polygon itself
62 if (strpos($polygon, "),(") === false) {
63 $ring = $polygon;
64 } else {
65 // Seperate outer ring and use it to determin min-max
66 $parts = explode("),(", $polygon);
67 $ring = $parts[0];
69 return $this->setMinMax($ring, array());
72 /**
73 * Adds to the PNG image object, the data related to a row in the GIS dataset.
75 * @param string $spatial GIS POLYGON object
76 * @param string $label Label for the GIS POLYGON object
77 * @param string $fill_color Color for the GIS POLYGON object
78 * @param array $scale_data Array containing data related to scaling
79 * @param object $image Image object
81 * @return object the modified image object
82 * @access public
84 public function prepareRowAsPng($spatial, $label, $fill_color,
85 $scale_data, $image
86 ) {
87 // allocate colors
88 $black = imagecolorallocate($image, 0, 0, 0);
89 $red = hexdec(substr($fill_color, 1, 2));
90 $green = hexdec(substr($fill_color, 3, 2));
91 $blue = hexdec(substr($fill_color, 4, 2));
92 $color = imagecolorallocate($image, $red, $green, $blue);
94 // Trim to remove leading 'POLYGON((' and trailing '))'
95 $polygon = substr($spatial, 9, (strlen($spatial) - 11));
97 // If the polygon doesnt have an inner polygon
98 if (strpos($polygon, "),(") === false) {
99 $points_arr = $this->extractPoints($polygon, $scale_data, true);
100 } else {
101 // Seperate outer and inner polygons
102 $parts = explode("),(", $polygon);
103 $outer = $parts[0];
104 $inner = array_slice($parts, 1);
106 $points_arr = $this->extractPoints($outer, $scale_data, true);
108 foreach ($inner as $inner_poly) {
109 $points_arr = array_merge(
110 $points_arr, $this->extractPoints($inner_poly, $scale_data, true)
115 // draw polygon
116 imagefilledpolygon($image, $points_arr, sizeof($points_arr) / 2, $color);
117 // print label if applicable
118 if (isset($label) && trim($label) != '') {
119 imagestring(
120 $image, 1, $points_arr[2], $points_arr[3], trim($label), $black
123 return $image;
127 * Adds to the TCPDF instance, the data related to a row in the GIS dataset.
129 * @param string $spatial GIS POLYGON object
130 * @param string $label Label for the GIS POLYGON object
131 * @param string $fill_color Color for the GIS POLYGON object
132 * @param array $scale_data Array containing data related to scaling
133 * @param object $pdf TCPDF instance
135 * @return object the modified TCPDF instance
136 * @access public
138 public function prepareRowAsPdf($spatial, $label, $fill_color, $scale_data, $pdf)
140 // allocate colors
141 $red = hexdec(substr($fill_color, 1, 2));
142 $green = hexdec(substr($fill_color, 3, 2));
143 $blue = hexdec(substr($fill_color, 4, 2));
144 $color = array($red, $green, $blue);
146 // Trim to remove leading 'POLYGON((' and trailing '))'
147 $polygon = substr($spatial, 9, (strlen($spatial) - 11));
149 // If the polygon doesnt have an inner polygon
150 if (strpos($polygon, "),(") === false) {
151 $points_arr = $this->extractPoints($polygon, $scale_data, true);
152 } else {
153 // Seperate outer and inner polygons
154 $parts = explode("),(", $polygon);
155 $outer = $parts[0];
156 $inner = array_slice($parts, 1);
158 $points_arr = $this->extractPoints($outer, $scale_data, true);
160 foreach ($inner as $inner_poly) {
161 $points_arr = array_merge(
162 $points_arr, $this->extractPoints($inner_poly, $scale_data, true)
167 // draw polygon
168 $pdf->Polygon($points_arr, 'F*', array(), $color, true);
169 // print label if applicable
170 if (isset($label) && trim($label) != '') {
171 $pdf->SetXY($points_arr[2], $points_arr[3]);
172 $pdf->SetFontSize(5);
173 $pdf->Cell(0, 0, trim($label));
175 return $pdf;
179 * Prepares and returns the code related to a row in the GIS dataset as SVG.
181 * @param string $spatial GIS POLYGON object
182 * @param string $label Label for the GIS POLYGON object
183 * @param string $fill_color Color for the GIS POLYGON object
184 * @param array $scale_data Array containing data related to scaling
186 * @return string the code related to a row in the GIS dataset
187 * @access public
189 public function prepareRowAsSvg($spatial, $label, $fill_color, $scale_data)
191 $polygon_options = array(
192 'name' => $label,
193 'id' => $label . rand(),
194 'class' => 'polygon vector',
195 'stroke' => 'black',
196 'stroke-width'=> 0.5,
197 'fill' => $fill_color,
198 'fill-rule' => 'evenodd',
199 'fill-opacity'=> 0.8,
202 // Trim to remove leading 'POLYGON((' and trailing '))'
203 $polygon = substr($spatial, 9, (strlen($spatial) - 11));
205 $row = '<path d="';
207 // If the polygon doesnt have an inner polygon
208 if (strpos($polygon, "),(") === false) {
209 $row .= $this->_drawPath($polygon, $scale_data);
210 } else {
211 // Seperate outer and inner polygons
212 $parts = explode("),(", $polygon);
213 $outer = $parts[0];
214 $inner = array_slice($parts, 1);
216 $row .= $this->_drawPath($outer, $scale_data);
218 foreach ($inner as $inner_poly) {
219 $row .= $this->_drawPath($inner_poly, $scale_data);
223 $row .= '"';
224 foreach ($polygon_options as $option => $val) {
225 $row .= ' ' . $option . '="' . trim($val) . '"';
227 $row .= '/>';
228 return $row;
232 * Prepares JavaScript related to a row in the GIS dataset
233 * to visualize it with OpenLayers.
235 * @param string $spatial GIS POLYGON object
236 * @param int $srid Spatial reference ID
237 * @param string $label Label for the GIS POLYGON object
238 * @param string $fill_color Color for the GIS POLYGON object
239 * @param array $scale_data Array containing data related to scaling
241 * @return string JavaScript related to a row in the GIS dataset
242 * @access public
244 public function prepareRowAsOl($spatial, $srid, $label, $fill_color, $scale_data)
246 $style_options = array(
247 'strokeColor' => '#000000',
248 'strokeWidth' => 0.5,
249 'fillColor' => $fill_color,
250 'fillOpacity' => 0.8,
251 'label' => $label,
252 'fontSize' => 10,
254 if ($srid == 0) {
255 $srid = 4326;
257 $row = $this->getBoundsForOl($srid, $scale_data);
259 // Trim to remove leading 'POLYGON((' and trailing '))'
260 $polygon = substr($spatial, 9, (strlen($spatial) - 11));
262 // Seperate outer and inner polygons
263 $parts = explode("),(", $polygon);
264 $row .= 'vectorLayer.addFeatures(new OpenLayers.Feature.Vector('
265 . $this->getPolygonForOpenLayers($parts, $srid)
266 . ', null, ' . json_encode($style_options) . '));';
267 return $row;
271 * Draws a ring of the polygon using SVG path element.
273 * @param string $polygon The ring
274 * @param array $scale_data Array containing data related to scaling
276 * @return string the code to draw the ring
277 * @access private
279 private function _drawPath($polygon, $scale_data)
281 $points_arr = $this->extractPoints($polygon, $scale_data);
283 $row = ' M ' . $points_arr[0][0] . ', ' . $points_arr[0][1];
284 $other_points = array_slice($points_arr, 1, count($points_arr) - 2);
285 foreach ($other_points as $point) {
286 $row .= ' L ' . $point[0] . ', ' . $point[1];
288 $row .= ' Z ';
290 return $row;
294 * Generate 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 $no_of_lines = isset($gis_data[$index]['POLYGON']['no_of_lines'])
306 ? $gis_data[$index]['POLYGON']['no_of_lines'] : 1;
307 if ($no_of_lines < 1) {
308 $no_of_lines = 1;
310 $wkt = 'POLYGON(';
311 for ($i = 0; $i < $no_of_lines; $i++) {
312 $no_of_points = isset($gis_data[$index]['POLYGON'][$i]['no_of_points'])
313 ? $gis_data[$index]['POLYGON'][$i]['no_of_points'] : 4;
314 if ($no_of_points < 4) {
315 $no_of_points = 4;
317 $wkt .= '(';
318 for ($j = 0; $j < $no_of_points; $j++) {
319 $wkt .= ((isset($gis_data[$index]['POLYGON'][$i][$j]['x'])
320 && trim($gis_data[$index]['POLYGON'][$i][$j]['x']) != '')
321 ? $gis_data[$index]['POLYGON'][$i][$j]['x'] : $empty)
322 . ' ' . ((isset($gis_data[$index]['POLYGON'][$i][$j]['y'])
323 && trim($gis_data[$index]['POLYGON'][$i][$j]['y']) != '')
324 ? $gis_data[$index]['POLYGON'][$i][$j]['y'] : $empty) .',';
326 $wkt = substr($wkt, 0, strlen($wkt) - 1);
327 $wkt .= '),';
329 $wkt = substr($wkt, 0, strlen($wkt) - 1);
330 $wkt .= ')';
331 return $wkt;
335 * Calculates the area of a closed simple polygon.
337 * @param array $ring array of points forming the ring
339 * @return float the area of a closed simple polygon
340 * @access public
341 * @static
343 public static function area($ring)
346 $no_of_points = count($ring);
348 // If the last point is same as the first point ignore it
349 $last = count($ring) - 1;
350 if (($ring[0]['x'] == $ring[$last]['x'])
351 && ($ring[0]['y'] == $ring[$last]['y'])
353 $no_of_points--;
356 // _n-1
357 // A = _1_ \ (X(i) * Y(i+1)) - (Y(i) * X(i+1))
358 // 2 /__
359 // i=0
360 $area = 0;
361 for ($i = 0; $i < $no_of_points; $i++) {
362 $j = ($i + 1) % $no_of_points;
363 $area += $ring[$i]['x'] * $ring[$j]['y'];
364 $area -= $ring[$i]['y'] * $ring[$j]['x'];
366 $area /= 2.0;
368 return $area;
372 * Determines whether a set of points represents an outer ring.
373 * If points are in clockwise orientation then, they form an outer ring.
375 * @param array $ring array of points forming the ring
377 * @return bool whether a set of points represents an outer ring
378 * @access public
379 * @static
381 public static function isOuterRing($ring)
383 // If area is negative then it's in clockwise orientation,
384 // i.e. it's an outer ring
385 if (PMA_GIS_Polygon::area($ring) < 0) {
386 return true;
388 return false;
392 * Determines whether a given point is inside a given polygon.
394 * @param array $point x, y coordinates of the point
395 * @param array $polygon array of points forming the ring
397 * @return bool whether a given point is inside a given polygon
398 * @access public
399 * @static
401 public static function isPointInsidePolygon($point, $polygon)
403 // If first point is repeated at the end remove it
404 $last = count($polygon) - 1;
405 if (($polygon[0]['x'] == $polygon[$last]['x'])
406 && ($polygon[0]['y'] == $polygon[$last]['y'])
408 $polygon = array_slice($polygon, 0, $last);
411 $no_of_points = count($polygon);
412 $counter = 0;
414 // Use ray casting algorithm
415 $p1 = $polygon[0];
416 for ($i = 1; $i <= $no_of_points; $i++) {
417 $p2 = $polygon[$i % $no_of_points];
418 if ($point['y'] > min(array($p1['y'], $p2['y']))) {
419 if ($point['y'] <= max(array($p1['y'], $p2['y']))) {
420 if ($point['x'] <= max(array($p1['x'], $p2['x']))) {
421 if ($p1['y'] != $p2['y']) {
422 $xinters = ($point['y'] - $p1['y'])
423 * ($p2['x'] - $p1['x'])
424 / ($p2['y'] - $p1['y']) + $p1['x'];
425 if ($p1['x'] == $p2['x'] || $point['x'] <= $xinters) {
426 $counter++;
432 $p1 = $p2;
435 if ($counter % 2 == 0) {
436 return false;
437 } else {
438 return true;
443 * Returns a point that is guaranteed to be on the surface of the ring.
444 * (for simple closed rings)
446 * @param array $ring array of points forming the ring
448 * @return array a point on the surface of the ring
449 * @access public
450 * @static
452 public static function getPointOnSurface($ring)
454 // Find two consecutive distinct points.
455 for ($i = 0; $i < count($ring) - 1; $i++) {
456 if ($ring[$i]['y'] != $ring[$i + 1]['y']) {
457 $x0 = $ring[$i]['x'];
458 $x1 = $ring[$i + 1]['x'];
459 $y0 = $ring[$i]['y'];
460 $y1 = $ring[$i + 1]['y'];
461 break;
465 if (! isset($x0)) {
466 return false;
469 // Find the mid point
470 $x2 = ($x0 + $x1) / 2;
471 $y2 = ($y0 + $y1) / 2;
473 // Always keep $epsilon < 1 to go with the reduction logic down here
474 $epsilon = 0.1;
475 $denominator = sqrt(
476 PMA_Util::pow(($y1 - $y0), 2)
477 + PMA_Util::pow(($x0 - $x1), 2)
479 $pointA = array(); $pointB = array();
481 while (true) {
482 // Get the points on either sides of the line
483 // with a distance of epsilon to the mid point
484 $pointA['x'] = $x2 + ($epsilon * ($y1 - $y0)) / $denominator;
485 $pointA['y'] = $y2 + ($pointA['x'] - $x2) * ($x0 - $x1) / ($y1 - $y0);
487 $pointB['x'] = $x2 + ($epsilon * ($y1 - $y0)) / (0 - $denominator);
488 $pointB['y'] = $y2 + ($pointB['x'] - $x2) * ($x0 - $x1) / ($y1 - $y0);
490 // One of the points should be inside the polygon,
491 // unless epcilon chosen is too large
492 if (PMA_GIS_Polygon::isPointInsidePolygon($pointA, $ring)) {
493 return $pointA;
494 } elseif (PMA_GIS_Polygon::isPointInsidePolygon($pointB, $ring)) {
495 return $pointB;
496 } else {
497 //If both are outside the polygon reduce the epsilon and
498 //recalculate the points(reduce exponentially for faster convergance)
499 $epsilon = PMA_Util::pow($epsilon, 2);
500 if ($epsilon == 0) {
501 return false;
508 /** Generate parameters for the GIS data editor from the value of the GIS column.
510 * @param string $value of the GIS column
511 * @param index $index of the geometry
513 * @return array params for the GIS data editor from the value of the GIS column
514 * @access public
516 public function generateParams($value, $index = -1)
518 if ($index == -1) {
519 $index = 0;
520 $params = array();
521 $data = PMA_GIS_Geometry::generateParams($value);
522 $params['srid'] = $data['srid'];
523 $wkt = $data['wkt'];
524 } else {
525 $params[$index]['gis_type'] = 'POLYGON';
526 $wkt = $value;
529 // Trim to remove leading 'POLYGON((' and trailing '))'
530 $polygon = substr($wkt, 9, (strlen($wkt) - 11));
531 // Seperate each linestring
532 $linerings = explode("),(", $polygon);
533 $params[$index]['POLYGON']['no_of_lines'] = count($linerings);
535 $j = 0;
536 foreach ($linerings as $linering) {
537 $points_arr = $this->extractPoints($linering, null);
538 $no_of_points = count($points_arr);
539 $params[$index]['POLYGON'][$j]['no_of_points'] = $no_of_points;
540 for ($i = 0; $i < $no_of_points; $i++) {
541 $params[$index]['POLYGON'][$j][$i]['x'] = $points_arr[$i][0];
542 $params[$index]['POLYGON'][$j][$i]['y'] = $points_arr[$i][1];
544 $j++;
546 return $params;