Upgraded phpmyadmin to 4.0.4 (All Languages) - No modifications yet
[openemr.git] / phpmyadmin / libraries / gis / pma_gis_multipolygon.php
bloba8c8241524c07cc8f48e768a7e3c0cd73272602c
1 <?php
2 /* vim: set expandtab sw=4 ts=4 sts=4: */
3 /**
4 * Handles actions related to GIS MULTIPOLYGON objects
6 * @package PhpMyAdmin-GIS
7 */
9 if (! defined('PHPMYADMIN')) {
10 exit;
13 /**
14 * Handles actions related to GIS MULTIPOLYGON objects
16 * @package PhpMyAdmin-GIS
18 class PMA_GIS_Multipolygon extends PMA_GIS_Geometry
20 // Hold the singleton instance of the class
21 private static $_instance;
23 /**
24 * A private constructor; prevents direct creation of object.
26 * @access private
28 private function __construct()
32 /**
33 * Returns the singleton.
35 * @return object the singleton
36 * @access public
38 public static function singleton()
40 if (!isset(self::$_instance)) {
41 $class = __CLASS__;
42 self::$_instance = new $class;
45 return self::$_instance;
48 /**
49 * Scales each row.
51 * @param string $spatial spatial data of a row
53 * @return array an array containing the min, max values for x and y cordinates
54 * @access public
56 public function scaleRow($spatial)
58 $min_max = array();
60 // Trim to remove leading 'MULTIPOLYGON(((' and trailing ')))'
61 $multipolygon = substr($spatial, 15, (strlen($spatial) - 18));
62 // Seperate each polygon
63 $polygons = explode(")),((", $multipolygon);
65 foreach ($polygons as $polygon) {
66 // If the polygon doesn't have an inner ring, use polygon itself
67 if (strpos($polygon, "),(") === false) {
68 $ring = $polygon;
69 } else {
70 // Seperate outer ring and use it to determin min-max
71 $parts = explode("),(", $polygon);
72 $ring = $parts[0];
74 $min_max = $this->setMinMax($ring, $min_max);
77 return $min_max;
80 /**
81 * Adds to the PNG image object, the data related to a row in the GIS dataset.
83 * @param string $spatial GIS MULTIPOLYGON object
84 * @param string $label Label for the GIS MULTIPOLYGON object
85 * @param string $fill_color Color for the GIS MULTIPOLYGON object
86 * @param array $scale_data Array containing data related to scaling
87 * @param object $image Image object
89 * @return object the modified image object
90 * @access public
92 public function prepareRowAsPng($spatial, $label, $fill_color,
93 $scale_data, $image
94 ) {
95 // allocate colors
96 $black = imagecolorallocate($image, 0, 0, 0);
97 $red = hexdec(substr($fill_color, 1, 2));
98 $green = hexdec(substr($fill_color, 3, 2));
99 $blue = hexdec(substr($fill_color, 4, 2));
100 $color = imagecolorallocate($image, $red, $green, $blue);
102 // Trim to remove leading 'MULTIPOLYGON(((' and trailing ')))'
103 $multipolygon = substr($spatial, 15, (strlen($spatial) - 18));
104 // Seperate each polygon
105 $polygons = explode(")),((", $multipolygon);
107 $first_poly = true;
108 foreach ($polygons as $polygon) {
109 // If the polygon doesnt have an inner polygon
110 if (strpos($polygon, "),(") === false) {
111 $points_arr = $this->extractPoints($polygon, $scale_data, true);
112 } else {
113 // Seperate outer and inner polygons
114 $parts = explode("),(", $polygon);
115 $outer = $parts[0];
116 $inner = array_slice($parts, 1);
118 $points_arr = $this->extractPoints($outer, $scale_data, true);
120 foreach ($inner as $inner_poly) {
121 $points_arr = array_merge(
122 $points_arr,
123 $this->extractPoints($inner_poly, $scale_data, true)
127 // draw polygon
128 imagefilledpolygon($image, $points_arr, sizeof($points_arr) / 2, $color);
129 // mark label point if applicable
130 if (isset($label) && trim($label) != '' && $first_poly) {
131 $label_point = array($points_arr[2], $points_arr[3]);
133 $first_poly = false;
135 // print label if applicable
136 if (isset($label_point)) {
137 imagestring(
138 $image, 1, $points_arr[2], $points_arr[3], trim($label), $black
141 return $image;
145 * Adds to the TCPDF instance, the data related to a row in the GIS dataset.
147 * @param string $spatial GIS MULTIPOLYGON object
148 * @param string $label Label for the GIS MULTIPOLYGON object
149 * @param string $fill_color Color for the GIS MULTIPOLYGON object
150 * @param array $scale_data Array containing data related to scaling
151 * @param object $pdf TCPDF instance
153 * @return object the modified TCPDF instance
154 * @access public
156 public function prepareRowAsPdf($spatial, $label, $fill_color, $scale_data, $pdf)
158 // allocate colors
159 $red = hexdec(substr($fill_color, 1, 2));
160 $green = hexdec(substr($fill_color, 3, 2));
161 $blue = hexdec(substr($fill_color, 4, 2));
162 $color = array($red, $green, $blue);
164 // Trim to remove leading 'MULTIPOLYGON(((' and trailing ')))'
165 $multipolygon = substr($spatial, 15, (strlen($spatial) - 18));
166 // Seperate each polygon
167 $polygons = explode(")),((", $multipolygon);
169 $first_poly = true;
170 foreach ($polygons as $polygon) {
171 // If the polygon doesnt have an inner polygon
172 if (strpos($polygon, "),(") === false) {
173 $points_arr = $this->extractPoints($polygon, $scale_data, true);
174 } else {
175 // Seperate outer and inner polygons
176 $parts = explode("),(", $polygon);
177 $outer = $parts[0];
178 $inner = array_slice($parts, 1);
180 $points_arr = $this->extractPoints($outer, $scale_data, true);
182 foreach ($inner as $inner_poly) {
183 $points_arr = array_merge(
184 $points_arr,
185 $this->extractPoints($inner_poly, $scale_data, true)
189 // draw polygon
190 $pdf->Polygon($points_arr, 'F*', array(), $color, true);
191 // mark label point if applicable
192 if (isset($label) && trim($label) != '' && $first_poly) {
193 $label_point = array($points_arr[2], $points_arr[3]);
195 $first_poly = false;
198 // print label if applicable
199 if (isset($label_point)) {
200 $pdf->SetXY($label_point[0], $label_point[1]);
201 $pdf->SetFontSize(5);
202 $pdf->Cell(0, 0, trim($label));
204 return $pdf;
208 * Prepares and returns the code related to a row in the GIS dataset as SVG.
210 * @param string $spatial GIS MULTIPOLYGON object
211 * @param string $label Label for the GIS MULTIPOLYGON object
212 * @param string $fill_color Color for the GIS MULTIPOLYGON object
213 * @param array $scale_data Array containing data related to scaling
215 * @return string the code related to a row in the GIS dataset
216 * @access public
218 public function prepareRowAsSvg($spatial, $label, $fill_color, $scale_data)
220 $polygon_options = array(
221 'name' => $label,
222 'class' => 'multipolygon vector',
223 'stroke' => 'black',
224 'stroke-width'=> 0.5,
225 'fill' => $fill_color,
226 'fill-rule' => 'evenodd',
227 'fill-opacity'=> 0.8,
230 $row = '';
232 // Trim to remove leading 'MULTIPOLYGON(((' and trailing ')))'
233 $multipolygon = substr($spatial, 15, (strlen($spatial) - 18));
234 // Seperate each polygon
235 $polygons = explode(")),((", $multipolygon);
237 foreach ($polygons as $polygon) {
238 $row .= '<path d="';
240 // If the polygon doesnt have an inner polygon
241 if (strpos($polygon, "),(") === false) {
242 $row .= $this->_drawPath($polygon, $scale_data);
243 } else {
244 // Seperate outer and inner polygons
245 $parts = explode("),(", $polygon);
246 $outer = $parts[0];
247 $inner = array_slice($parts, 1);
249 $row .= $this->_drawPath($outer, $scale_data);
251 foreach ($inner as $inner_poly) {
252 $row .= $this->_drawPath($inner_poly, $scale_data);
255 $polygon_options['id'] = $label . rand();
256 $row .= '"';
257 foreach ($polygon_options as $option => $val) {
258 $row .= ' ' . $option . '="' . trim($val) . '"';
260 $row .= '/>';
263 return $row;
267 * Prepares JavaScript related to a row in the GIS dataset
268 * to visualize it with OpenLayers.
270 * @param string $spatial GIS MULTIPOLYGON object
271 * @param int $srid Spatial reference ID
272 * @param string $label Label for the GIS MULTIPOLYGON object
273 * @param string $fill_color Color for the GIS MULTIPOLYGON object
274 * @param array $scale_data Array containing data related to scaling
276 * @return string JavaScript related to a row in the GIS dataset
277 * @access public
279 public function prepareRowAsOl($spatial, $srid, $label, $fill_color, $scale_data)
281 $style_options = array(
282 'strokeColor' => '#000000',
283 'strokeWidth' => 0.5,
284 'fillColor' => $fill_color,
285 'fillOpacity' => 0.8,
286 'label' => $label,
287 'fontSize' => 10,
289 if ($srid == 0) {
290 $srid = 4326;
292 $row = $this->getBoundsForOl($srid, $scale_data);
294 // Trim to remove leading 'MULTIPOLYGON(((' and trailing ')))'
295 $multipolygon = substr($spatial, 15, (strlen($spatial) - 18));
296 // Seperate each polygon
297 $polygons = explode(")),((", $multipolygon);
299 $row .= 'vectorLayer.addFeatures(new OpenLayers.Feature.Vector('
300 . 'new OpenLayers.Geometry.MultiPolygon('
301 . $this->getPolygonArrayForOpenLayers($polygons, $srid)
302 . '), null, ' . json_encode($style_options) . '));';
303 return $row;
307 * Draws a ring of the polygon using SVG path element.
309 * @param string $polygon The ring
310 * @param array $scale_data Array containing data related to scaling
312 * @return string the code to draw the ring
313 * @access private
315 private function _drawPath($polygon, $scale_data)
317 $points_arr = $this->extractPoints($polygon, $scale_data);
319 $row = ' M ' . $points_arr[0][0] . ', ' . $points_arr[0][1];
320 $other_points = array_slice($points_arr, 1, count($points_arr) - 2);
321 foreach ($other_points as $point) {
322 $row .= ' L ' . $point[0] . ', ' . $point[1];
324 $row .= ' Z ';
326 return $row;
330 * Generate the WKT with the set of parameters passed by the GIS editor.
332 * @param array $gis_data GIS data
333 * @param int $index Index into the parameter object
334 * @param string $empty Value for empty points
336 * @return string WKT with the set of parameters passed by the GIS editor
337 * @access public
339 public function generateWkt($gis_data, $index, $empty = '')
341 $data_row = $gis_data[$index]['MULTIPOLYGON'];
343 $no_of_polygons = isset($data_row['no_of_polygons'])
344 ? $data_row['no_of_polygons'] : 1;
345 if ($no_of_polygons < 1) {
346 $no_of_polygons = 1;
349 $wkt = 'MULTIPOLYGON(';
350 for ($k = 0; $k < $no_of_polygons; $k++) {
351 $no_of_lines = isset($data_row[$k]['no_of_lines'])
352 ? $data_row[$k]['no_of_lines'] : 1;
353 if ($no_of_lines < 1) {
354 $no_of_lines = 1;
356 $wkt .= '(';
357 for ($i = 0; $i < $no_of_lines; $i++) {
358 $no_of_points = isset($data_row[$k][$i]['no_of_points'])
359 ? $data_row[$k][$i]['no_of_points'] : 4;
360 if ($no_of_points < 4) {
361 $no_of_points = 4;
363 $wkt .= '(';
364 for ($j = 0; $j < $no_of_points; $j++) {
365 $wkt .= ((isset($data_row[$k][$i][$j]['x'])
366 && trim($data_row[$k][$i][$j]['x']) != '')
367 ? $data_row[$k][$i][$j]['x'] : $empty)
368 . ' ' . ((isset($data_row[$k][$i][$j]['y'])
369 && trim($data_row[$k][$i][$j]['y']) != '')
370 ? $data_row[$k][$i][$j]['y'] : $empty) .',';
372 $wkt = substr($wkt, 0, strlen($wkt) - 1);
373 $wkt .= '),';
375 $wkt = substr($wkt, 0, strlen($wkt) - 1);
376 $wkt .= '),';
378 $wkt = substr($wkt, 0, strlen($wkt) - 1);
379 $wkt .= ')';
380 return $wkt;
384 * Generate the WKT for the data from ESRI shape files.
386 * @param array $row_data GIS data
388 * @return string the WKT for the data from ESRI shape files
389 * @access public
391 public function getShape($row_data)
393 // Determines whether each line ring is an inner ring or an outer ring.
394 // If it's an inner ring get a point on the surface which can be used to
395 // correctly classify inner rings to their respective outer rings.
396 include_once './libraries/gis/pma_gis_polygon.php';
397 foreach ($row_data['parts'] as $i => $ring) {
398 $row_data['parts'][$i]['isOuter']
399 = PMA_GIS_Polygon::isOuterRing($ring['points']);
402 // Find points on surface for inner rings
403 foreach ($row_data['parts'] as $i => $ring) {
404 if (! $ring['isOuter']) {
405 $row_data['parts'][$i]['pointOnSurface']
406 = PMA_GIS_Polygon::getPointOnSurface($ring['points']);
410 // Classify inner rings to their respective outer rings.
411 foreach ($row_data['parts'] as $j => $ring1) {
412 if (! $ring1['isOuter']) {
413 foreach ($row_data['parts'] as $k => $ring2) {
414 if ($ring2['isOuter']) {
415 // If the pointOnSurface of the inner ring
416 // is also inside the outer ring
417 if (PMA_GIS_Polygon::isPointInsidePolygon(
418 $ring1['pointOnSurface'], $ring2['points']
419 )) {
420 if (! isset($ring2['inner'])) {
421 $row_data['parts'][$k]['inner'] = array();
423 $row_data['parts'][$k]['inner'][] = $j;
430 $wkt = 'MULTIPOLYGON(';
431 // for each polygon
432 foreach ($row_data['parts'] as $ring) {
433 if ($ring['isOuter']) {
434 $wkt .= '('; // start of polygon
436 $wkt .= '('; // start of outer ring
437 foreach ($ring['points'] as $point) {
438 $wkt .= $point['x'] . ' ' . $point['y'] . ',';
440 $wkt = substr($wkt, 0, strlen($wkt) - 1);
441 $wkt .= ')'; // end of outer ring
443 // inner rings if any
444 if (isset($ring['inner'])) {
445 foreach ($ring['inner'] as $j) {
446 $wkt .= ',('; // start of inner ring
447 foreach ($row_data['parts'][$j]['points'] as $innerPoint) {
448 $wkt .= $innerPoint['x'] . ' ' . $innerPoint['y'] . ',';
450 $wkt = substr($wkt, 0, strlen($wkt) - 1);
451 $wkt .= ')'; // end of inner ring
455 $wkt .= '),'; // end of polygon
458 $wkt = substr($wkt, 0, strlen($wkt) - 1);
460 $wkt .= ')'; // end of multipolygon
461 return $wkt;
465 * Generate parameters for the GIS data editor from the value of the GIS column.
467 * @param string $value of the GIS column
468 * @param index $index of the geometry
470 * @return array params for the GIS data editor from the value of the GIS column
471 * @access public
473 public function generateParams($value, $index = -1)
475 if ($index == -1) {
476 $index = 0;
477 $params = array();
478 $data = PMA_GIS_Geometry::generateParams($value);
479 $params['srid'] = $data['srid'];
480 $wkt = $data['wkt'];
481 } else {
482 $params[$index]['gis_type'] = 'MULTIPOLYGON';
483 $wkt = $value;
486 // Trim to remove leading 'MULTIPOLYGON(((' and trailing ')))'
487 $multipolygon = substr($wkt, 15, (strlen($wkt) - 18));
488 // Seperate each polygon
489 $polygons = explode(")),((", $multipolygon);
491 $param_row =& $params[$index]['MULTIPOLYGON'];
492 $param_row['no_of_polygons'] = count($polygons);
494 $k = 0;
495 foreach ($polygons as $polygon) {
496 // If the polygon doesnt have an inner polygon
497 if (strpos($polygon, "),(") === false) {
498 $param_row[$k]['no_of_lines'] = 1;
499 $points_arr = $this->extractPoints($polygon, null);
500 $no_of_points = count($points_arr);
501 $param_row[$k][0]['no_of_points'] = $no_of_points;
502 for ($i = 0; $i < $no_of_points; $i++) {
503 $param_row[$k][0][$i]['x'] = $points_arr[$i][0];
504 $param_row[$k][0][$i]['y'] = $points_arr[$i][1];
506 } else {
507 // Seperate outer and inner polygons
508 $parts = explode("),(", $polygon);
509 $param_row[$k]['no_of_lines'] = count($parts);
510 $j = 0;
511 foreach ($parts as $ring) {
512 $points_arr = $this->extractPoints($ring, null);
513 $no_of_points = count($points_arr);
514 $param_row[$k][$j]['no_of_points'] = $no_of_points;
515 for ($i = 0; $i < $no_of_points; $i++) {
516 $param_row[$k][$j][$i]['x'] = $points_arr[$i][0];
517 $param_row[$k][$j][$i]['y'] = $points_arr[$i][1];
519 $j++;
522 $k++;
524 return $params;