Labels for polygons and multipolygons
[phpmyadmin/crack.git] / libraries / gis / pma_gis_polygon.php
blob16e2ae0a8c80d0d8d97104bba7645e3a064706a9
1 <?php
2 /**
3 * Handles the visualization of GIS POLYGON objects.
5 * @package phpMyAdmin-GIS
6 */
7 class PMA_GIS_Polygon extends PMA_GIS_Geometry
9 // Hold the singleton instance of the class
10 private static $_instance;
12 /**
13 * A private constructor; prevents direct creation of object.
15 private function __construct()
19 /**
20 * Returns the singleton.
22 * @return the singleton
24 public static function singleton()
26 if (!isset(self::$_instance)) {
27 $class = __CLASS__;
28 self::$_instance = new $class;
31 return self::$_instance;
34 /**
35 * Scales each row.
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)
43 $min_max = array();
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);
51 } else {
52 // Seperate outer and inner polygons
53 $parts = explode("),(", $polygon);
54 $outer = $parts[0];
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);
63 return $min_max;
66 /**
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)
79 // allocate colors
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);
92 } else {
93 // Seperate outer and inner polygons
94 $parts = explode("),(", $polygon);
95 $outer = $parts[0];
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)
107 // draw polygon
108 imagefilledpolygon($image, $points_arr, sizeof($points_arr) / 2, $color);
109 // print label if applicable
110 if (isset($label) && trim($label) != '') {
111 imagestring($image, 2, $points_arr[2], $points_arr[3], trim($label), $black);
113 return $image;
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)
129 // allocate colors
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);
141 } else {
142 // Seperate outer and inner polygons
143 $parts = explode("),(", $polygon);
144 $outer = $parts[0];
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)
156 // draw polygon
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(7);
162 $pdf->Cell(0, 0, trim($label));
164 return $pdf;
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(
180 'name' => $label,
181 'id' => $label . rand(),
182 'class' => 'polygon vector',
183 'stroke' => 'black',
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));
193 $row = '<path d="';
195 // If the polygon doesnt have an inner polygon
196 if (strpos($polygon, "),(") === false) {
197 $row .= $this->_drawPath($polygon, $scale_data);
198 } else {
199 // Seperate outer and inner polygons
200 $parts = explode("),(", $polygon);
201 $outer = $parts[0];
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);
211 $row .= '"';
212 foreach ($polygon_options as $option => $val) {
213 $row .= ' ' . $option . '="' . trim($val) . '"';
215 $row .= '/>';
216 return $row;
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,
238 'label' => $label,
239 'fontSize' => 10,
241 if ($srid == 0) {
242 $srid = 4326;
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) . '));';
252 return $row;
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];
272 $row .= ' Z ';
274 return $row;
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) {
291 $no_of_lines = 1;
293 $wkt = 'POLYGON(';
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) {
298 $no_of_points = 4;
300 $wkt .= '(';
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);
310 $wkt .= '),';
312 $wkt = substr($wkt, 0, strlen($wkt) - 1);
313 $wkt .= ')';
314 return $wkt;
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) {
326 $no_of_points = count($ring);
328 // If the last point is same as the first point ignore it
329 $last = count($ring) - 1;
330 if (($ring[0]['x'] == $ring[$last]['x']) && ($ring[0]['y'] == $ring[$last]['y'])) {
331 $no_of_points--;
334 // _n-1
335 // A = _1_ \ (X(i) * Y(i+1)) - (Y(i) * X(i+1))
336 // 2 /__
337 // i=0
338 $area = 0;
339 for ($i = 0; $i < $no_of_points; $i++) {
340 $j = ($i + 1) % $no_of_points;
341 $area += $ring[$i]['x'] * $ring[$j]['y'];
342 $area -= $ring[$i]['y'] * $ring[$j]['x'];
344 $area /= 2.0;
346 return $area;
350 * Determines whether a set of points represents an outer ring.
351 * If points are in clockwise orientation then, they form an outer ring.
353 * @param array $ring array of points forming the ring
355 * @return whether a set of points represents an outer ring.
357 public static function isOuterRing($ring)
359 // If area is negative then it's in clockwise orientation, i.e. it's an outer ring
360 if (PMA_GIS_Polygon::area($ring) < 0) {
361 return true;
363 return false;
367 * Determines whether a given point is inside a given polygon.
369 * @param array $point x, y coordinates of the point
370 * @param array $ring array of points forming the ring
372 * @return whether a given point is inside a given polygon
374 public static function isPointInsidePolygon($point, $polygon)
376 // If first point is repeated at the end remove it
377 $last = count($polygon) - 1;
378 if (($polygon[0]['x'] == $polygon[$last]['x']) && ($polygon[0]['y'] == $polygon[$last]['y'])) {
379 $polygon = array_slice($polygon, 0, $last);
382 $no_of_points = count($polygon);
383 $counter = 0;
385 // Use ray casting algorithm
386 $p1 = $polygon[0];
387 for ($i = 1; $i <= $no_of_points; $i++) {
388 $p2 = $polygon[$i % $no_of_points];
389 if ($point['y'] > min(array($p1['y'], $p2['y']))) {
390 if ($point['y'] <= max(array($p1['y'], $p2['y']))) {
391 if ($point['x'] <= max(array($p1['x'], $p2['x']))) {
392 if ($p1['y'] != $p2['y']) {
393 $xinters = ($point['y'] - $p1['y']) * ($p2['x'] - $p1['x']) / ($p2['y'] - $p1['y']) + $p1['x'];
394 if ($p1['x'] == $p2['x'] || $point['x'] <= $xinters) {
395 $counter++;
401 $p1 = $p2;
404 if ($counter % 2 == 0) {
405 return false;
406 } else {
407 return true;
412 * Returns a point that is guaranteed to be on the surface of the ring.
413 * (for simple closed rings)
415 * @param array $ring array of points forming the ring
417 public static function getPointOnSurface($ring)
419 // Find two consecutive distinct points.
420 for ($i = 0; $i < count($ring) - 1; $i++) {
421 if ($ring[$i]['y'] != $ring[$i + 1]['y']) {
422 $x0 = $ring[$i]['x'];
423 $x1 = $ring[$i + 1]['x'];
424 $y0 = $ring[$i]['y'];
425 $y1 = $ring[$i + 1]['y'];
426 break;
430 if (! isset($x0)) {
431 return false;
434 // Find the mid point
435 $x2 = ($x0 + $x1) / 2;
436 $y2 = ($y0 + $y1) / 2;
438 // Always keep $epsilon < 1 to go with the reduction logic found later in this method
439 $epsilon = 0.1;
440 $denominator = sqrt(pow(($y1 - $y0), 2) + pow(($x0 - $x1), 2));
441 $pointA = array(); $pointB = array();
443 while (true) {
444 // Get the points on either sides of the line with a distance of epsilon to the mid point
445 $pointA['x'] = $x2 + ($epsilon * ($y1 - $y0)) / $denominator;
446 $pointA['y'] = $y2 + ($pointA['x'] - $x2) * ($x0 - $x1) / ($y1 - $y0);
448 $pointB['x'] = $x2 + ($epsilon * ($y1 - $y0)) / (0 - $denominator);
449 $pointB['y'] = $y2 + ($pointB['x'] - $x2) * ($x0 - $x1) / ($y1 - $y0);
451 // One of the points should be inside the polygon, unless epcilon chosen is too large
452 if (PMA_GIS_Polygon::isPointInsidePolygon($pointA, $ring)) {
453 return $pointA;
454 } elseif (PMA_GIS_Polygon::isPointInsidePolygon($pointB, $ring)) {
455 return $pointB;
458 // If both are outside the polygon reduce the epsilon and recalculate the points
459 // (reduce exponentially for faster convergance)
460 else {
461 $epsilon = pow($epsilon, 2);
462 if ($epsilon == 0) {
463 return false;
470 /** Generate parameters for the GIS data editor from the value of the GIS column.
472 * @param string $value of the GIS column
473 * @param index $index of the geometry
475 * @return parameters for the GIS data editor from the value of the GIS column
477 public function generateParams($value, $index = -1)
479 if ($index == -1) {
480 $index = 0;
481 $params = array();
482 $data = PMA_GIS_Geometry::generateParams($value);
483 $params['srid'] = $data['srid'];
484 $wkt = $data['wkt'];
485 } else {
486 $params[$index]['gis_type'] = 'POLYGON';
487 $wkt = $value;
490 // Trim to remove leading 'POLYGON((' and trailing '))'
491 $polygon = substr($wkt, 9, (strlen($wkt) - 11));
492 // Seperate each linestring
493 $linerings = explode("),(", $polygon);
494 $params[$index]['POLYGON']['no_of_lines'] = count($linerings);
496 $j = 0;
497 foreach ($linerings as $linering) {
498 $points_arr = $this->extractPoints($linering, null);
499 $no_of_points = count($points_arr);
500 $params[$index]['POLYGON'][$j]['no_of_points'] = $no_of_points;
501 for ($i = 0; $i < $no_of_points; $i++) {
502 $params[$index]['POLYGON'][$j][$i]['x'] = $points_arr[$i][0];
503 $params[$index]['POLYGON'][$j][$i]['y'] = $points_arr[$i][1];
505 $j++;
507 return $params;