3 * Handles the visualization of GIS POLYGON objects.
5 * @package phpMyAdmin-GIS
7 class PMA_GIS_Polygon
extends PMA_GIS_Geometry
9 // Hold the singleton instance of the class
10 private static $_instance;
13 * A private constructor; prevents direct creation of object.
15 private function __construct()
20 * Returns the singleton.
22 * @return the singleton
24 public static function singleton()
26 if (!isset(self
::$_instance)) {
28 self
::$_instance = new $class;
31 return self
::$_instance;
37 * @param string $spatial spatial data of a row
39 * @return array containing the min, max values for x and y cordinates
41 public function scaleRow($spatial)
45 // Trim to remove leading 'POLYGON((' and trailing '))'
46 $polygon = substr($spatial, 9, (strlen($spatial) - 11));
48 // If the polygon doesnt have an inner polygon
49 if (strpos($polygon, "),(") === false) {
50 $min_max = $this->setMinMax($polygon, $min_max);
52 // Seperate outer and inner polygons
53 $parts = explode("),(", $polygon);
55 $inner = array_slice($parts, 1);
57 $min_max = $this->setMinMax($outer, $min_max);
59 foreach ($inner as $inner_poly) {
60 $min_max = $this->setMinMax($inner_poly, $min_max);
67 * Adds to the PNG image object, the data related to a row in the GIS dataset.
69 * @param string $spatial GIS POLYGON object
70 * @param string $label Label for the GIS POLYGON object
71 * @param string $fill_color Color for the GIS POLYGON object
72 * @param array $scale_data Array containing data related to scaling
73 * @param image $image Image object
75 * @return the modified image object
77 public function prepareRowAsPng($spatial, $label, $fill_color, $scale_data, $image)
80 $black = imagecolorallocate($image, 0, 0, 0);
81 $red = hexdec(substr($fill_color, 1, 2));
82 $green = hexdec(substr($fill_color, 3, 2));
83 $blue = hexdec(substr($fill_color, 4, 2));
84 $color = imagecolorallocate($image, $red, $green, $blue);
86 // Trim to remove leading 'POLYGON((' and trailing '))'
87 $polygon = substr($spatial, 9, (strlen($spatial) - 11));
89 // If the polygon doesnt have an inner polygon
90 if (strpos($polygon, "),(") === false) {
91 $points_arr = $this->extractPoints($polygon, $scale_data, true);
93 // Seperate outer and inner polygons
94 $parts = explode("),(", $polygon);
96 $inner = array_slice($parts, 1);
98 $points_arr = $this->extractPoints($outer, $scale_data, true);
100 foreach ($inner as $inner_poly) {
101 $points_arr = array_merge(
102 $points_arr, $this->extractPoints($inner_poly, $scale_data, true)
108 imagefilledpolygon($image, $points_arr, sizeof($points_arr) / 2, $color);
109 // print label if applicable
110 if (isset($label) && trim($label) != '') {
111 imagestring($image, 1, $points_arr[2], $points_arr[3], trim($label), $black);
117 * Adds to the TCPDF instance, the data related to a row in the GIS dataset.
119 * @param string $spatial GIS POLYGON object
120 * @param string $label Label for the GIS POLYGON object
121 * @param string $fill_color Color for the GIS POLYGON object
122 * @param array $scale_data Array containing data related to scaling
123 * @param image $pdf TCPDF instance
125 * @return the modified TCPDF instance
127 public function prepareRowAsPdf($spatial, $label, $fill_color, $scale_data, $pdf)
130 $red = hexdec(substr($fill_color, 1, 2));
131 $green = hexdec(substr($fill_color, 3, 2));
132 $blue = hexdec(substr($fill_color, 4, 2));
133 $color = array($red, $green, $blue);
135 // Trim to remove leading 'POLYGON((' and trailing '))'
136 $polygon = substr($spatial, 9, (strlen($spatial) - 11));
138 // If the polygon doesnt have an inner polygon
139 if (strpos($polygon, "),(") === false) {
140 $points_arr = $this->extractPoints($polygon, $scale_data, true);
142 // Seperate outer and inner polygons
143 $parts = explode("),(", $polygon);
145 $inner = array_slice($parts, 1);
147 $points_arr = $this->extractPoints($outer, $scale_data, true);
149 foreach ($inner as $inner_poly) {
150 $points_arr = array_merge(
151 $points_arr, $this->extractPoints($inner_poly, $scale_data, true)
157 $pdf->Polygon($points_arr, 'F*', array(), $color, true);
158 // print label if applicable
159 if (isset($label) && trim($label) != '') {
160 $pdf->SetXY($points_arr[2], $points_arr[3]);
161 $pdf->SetFontSize(5);
162 $pdf->Cell(0, 0, trim($label));
168 * Prepares and returns the code related to a row in the GIS dataset as SVG.
170 * @param string $spatial GIS POLYGON object
171 * @param string $label Label for the GIS POLYGON object
172 * @param string $fill_color Color for the GIS POLYGON object
173 * @param array $scale_data Array containing data related to scaling
175 * @return the code related to a row in the GIS dataset
177 public function prepareRowAsSvg($spatial, $label, $fill_color, $scale_data)
179 $polygon_options = array(
181 'id' => $label . rand(),
182 'class' => 'polygon vector',
184 'stroke-width'=> 0.5,
185 'fill' => $fill_color,
186 'fill-rule' => 'evenodd',
187 'fill-opacity'=> 0.8,
190 // Trim to remove leading 'POLYGON((' and trailing '))'
191 $polygon = substr($spatial, 9, (strlen($spatial) - 11));
195 // If the polygon doesnt have an inner polygon
196 if (strpos($polygon, "),(") === false) {
197 $row .= $this->_drawPath($polygon, $scale_data);
199 // Seperate outer and inner polygons
200 $parts = explode("),(", $polygon);
202 $inner = array_slice($parts, 1);
204 $row .= $this->_drawPath($outer, $scale_data);
206 foreach ($inner as $inner_poly) {
207 $row .= $this->_drawPath($inner_poly, $scale_data);
212 foreach ($polygon_options as $option => $val) {
213 $row .= ' ' . $option . '="' . trim($val) . '"';
220 * Prepares JavaScript related to a row in the GIS dataset
221 * to visualize it with OpenLayers.
223 * @param string $spatial GIS POLYGON object
224 * @param int $srid Spatial reference ID
225 * @param string $label Label for the GIS POLYGON object
226 * @param string $fill_color Color for the GIS POLYGON object
227 * @param array $scale_data Array containing data related to scaling
229 * @return JavaScript related to a row in the GIS dataset
231 public function prepareRowAsOl($spatial, $srid, $label, $fill_color, $scale_data)
233 $style_options = array(
234 'strokeColor' => '#000000',
235 'strokeWidth' => 0.5,
236 'fillColor' => $fill_color,
237 'fillOpacity' => 0.8,
244 $row = $this->getBoundsForOl($srid, $scale_data);
246 // Trim to remove leading 'POLYGON((' and trailing '))'
247 $polygon = substr($spatial, 9, (strlen($spatial) - 11));
249 $row .= 'vectorLayer.addFeatures(new OpenLayers.Feature.Vector(';
250 $row .= $this->addPointsForOpenLayersPolygon($polygon, $srid);
251 $row .= 'null, ' . json_encode($style_options) . '));';
256 * Draws a ring of the polygon using SVG path element.
258 * @param string $polygon The ring
259 * @param array $scale_data Array containing data related to scaling
261 * @return the code to draw the ring
263 private function _drawPath($polygon, $scale_data)
265 $points_arr = $this->extractPoints($polygon, $scale_data);
267 $row = ' M ' . $points_arr[0][0] . ', ' . $points_arr[0][1];
268 $other_points = array_slice($points_arr, 1, count($points_arr) - 2);
269 foreach ($other_points as $point) {
270 $row .= ' L ' . $point[0] . ', ' . $point[1];
278 * Generate the WKT with the set of parameters passed by the GIS editor.
280 * @param array $gis_data GIS data
281 * @param int $index Index into the parameter object
282 * @param string $empty Value for empty points
284 * @return WKT with the set of parameters passed by the GIS editor
286 public function generateWkt($gis_data, $index, $empty = '')
288 $no_of_lines = isset($gis_data[$index]['POLYGON']['no_of_lines'])
289 ?
$gis_data[$index]['POLYGON']['no_of_lines'] : 1;
290 if ($no_of_lines < 1) {
294 for ($i = 0; $i < $no_of_lines; $i++
) {
295 $no_of_points = isset($gis_data[$index]['POLYGON'][$i]['no_of_points'])
296 ?
$gis_data[$index]['POLYGON'][$i]['no_of_points'] : 4;
297 if ($no_of_points < 4) {
301 for ($j = 0; $j < $no_of_points; $j++
) {
302 $wkt .= ((isset($gis_data[$index]['POLYGON'][$i][$j]['x'])
303 && trim($gis_data[$index]['POLYGON'][$i][$j]['x']) != '')
304 ?
$gis_data[$index]['POLYGON'][$i][$j]['x'] : $empty)
305 . ' ' . ((isset($gis_data[$index]['POLYGON'][$i][$j]['y'])
306 && trim($gis_data[$index]['POLYGON'][$i][$j]['y']) != '')
307 ?
$gis_data[$index]['POLYGON'][$i][$j]['y'] : $empty) .',';
309 $wkt = substr($wkt, 0, strlen($wkt) - 1);
312 $wkt = substr($wkt, 0, strlen($wkt) - 1);
318 * Calculates the area of a closed simple polygon.
320 * @param array $ring array of points forming the ring
322 * @return the area of a closed simple polygon.
324 public static function area($ring)
327 $no_of_points = count($ring);
329 // If the last point is same as the first point ignore it
330 $last = count($ring) - 1;
331 if (($ring[0]['x'] == $ring[$last]['x'])
332 && ($ring[0]['y'] == $ring[$last]['y'])
338 // A = _1_ \ (X(i) * Y(i+1)) - (Y(i) * X(i+1))
342 for ($i = 0; $i < $no_of_points; $i++
) {
343 $j = ($i +
1) %
$no_of_points;
344 $area +
= $ring[$i]['x'] * $ring[$j]['y'];
345 $area -= $ring[$i]['y'] * $ring[$j]['x'];
353 * Determines whether a set of points represents an outer ring.
354 * If points are in clockwise orientation then, they form an outer ring.
356 * @param array $ring array of points forming the ring
358 * @return whether a set of points represents an outer ring.
360 public static function isOuterRing($ring)
362 // If area is negative then it's in clockwise orientation,
363 // i.e. it's an outer ring
364 if (PMA_GIS_Polygon
::area($ring) < 0) {
371 * Determines whether a given point is inside a given polygon.
373 * @param array $point x, y coordinates of the point
374 * @param array $polygon array of points forming the ring
376 * @return whether a given point is inside a given polygon
378 public static function isPointInsidePolygon($point, $polygon)
380 // If first point is repeated at the end remove it
381 $last = count($polygon) - 1;
382 if (($polygon[0]['x'] == $polygon[$last]['x'])
383 && ($polygon[0]['y'] == $polygon[$last]['y'])
385 $polygon = array_slice($polygon, 0, $last);
388 $no_of_points = count($polygon);
391 // Use ray casting algorithm
393 for ($i = 1; $i <= $no_of_points; $i++
) {
394 $p2 = $polygon[$i %
$no_of_points];
395 if ($point['y'] > min(array($p1['y'], $p2['y']))) {
396 if ($point['y'] <= max(array($p1['y'], $p2['y']))) {
397 if ($point['x'] <= max(array($p1['x'], $p2['x']))) {
398 if ($p1['y'] != $p2['y']) {
399 $xinters = ($point['y'] - $p1['y'])
400 * ($p2['x'] - $p1['x'])
401 / ($p2['y'] - $p1['y']) +
$p1['x'];
402 if ($p1['x'] == $p2['x'] ||
$point['x'] <= $xinters) {
412 if ($counter %
2 == 0) {
420 * Returns a point that is guaranteed to be on the surface of the ring.
421 * (for simple closed rings)
423 * @param array $ring array of points forming the ring
425 * @return a point on the surface of the ring
427 public static function getPointOnSurface($ring)
429 // Find two consecutive distinct points.
430 for ($i = 0; $i < count($ring) - 1; $i++
) {
431 if ($ring[$i]['y'] != $ring[$i +
1]['y']) {
432 $x0 = $ring[$i]['x'];
433 $x1 = $ring[$i +
1]['x'];
434 $y0 = $ring[$i]['y'];
435 $y1 = $ring[$i +
1]['y'];
444 // Find the mid point
445 $x2 = ($x0 +
$x1) / 2;
446 $y2 = ($y0 +
$y1) / 2;
448 // Always keep $epsilon < 1 to go with the reduction logic down here
450 $denominator = sqrt(pow(($y1 - $y0), 2) +
pow(($x0 - $x1), 2));
451 $pointA = array(); $pointB = array();
454 // Get the points on either sides of the line
455 // with a distance of epsilon to the mid point
456 $pointA['x'] = $x2 +
($epsilon * ($y1 - $y0)) / $denominator;
457 $pointA['y'] = $y2 +
($pointA['x'] - $x2) * ($x0 - $x1) / ($y1 - $y0);
459 $pointB['x'] = $x2 +
($epsilon * ($y1 - $y0)) / (0 - $denominator);
460 $pointB['y'] = $y2 +
($pointB['x'] - $x2) * ($x0 - $x1) / ($y1 - $y0);
462 // One of the points should be inside the polygon,
463 // unless epcilon chosen is too large
464 if (PMA_GIS_Polygon
::isPointInsidePolygon($pointA, $ring)) {
466 } elseif (PMA_GIS_Polygon
::isPointInsidePolygon($pointB, $ring)) {
469 //If both are outside the polygon reduce the epsilon and
470 //recalculate the points(reduce exponentially for faster convergance)
471 $epsilon = pow($epsilon, 2);
480 /** Generate parameters for the GIS data editor from the value of the GIS column.
482 * @param string $value of the GIS column
483 * @param index $index of the geometry
485 * @return parameters for the GIS data editor from the value of the GIS column
487 public function generateParams($value, $index = -1)
492 $data = PMA_GIS_Geometry
::generateParams($value);
493 $params['srid'] = $data['srid'];
496 $params[$index]['gis_type'] = 'POLYGON';
500 // Trim to remove leading 'POLYGON((' and trailing '))'
501 $polygon = substr($wkt, 9, (strlen($wkt) - 11));
502 // Seperate each linestring
503 $linerings = explode("),(", $polygon);
504 $params[$index]['POLYGON']['no_of_lines'] = count($linerings);
507 foreach ($linerings as $linering) {
508 $points_arr = $this->extractPoints($linering, null);
509 $no_of_points = count($points_arr);
510 $params[$index]['POLYGON'][$j]['no_of_points'] = $no_of_points;
511 for ($i = 0; $i < $no_of_points; $i++
) {
512 $params[$index]['POLYGON'][$j][$i]['x'] = $points_arr[$i][0];
513 $params[$index]['POLYGON'][$j][$i]['y'] = $points_arr[$i][1];