Advisor: mark that 'Rate of reading fixed position' may be wrong, requires further...
[phpmyadmin/thilanka.git] / libraries / gis / pma_gis_multipolygon.php
blobb5765efb2e3ce6edbd9090c7d13cb5ced79a9b9c
1 <?php
2 /**
3 * Handles the visualization of GIS MULTIPOLYGON objects.
5 * @package phpMyAdmin-GIS
6 */
7 class PMA_GIS_Multipolygon 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 'MULTIPOLYGON(((' and trailing ')))'
46 $multipolygon = substr($spatial, 15, (strlen($spatial) - 18));
47 // Seperate each polygon
48 $polygons = explode(")),((", $multipolygon);
50 foreach ($polygons as $polygon) {
51 // If the polygon doesnt have an inner polygon
52 if (strpos($polygon, "),(") === false) {
53 $min_max = $this->setMinMax($polygon, $min_max);
54 } else {
55 // Seperate outer and inner polygons
56 $parts = explode("),(", $polygon);
57 $outer = $parts[0];
58 $inner = array_slice($parts, 1);
60 $min_max = $this->setMinMax($outer, $min_max);
62 foreach ($inner as $inner_poly) {
63 $min_max = $this->setMinMax($inner_poly, $min_max);
68 return $min_max;
71 /**
72 * Adds to the PNG image object, the data related to a row in the GIS dataset.
74 * @param string $spatial GIS MULTIPOLYGON object
75 * @param string $label Label for the GIS MULTIPOLYGON object
76 * @param string $fill_color Color for the GIS MULTIPOLYGON object
77 * @param array $scale_data Array containing data related to scaling
78 * @param image $image Image object
80 * @return the modified image object
82 public function prepareRowAsPng($spatial, $label, $fill_color, $scale_data, $image)
84 // allocate colors
85 $black = imagecolorallocate($image, 0, 0, 0);
86 $red = hexdec(substr($fill_color, 1, 2));
87 $green = hexdec(substr($fill_color, 3, 2));
88 $blue = hexdec(substr($fill_color, 4, 2));
89 $color = imagecolorallocate($image, $red, $green, $blue);
91 // Trim to remove leading 'MULTIPOLYGON(((' and trailing ')))'
92 $multipolygon = substr($spatial, 15, (strlen($spatial) - 18));
93 // Seperate each polygon
94 $polygons = explode(")),((", $multipolygon);
96 $first_poly = true;
97 foreach ($polygons as $polygon) {
98 // If the polygon doesnt have an inner polygon
99 if (strpos($polygon, "),(") === false) {
100 $points_arr = $this->extractPoints($polygon, $scale_data, true);
101 } else {
102 // Seperate outer and inner polygons
103 $parts = explode("),(", $polygon);
104 $outer = $parts[0];
105 $inner = array_slice($parts, 1);
107 $points_arr = $this->extractPoints($outer, $scale_data, true);
109 foreach ($inner as $inner_poly) {
110 $points_arr = array_merge(
111 $points_arr, $this->extractPoints($inner_poly, $scale_data, true)
115 // draw polygon
116 imagefilledpolygon($image, $points_arr, sizeof($points_arr) / 2, $color);
117 // mark label point if applicable
118 if (isset($label) && trim($label) != '' && $first_poly) {
119 $label_point = array($points_arr[2], $points_arr[3]);
121 $first_poly = false;
123 // print label if applicable
124 if (isset($label_point)) {
125 imagestring($image, 1, $points_arr[2], $points_arr[3], trim($label), $black);
127 return $image;
131 * Adds to the TCPDF instance, the data related to a row in the GIS dataset.
133 * @param string $spatial GIS MULTIPOLYGON object
134 * @param string $label Label for the GIS MULTIPOLYGON object
135 * @param string $fill_color Color for the GIS MULTIPOLYGON object
136 * @param array $scale_data Array containing data related to scaling
137 * @param image $pdf TCPDF instance
139 * @return the modified TCPDF instance
141 public function prepareRowAsPdf($spatial, $label, $fill_color, $scale_data, $pdf)
143 // allocate colors
144 $red = hexdec(substr($fill_color, 1, 2));
145 $green = hexdec(substr($fill_color, 3, 2));
146 $blue = hexdec(substr($fill_color, 4, 2));
147 $color = array($red, $green, $blue);
149 // Trim to remove leading 'MULTIPOLYGON(((' and trailing ')))'
150 $multipolygon = substr($spatial, 15, (strlen($spatial) - 18));
151 // Seperate each polygon
152 $polygons = explode(")),((", $multipolygon);
154 $first_poly = true;
155 foreach ($polygons as $polygon) {
156 // If the polygon doesnt have an inner polygon
157 if (strpos($polygon, "),(") === false) {
158 $points_arr = $this->extractPoints($polygon, $scale_data, true);
159 } else {
160 // Seperate outer and inner polygons
161 $parts = explode("),(", $polygon);
162 $outer = $parts[0];
163 $inner = array_slice($parts, 1);
165 $points_arr = $this->extractPoints($outer, $scale_data, true);
167 foreach ($inner as $inner_poly) {
168 $points_arr = array_merge(
169 $points_arr,
170 $this->extractPoints($inner_poly, $scale_data, true)
174 // draw polygon
175 $pdf->Polygon($points_arr, 'F*', array(), $color, true);
176 // mark label point if applicable
177 if (isset($label) && trim($label) != '' && $first_poly) {
178 $label_point = array($points_arr[2], $points_arr[3]);
180 $first_poly = false;
183 // print label if applicable
184 if (isset($label_point)) {
185 $pdf->SetXY($label_point[0], $label_point[1]);
186 $pdf->SetFontSize(5);
187 $pdf->Cell(0, 0, trim($label));
189 return $pdf;
193 * Prepares and returns the code related to a row in the GIS dataset as SVG.
195 * @param string $spatial GIS MULTIPOLYGON object
196 * @param string $label Label for the GIS MULTIPOLYGON object
197 * @param string $fill_color Color for the GIS MULTIPOLYGON object
198 * @param array $scale_data Array containing data related to scaling
200 * @return the code related to a row in the GIS dataset
202 public function prepareRowAsSvg($spatial, $label, $fill_color, $scale_data)
204 $polygon_options = array(
205 'name' => $label,
206 'class' => 'multipolygon vector',
207 'stroke' => 'black',
208 'stroke-width'=> 0.5,
209 'fill' => $fill_color,
210 'fill-rule' => 'evenodd',
211 'fill-opacity'=> 0.8,
214 $row = '';
216 // Trim to remove leading 'MULTIPOLYGON(((' and trailing ')))'
217 $multipolygon = substr($spatial, 15, (strlen($spatial) - 18));
218 // Seperate each polygon
219 $polygons = explode(")),((", $multipolygon);
221 foreach ($polygons as $polygon) {
222 $row .= '<path d="';
224 // If the polygon doesnt have an inner polygon
225 if (strpos($polygon, "),(") === false) {
226 $row .= $this->_drawPath($polygon, $scale_data);
227 } else {
228 // Seperate outer and inner polygons
229 $parts = explode("),(", $polygon);
230 $outer = $parts[0];
231 $inner = array_slice($parts, 1);
233 $row .= $this->_drawPath($outer, $scale_data);
235 foreach ($inner as $inner_poly) {
236 $row .= $this->_drawPath($inner_poly, $scale_data);
239 $polygon_options['id'] = $label . rand();
240 $row .= '"';
241 foreach ($polygon_options as $option => $val) {
242 $row .= ' ' . $option . '="' . trim($val) . '"';
244 $row .= '/>';
247 return $row;
251 * Prepares JavaScript related to a row in the GIS dataset
252 * to visualize it with OpenLayers.
254 * @param string $spatial GIS MULTIPOLYGON object
255 * @param int $srid Spatial reference ID
256 * @param string $label Label for the GIS MULTIPOLYGON object
257 * @param string $fill_color Color for the GIS MULTIPOLYGON object
258 * @param array $scale_data Array containing data related to scaling
260 * @return JavaScript related to a row in the GIS dataset
262 public function prepareRowAsOl($spatial, $srid, $label, $fill_color, $scale_data)
264 $style_options = array(
265 'strokeColor' => '#000000',
266 'strokeWidth' => 0.5,
267 'fillColor' => $fill_color,
268 'fillOpacity' => 0.8,
269 'label' => $label,
270 'fontSize' => 10,
272 if ($srid == 0) {
273 $srid = 4326;
275 $row = $this->getBoundsForOl($srid, $scale_data);
277 // Trim to remove leading 'MULTIPOLYGON(((' and trailing ')))'
278 $multipolygon = substr($spatial, 15, (strlen($spatial) - 18));
279 // Seperate each polygon
280 $polygons = explode(")),((", $multipolygon);
282 $row .= 'vectorLayer.addFeatures(new OpenLayers.Feature.Vector('
283 . 'new OpenLayers.Geometry.MultiPolygon(new Array(';
285 foreach ($polygons as $polygon) {
286 $row .= $this->addPointsForOpenLayersPolygon($polygon, $srid);
288 $row = substr($row, 0, strlen($row) - 2);
289 $row .= ')), null, ' . json_encode($style_options) . '));';
290 return $row;
294 * Draws a ring of the polygon using SVG path element.
296 * @param string $polygon The ring
297 * @param array $scale_data Array containing data related to scaling
299 * @return the code to draw the ring
301 private function _drawPath($polygon, $scale_data)
303 $points_arr = $this->extractPoints($polygon, $scale_data);
305 $row = ' M ' . $points_arr[0][0] . ', ' . $points_arr[0][1];
306 $other_points = array_slice($points_arr, 1, count($points_arr) - 2);
307 foreach ($other_points as $point) {
308 $row .= ' L ' . $point[0] . ', ' . $point[1];
310 $row .= ' Z ';
312 return $row;
316 * Generate the WKT with the set of parameters passed by the GIS editor.
318 * @param array $gis_data GIS data
319 * @param int $index Index into the parameter object
320 * @param string $empty Value for empty points
322 * @return WKT with the set of parameters passed by the GIS editor
324 public function generateWkt($gis_data, $index, $empty = '')
326 $no_of_polygons = isset($gis_data[$index]['MULTIPOLYGON']['no_of_polygons'])
327 ? $gis_data[$index]['MULTIPOLYGON']['no_of_polygons'] : 1;
328 if ($no_of_polygons < 1) {
329 $no_of_polygons = 1;
331 $wkt = 'MULTIPOLYGON(';
332 for ($k = 0; $k < $no_of_polygons; $k++) {
333 $no_of_lines = isset($gis_data[$index]['MULTIPOLYGON'][$k]['no_of_lines'])
334 ? $gis_data[$index]['MULTIPOLYGON'][$k]['no_of_lines'] : 1;
335 if ($no_of_lines < 1) {
336 $no_of_lines = 1;
338 $wkt .= '(';
339 for ($i = 0; $i < $no_of_lines; $i++) {
340 $no_of_points = isset($gis_data[$index]['MULTIPOLYGON'][$k][$i]['no_of_points'])
341 ? $gis_data[$index]['MULTIPOLYGON'][$k][$i]['no_of_points'] : 4;
342 if ($no_of_points < 4) {
343 $no_of_points = 4;
345 $wkt .= '(';
346 for ($j = 0; $j < $no_of_points; $j++) {
347 $wkt .= ((isset($gis_data[$index]['MULTIPOLYGON'][$k][$i][$j]['x'])
348 && trim($gis_data[$index]['MULTIPOLYGON'][$k][$i][$j]['x']) != '')
349 ? $gis_data[$index]['MULTIPOLYGON'][$k][$i][$j]['x'] : $empty)
350 . ' ' . ((isset($gis_data[$index]['MULTIPOLYGON'][$k][$i][$j]['y'])
351 && trim($gis_data[$index]['MULTIPOLYGON'][$k][$i][$j]['y']) != '')
352 ? $gis_data[$index]['MULTIPOLYGON'][$k][$i][$j]['y'] : $empty) .',';
354 $wkt = substr($wkt, 0, strlen($wkt) - 1);
355 $wkt .= '),';
357 $wkt = substr($wkt, 0, strlen($wkt) - 1);
358 $wkt .= '),';
360 $wkt = substr($wkt, 0, strlen($wkt) - 1);
361 $wkt .= ')';
362 return $wkt;
366 * Generate the WKT for the data from ESRI shape files.
368 * @param array $row_data GIS data
370 * @return the WKT for the data from ESRI shape files
372 public function getShape($row_data)
374 // Determines whether each line ring is an inner ring or an outer ring.
375 // If it's an inner ring get a point on the surface which can be used to
376 // correctly classify inner rings to their respective outer rings.
377 require_once './libraries/gis/pma_gis_polygon.php';
378 foreach ($row_data['parts'] as $i => $ring) {
379 $row_data['parts'][$i]['isOuter'] = PMA_GIS_Polygon::isOuterRing($ring['points']);
382 // Find points on surface for inner rings
383 foreach ($row_data['parts'] as $i => $ring) {
384 if (! $ring['isOuter']) {
385 $row_data['parts'][$i]['pointOnSurface'] = PMA_GIS_Polygon::getPointOnSurface($ring['points']);
389 // Classify inner rings to their respective outer rings.
390 foreach ($row_data['parts'] as $j => $ring1) {
391 if (! $ring1['isOuter']) {
392 foreach ($row_data['parts'] as $k => $ring2) {
393 if ($ring2['isOuter']) {
394 // If the pointOnSurface of the inner ring is also inside the outer ring
395 if (PMA_GIS_Polygon::isPointInsidePolygon($ring1['pointOnSurface'], $ring2['points'])) {
396 if (! isset($ring2['inner'])) {
397 $row_data['parts'][$k]['inner'] = array();
399 $row_data['parts'][$k]['inner'][] = $j;
406 $wkt = 'MULTIPOLYGON(';
407 // for each polygon
408 foreach ($row_data['parts'] as $ring) {
409 if ($ring['isOuter']) {
410 $wkt .= '('; // start of polygon
412 $wkt .= '('; // start of outer ring
413 foreach($ring['points'] as $point) {
414 $wkt .= $point['x'] . ' ' . $point['y'] . ',';
416 $wkt = substr($wkt, 0, strlen($wkt) - 1);
417 $wkt .= ')'; // end of outer ring
419 // inner rings if any
420 if (isset($ring['inner'])) {
421 foreach ($ring['inner'] as $j) {
422 $wkt .= ',('; // start of inner ring
423 foreach ($row_data['parts'][$j]['points'] as $innerPoint) {
424 $wkt .= $innerPoint['x'] . ' ' . $innerPoint['y'] . ',';
426 $wkt = substr($wkt, 0, strlen($wkt) - 1);
427 $wkt .= ')'; // end of inner ring
431 $wkt .= '),'; // end of polygon
434 $wkt = substr($wkt, 0, strlen($wkt) - 1);
436 $wkt .= ')'; // end of multipolygon
437 return $wkt;
441 * Generate parameters for the GIS data editor from the value of the GIS column.
443 * @param string $value of the GIS column
444 * @param index $index of the geometry
446 * @return parameters for the GIS data editor from the value of the GIS column
448 public function generateParams($value, $index = -1)
450 if ($index == -1) {
451 $index = 0;
452 $params = array();
453 $data = PMA_GIS_Geometry::generateParams($value);
454 $params['srid'] = $data['srid'];
455 $wkt = $data['wkt'];
456 } else {
457 $params[$index]['gis_type'] = 'MULTIPOLYGON';
458 $wkt = $value;
461 // Trim to remove leading 'MULTIPOLYGON(((' and trailing ')))'
462 $multipolygon = substr($wkt, 15, (strlen($wkt) - 18));
463 // Seperate each polygon
464 $polygons = explode(")),((", $multipolygon);
465 $params[$index]['MULTIPOLYGON']['no_of_polygons'] = count($polygons);
467 $k = 0;
468 foreach ($polygons as $polygon) {
469 // If the polygon doesnt have an inner polygon
470 if (strpos($polygon, "),(") === false) {
471 $params[$index]['MULTIPOLYGON'][$k]['no_of_lines'] = 1;
472 $points_arr = $this->extractPoints($polygon, null);
473 $no_of_points = count($points_arr);
474 $params[$index]['MULTIPOLYGON'][$k][0]['no_of_points'] = $no_of_points;
475 for ($i = 0; $i < $no_of_points; $i++) {
476 $params[$index]['MULTIPOLYGON'][$k][0][$i]['x'] = $points_arr[$i][0];
477 $params[$index]['MULTIPOLYGON'][$k][0][$i]['y'] = $points_arr[$i][1];
479 } else {
480 // Seperate outer and inner polygons
481 $parts = explode("),(", $polygon);
482 $params[$index]['MULTIPOLYGON'][$k]['no_of_lines'] = count($parts);
483 $j = 0;
484 foreach ($parts as $ring) {
485 $points_arr = $this->extractPoints($ring, null);
486 $no_of_points = count($points_arr);
487 $params[$index]['MULTIPOLYGON'][$k][$j]['no_of_points'] = $no_of_points;
488 for ($i = 0; $i < $no_of_points; $i++) {
489 $params[$index]['MULTIPOLYGON'][$k][$j][$i]['x'] = $points_arr[$i][0];
490 $params[$index]['MULTIPOLYGON'][$k][$j][$i]['y'] = $points_arr[$i][1];
492 $j++;
495 $k++;
497 return $params;