2 /* vim: set expandtab sw=4 ts=4 sts=4: */
4 * Handles actions related to GIS POLYGON objects
6 * @package PhpMyAdmin-GIS
9 if (! defined('PHPMYADMIN')) {
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;
24 * A private constructor; prevents direct creation of object.
28 private function __construct()
33 * Returns the singleton.
35 * @return PMA_GIS_Polygon the singleton
38 public static function singleton()
40 if (!isset(self
::$_instance)) {
42 self
::$_instance = new $class;
45 return self
::$_instance;
51 * @param string $spatial spatial data of a row
53 * @return array an array containing the min, max values for x and y coordinates
56 public function scaleRow($spatial)
58 // Trim to remove leading 'POLYGON((' and trailing '))'
59 $polygon = /*overload*/mb_substr(
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) {
69 // Separate outer ring and use it to determine min-max
70 $parts = explode("),(", $polygon);
73 return $this->setMinMax($ring, array());
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
88 public function prepareRowAsPng($spatial, $label, $fill_color,
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(
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);
109 // Separate outer and inner polygons
110 $parts = explode("),(", $polygon);
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)
124 imagefilledpolygon($image, $points_arr, sizeof($points_arr) / 2, $color);
125 // print label if applicable
126 if (isset($label) && trim($label) != '') {
128 $image, 1, $points_arr[2], $points_arr[3], trim($label), $black
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
146 public function prepareRowAsPdf($spatial, $label, $fill_color, $scale_data, $pdf)
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(
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);
165 // Separate outer and inner polygons
166 $parts = explode("),(", $polygon);
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)
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));
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
201 public function prepareRowAsSvg($spatial, $label, $fill_color, $scale_data)
203 $polygon_options = array(
205 'id' => $label . rand(),
206 'class' => 'polygon vector',
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(
218 /*overload*/mb_strlen($spatial) - 11
223 // If the polygon doesn't have an inner polygon
224 if (/*overload*/mb_strpos($polygon, "),(") === false) {
225 $row .= $this->_drawPath($polygon, $scale_data);
227 // Separate outer and inner polygons
228 $parts = explode("),(", $polygon);
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);
240 foreach ($polygon_options as $option => $val) {
241 $row .= ' ' . $option . '="' . trim($val) . '"';
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
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,
273 $row = $this->getBoundsForOl($srid, $scale_data);
275 // Trim to remove leading 'POLYGON((' and trailing '))'
276 $polygon = /*overload*/mb_substr(
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) . '));';
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
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];
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
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) {
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) {
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);
350 $wkt = /*overload*/mb_substr($wkt, 0, /*overload*/mb_strlen($wkt) - 1);
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
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'])
378 // A = _1_ \ (X(i) * Y(i+1)) - (Y(i) * X(i+1))
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'];
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
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) {
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
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);
435 // Use ray casting algorithm
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']))) {
444 if ($point['y'] > max(array($p1['y'], $p2['y']))) {
449 if ($point['x'] > max(array($p1['x'], $p2['x']))) {
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) {
466 if ($counter %
2 == 0) {
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
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'];
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
507 PMA_Util
::pow(($y1 - $y0), 2)
508 + PMA_Util
::pow(($x0 - $x1), 2)
510 $pointA = array(); $pointB = array();
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)) {
527 if (PMA_GIS_Polygon
::isPointInsidePolygon($pointB, $ring)) {
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);
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
548 public function generateParams($value, $index = -1)
553 $data = PMA_GIS_Geometry
::generateParams($value);
554 $params['srid'] = $data['srid'];
557 $params[$index]['gis_type'] = 'POLYGON';
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);
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];