Fix format strings
[phpmyadmin/madhuracj.git] / libraries / gis / pma_gis_multipolygon.php
blob732f5fea9bc935a961842c60c737686310d5a55f
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 doesn't have an inner ring, use polygon itself
52 if (strpos($polygon, "),(") === false) {
53 $ring = $polygon;
54 } else {
55 // Seperate outer ring and use it to determin min-max
56 $parts = explode("),(", $polygon);
57 $ring = $parts[0];
59 $min_max = $this->setMinMax($ring, $min_max);
62 return $min_max;
65 /**
66 * Adds to the PNG image object, the data related to a row in the GIS dataset.
68 * @param string $spatial GIS MULTIPOLYGON object
69 * @param string $label Label for the GIS MULTIPOLYGON object
70 * @param string $fill_color Color for the GIS MULTIPOLYGON object
71 * @param array $scale_data Array containing data related to scaling
72 * @param image $image Image object
74 * @return the modified image object
76 public function prepareRowAsPng($spatial, $label, $fill_color, $scale_data, $image)
78 // allocate colors
79 $black = imagecolorallocate($image, 0, 0, 0);
80 $red = hexdec(substr($fill_color, 1, 2));
81 $green = hexdec(substr($fill_color, 3, 2));
82 $blue = hexdec(substr($fill_color, 4, 2));
83 $color = imagecolorallocate($image, $red, $green, $blue);
85 // Trim to remove leading 'MULTIPOLYGON(((' and trailing ')))'
86 $multipolygon = substr($spatial, 15, (strlen($spatial) - 18));
87 // Seperate each polygon
88 $polygons = explode(")),((", $multipolygon);
90 $first_poly = true;
91 foreach ($polygons as $polygon) {
92 // If the polygon doesnt have an inner polygon
93 if (strpos($polygon, "),(") === false) {
94 $points_arr = $this->extractPoints($polygon, $scale_data, true);
95 } else {
96 // Seperate outer and inner polygons
97 $parts = explode("),(", $polygon);
98 $outer = $parts[0];
99 $inner = array_slice($parts, 1);
101 $points_arr = $this->extractPoints($outer, $scale_data, true);
103 foreach ($inner as $inner_poly) {
104 $points_arr = array_merge(
105 $points_arr, $this->extractPoints($inner_poly, $scale_data, true)
109 // draw polygon
110 imagefilledpolygon($image, $points_arr, sizeof($points_arr) / 2, $color);
111 // mark label point if applicable
112 if (isset($label) && trim($label) != '' && $first_poly) {
113 $label_point = array($points_arr[2], $points_arr[3]);
115 $first_poly = false;
117 // print label if applicable
118 if (isset($label_point)) {
119 imagestring($image, 1, $points_arr[2], $points_arr[3], trim($label), $black);
121 return $image;
125 * Adds to the TCPDF instance, the data related to a row in the GIS dataset.
127 * @param string $spatial GIS MULTIPOLYGON object
128 * @param string $label Label for the GIS MULTIPOLYGON object
129 * @param string $fill_color Color for the GIS MULTIPOLYGON object
130 * @param array $scale_data Array containing data related to scaling
131 * @param image $pdf TCPDF instance
133 * @return the modified TCPDF instance
135 public function prepareRowAsPdf($spatial, $label, $fill_color, $scale_data, $pdf)
137 // allocate colors
138 $red = hexdec(substr($fill_color, 1, 2));
139 $green = hexdec(substr($fill_color, 3, 2));
140 $blue = hexdec(substr($fill_color, 4, 2));
141 $color = array($red, $green, $blue);
143 // Trim to remove leading 'MULTIPOLYGON(((' and trailing ')))'
144 $multipolygon = substr($spatial, 15, (strlen($spatial) - 18));
145 // Seperate each polygon
146 $polygons = explode(")),((", $multipolygon);
148 $first_poly = true;
149 foreach ($polygons as $polygon) {
150 // If the polygon doesnt have an inner polygon
151 if (strpos($polygon, "),(") === false) {
152 $points_arr = $this->extractPoints($polygon, $scale_data, true);
153 } else {
154 // Seperate outer and inner polygons
155 $parts = explode("),(", $polygon);
156 $outer = $parts[0];
157 $inner = array_slice($parts, 1);
159 $points_arr = $this->extractPoints($outer, $scale_data, true);
161 foreach ($inner as $inner_poly) {
162 $points_arr = array_merge(
163 $points_arr,
164 $this->extractPoints($inner_poly, $scale_data, true)
168 // draw polygon
169 $pdf->Polygon($points_arr, 'F*', array(), $color, true);
170 // mark label point if applicable
171 if (isset($label) && trim($label) != '' && $first_poly) {
172 $label_point = array($points_arr[2], $points_arr[3]);
174 $first_poly = false;
177 // print label if applicable
178 if (isset($label_point)) {
179 $pdf->SetXY($label_point[0], $label_point[1]);
180 $pdf->SetFontSize(5);
181 $pdf->Cell(0, 0, trim($label));
183 return $pdf;
187 * Prepares and returns the code related to a row in the GIS dataset as SVG.
189 * @param string $spatial GIS MULTIPOLYGON object
190 * @param string $label Label for the GIS MULTIPOLYGON object
191 * @param string $fill_color Color for the GIS MULTIPOLYGON object
192 * @param array $scale_data Array containing data related to scaling
194 * @return the code related to a row in the GIS dataset
196 public function prepareRowAsSvg($spatial, $label, $fill_color, $scale_data)
198 $polygon_options = array(
199 'name' => $label,
200 'class' => 'multipolygon vector',
201 'stroke' => 'black',
202 'stroke-width'=> 0.5,
203 'fill' => $fill_color,
204 'fill-rule' => 'evenodd',
205 'fill-opacity'=> 0.8,
208 $row = '';
210 // Trim to remove leading 'MULTIPOLYGON(((' and trailing ')))'
211 $multipolygon = substr($spatial, 15, (strlen($spatial) - 18));
212 // Seperate each polygon
213 $polygons = explode(")),((", $multipolygon);
215 foreach ($polygons as $polygon) {
216 $row .= '<path d="';
218 // If the polygon doesnt have an inner polygon
219 if (strpos($polygon, "),(") === false) {
220 $row .= $this->_drawPath($polygon, $scale_data);
221 } else {
222 // Seperate outer and inner polygons
223 $parts = explode("),(", $polygon);
224 $outer = $parts[0];
225 $inner = array_slice($parts, 1);
227 $row .= $this->_drawPath($outer, $scale_data);
229 foreach ($inner as $inner_poly) {
230 $row .= $this->_drawPath($inner_poly, $scale_data);
233 $polygon_options['id'] = $label . rand();
234 $row .= '"';
235 foreach ($polygon_options as $option => $val) {
236 $row .= ' ' . $option . '="' . trim($val) . '"';
238 $row .= '/>';
241 return $row;
245 * Prepares JavaScript related to a row in the GIS dataset
246 * to visualize it with OpenLayers.
248 * @param string $spatial GIS MULTIPOLYGON object
249 * @param int $srid Spatial reference ID
250 * @param string $label Label for the GIS MULTIPOLYGON object
251 * @param string $fill_color Color for the GIS MULTIPOLYGON object
252 * @param array $scale_data Array containing data related to scaling
254 * @return JavaScript related to a row in the GIS dataset
256 public function prepareRowAsOl($spatial, $srid, $label, $fill_color, $scale_data)
258 $style_options = array(
259 'strokeColor' => '#000000',
260 'strokeWidth' => 0.5,
261 'fillColor' => $fill_color,
262 'fillOpacity' => 0.8,
263 'label' => $label,
264 'fontSize' => 10,
266 if ($srid == 0) {
267 $srid = 4326;
269 $row = $this->getBoundsForOl($srid, $scale_data);
271 // Trim to remove leading 'MULTIPOLYGON(((' and trailing ')))'
272 $multipolygon = substr($spatial, 15, (strlen($spatial) - 18));
273 // Seperate each polygon
274 $polygons = explode(")),((", $multipolygon);
276 $row .= 'vectorLayer.addFeatures(new OpenLayers.Feature.Vector('
277 . 'new OpenLayers.Geometry.MultiPolygon(new Array(';
279 foreach ($polygons as $polygon) {
280 $row .= $this->addPointsForOpenLayersPolygon($polygon, $srid);
282 $row = substr($row, 0, strlen($row) - 2);
283 $row .= ')), null, ' . json_encode($style_options) . '));';
284 return $row;
288 * Draws a ring of the polygon using SVG path element.
290 * @param string $polygon The ring
291 * @param array $scale_data Array containing data related to scaling
293 * @return the code to draw the ring
295 private function _drawPath($polygon, $scale_data)
297 $points_arr = $this->extractPoints($polygon, $scale_data);
299 $row = ' M ' . $points_arr[0][0] . ', ' . $points_arr[0][1];
300 $other_points = array_slice($points_arr, 1, count($points_arr) - 2);
301 foreach ($other_points as $point) {
302 $row .= ' L ' . $point[0] . ', ' . $point[1];
304 $row .= ' Z ';
306 return $row;
310 * Generate the WKT with the set of parameters passed by the GIS editor.
312 * @param array $gis_data GIS data
313 * @param int $index Index into the parameter object
314 * @param string $empty Value for empty points
316 * @return WKT with the set of parameters passed by the GIS editor
318 public function generateWkt($gis_data, $index, $empty = '')
320 $no_of_polygons = isset($gis_data[$index]['MULTIPOLYGON']['no_of_polygons'])
321 ? $gis_data[$index]['MULTIPOLYGON']['no_of_polygons'] : 1;
322 if ($no_of_polygons < 1) {
323 $no_of_polygons = 1;
325 $wkt = 'MULTIPOLYGON(';
326 for ($k = 0; $k < $no_of_polygons; $k++) {
327 $no_of_lines = isset($gis_data[$index]['MULTIPOLYGON'][$k]['no_of_lines'])
328 ? $gis_data[$index]['MULTIPOLYGON'][$k]['no_of_lines'] : 1;
329 if ($no_of_lines < 1) {
330 $no_of_lines = 1;
332 $wkt .= '(';
333 for ($i = 0; $i < $no_of_lines; $i++) {
334 $no_of_points = isset($gis_data[$index]['MULTIPOLYGON'][$k][$i]['no_of_points'])
335 ? $gis_data[$index]['MULTIPOLYGON'][$k][$i]['no_of_points'] : 4;
336 if ($no_of_points < 4) {
337 $no_of_points = 4;
339 $wkt .= '(';
340 for ($j = 0; $j < $no_of_points; $j++) {
341 $wkt .= ((isset($gis_data[$index]['MULTIPOLYGON'][$k][$i][$j]['x'])
342 && trim($gis_data[$index]['MULTIPOLYGON'][$k][$i][$j]['x']) != '')
343 ? $gis_data[$index]['MULTIPOLYGON'][$k][$i][$j]['x'] : $empty)
344 . ' ' . ((isset($gis_data[$index]['MULTIPOLYGON'][$k][$i][$j]['y'])
345 && trim($gis_data[$index]['MULTIPOLYGON'][$k][$i][$j]['y']) != '')
346 ? $gis_data[$index]['MULTIPOLYGON'][$k][$i][$j]['y'] : $empty) .',';
348 $wkt = substr($wkt, 0, strlen($wkt) - 1);
349 $wkt .= '),';
351 $wkt = substr($wkt, 0, strlen($wkt) - 1);
352 $wkt .= '),';
354 $wkt = substr($wkt, 0, strlen($wkt) - 1);
355 $wkt .= ')';
356 return $wkt;
360 * Generate the WKT for the data from ESRI shape files.
362 * @param array $row_data GIS data
364 * @return the WKT for the data from ESRI shape files
366 public function getShape($row_data)
368 // Determines whether each line ring is an inner ring or an outer ring.
369 // If it's an inner ring get a point on the surface which can be used to
370 // correctly classify inner rings to their respective outer rings.
371 include_once './libraries/gis/pma_gis_polygon.php';
372 foreach ($row_data['parts'] as $i => $ring) {
373 $row_data['parts'][$i]['isOuter'] = PMA_GIS_Polygon::isOuterRing($ring['points']);
376 // Find points on surface for inner rings
377 foreach ($row_data['parts'] as $i => $ring) {
378 if (! $ring['isOuter']) {
379 $row_data['parts'][$i]['pointOnSurface']
380 = PMA_GIS_Polygon::getPointOnSurface($ring['points']);
384 // Classify inner rings to their respective outer rings.
385 foreach ($row_data['parts'] as $j => $ring1) {
386 if (! $ring1['isOuter']) {
387 foreach ($row_data['parts'] as $k => $ring2) {
388 if ($ring2['isOuter']) {
389 // If the pointOnSurface of the inner ring
390 // is also inside the outer ring
391 if (PMA_GIS_Polygon::isPointInsidePolygon(
392 $ring1['pointOnSurface'], $ring2['points']
393 )) {
394 if (! isset($ring2['inner'])) {
395 $row_data['parts'][$k]['inner'] = array();
397 $row_data['parts'][$k]['inner'][] = $j;
404 $wkt = 'MULTIPOLYGON(';
405 // for each polygon
406 foreach ($row_data['parts'] as $ring) {
407 if ($ring['isOuter']) {
408 $wkt .= '('; // start of polygon
410 $wkt .= '('; // start of outer ring
411 foreach ($ring['points'] as $point) {
412 $wkt .= $point['x'] . ' ' . $point['y'] . ',';
414 $wkt = substr($wkt, 0, strlen($wkt) - 1);
415 $wkt .= ')'; // end of outer ring
417 // inner rings if any
418 if (isset($ring['inner'])) {
419 foreach ($ring['inner'] as $j) {
420 $wkt .= ',('; // start of inner ring
421 foreach ($row_data['parts'][$j]['points'] as $innerPoint) {
422 $wkt .= $innerPoint['x'] . ' ' . $innerPoint['y'] . ',';
424 $wkt = substr($wkt, 0, strlen($wkt) - 1);
425 $wkt .= ')'; // end of inner ring
429 $wkt .= '),'; // end of polygon
432 $wkt = substr($wkt, 0, strlen($wkt) - 1);
434 $wkt .= ')'; // end of multipolygon
435 return $wkt;
439 * Generate parameters for the GIS data editor from the value of the GIS column.
441 * @param string $value of the GIS column
442 * @param index $index of the geometry
444 * @return parameters for the GIS data editor from the value of the GIS column
446 public function generateParams($value, $index = -1)
448 if ($index == -1) {
449 $index = 0;
450 $params = array();
451 $data = PMA_GIS_Geometry::generateParams($value);
452 $params['srid'] = $data['srid'];
453 $wkt = $data['wkt'];
454 } else {
455 $params[$index]['gis_type'] = 'MULTIPOLYGON';
456 $wkt = $value;
459 // Trim to remove leading 'MULTIPOLYGON(((' and trailing ')))'
460 $multipolygon = substr($wkt, 15, (strlen($wkt) - 18));
461 // Seperate each polygon
462 $polygons = explode(")),((", $multipolygon);
463 $params[$index]['MULTIPOLYGON']['no_of_polygons'] = count($polygons);
465 $k = 0;
466 foreach ($polygons as $polygon) {
467 // If the polygon doesnt have an inner polygon
468 if (strpos($polygon, "),(") === false) {
469 $params[$index]['MULTIPOLYGON'][$k]['no_of_lines'] = 1;
470 $points_arr = $this->extractPoints($polygon, null);
471 $no_of_points = count($points_arr);
472 $params[$index]['MULTIPOLYGON'][$k][0]['no_of_points'] = $no_of_points;
473 for ($i = 0; $i < $no_of_points; $i++) {
474 $params[$index]['MULTIPOLYGON'][$k][0][$i]['x'] = $points_arr[$i][0];
475 $params[$index]['MULTIPOLYGON'][$k][0][$i]['y'] = $points_arr[$i][1];
477 } else {
478 // Seperate outer and inner polygons
479 $parts = explode("),(", $polygon);
480 $params[$index]['MULTIPOLYGON'][$k]['no_of_lines'] = count($parts);
481 $j = 0;
482 foreach ($parts as $ring) {
483 $points_arr = $this->extractPoints($ring, null);
484 $no_of_points = count($points_arr);
485 $params[$index]['MULTIPOLYGON'][$k][$j]['no_of_points'] = $no_of_points;
486 for ($i = 0; $i < $no_of_points; $i++) {
487 $params[$index]['MULTIPOLYGON'][$k][$j][$i]['x'] = $points_arr[$i][0];
488 $params[$index]['MULTIPOLYGON'][$k][$j][$i]['y'] = $points_arr[$i][1];
490 $j++;
493 $k++;
495 return $params;