1. Check existence of mb_string, mysql and xml extensions before installation.
[openemr.git] / phpmyadmin / libraries / gis / GIS_Geometry.class.php
blob8645465c72a36dcca460ceea8ec87215510e6f5b
1 <?php
2 /* vim: set expandtab sw=4 ts=4 sts=4: */
3 /**
4 * Base class for all GIS data type classes
6 * @package PhpMyAdmin-GIS
7 */
9 if (! defined('PHPMYADMIN')) {
10 exit;
13 /**
14 * Base class for all GIS data type classes.
16 * @package PhpMyAdmin-GIS
18 abstract class PMA_GIS_Geometry
20 /**
21 * Prepares and returns the code related to a row in the GIS dataset as SVG.
23 * @param string $spatial GIS data object
24 * @param string $label label for the GIS data object
25 * @param string $color color for the GIS data object
26 * @param array $scale_data data related to scaling
28 * @return string the code related to a row in the GIS dataset
29 * @access public
31 public abstract function prepareRowAsSvg($spatial, $label, $color, $scale_data);
33 /**
34 * Adds to the PNG image object, the data related to a row in the GIS dataset.
36 * @param string $spatial GIS data object
37 * @param string $label label for the GIS data object
38 * @param string $color color for the GIS data object
39 * @param array $scale_data array containing data related to scaling
40 * @param object $image image object
42 * @return object the modified image object
43 * @access public
45 public abstract function prepareRowAsPng($spatial, $label, $color,
46 $scale_data, $image
49 /**
50 * Adds to the TCPDF instance, the data related to a row in the GIS dataset.
52 * @param string $spatial GIS data object
53 * @param string $label label for the GIS data object
54 * @param string $color color for the GIS data object
55 * @param array $scale_data array containing data related to scaling
56 * @param TCPDF $pdf TCPDF instance
58 * @return TCPDF the modified TCPDF instance
59 * @access public
61 public abstract function prepareRowAsPdf($spatial, $label, $color,
62 $scale_data, $pdf
65 /**
66 * Prepares the JavaScript related to a row in the GIS dataset
67 * to visualize it with OpenLayers.
69 * @param string $spatial GIS data object
70 * @param int $srid spatial reference ID
71 * @param string $label label for the GIS data object
72 * @param string $color color for the GIS data object
73 * @param array $scale_data array containing data related to scaling
75 * @return string the JavaScript related to a row in the GIS dataset
76 * @access public
78 public abstract function prepareRowAsOl($spatial, $srid, $label,
79 $color, $scale_data
82 /**
83 * Scales each row.
85 * @param string $spatial spatial data of a row
87 * @return array array containing the min, max values for x and y coordinates
88 * @access public
90 public abstract function scaleRow($spatial);
92 /**
93 * Generates the WKT with the set of parameters passed by the GIS editor.
95 * @param array $gis_data GIS data
96 * @param int $index index into the parameter object
97 * @param string $empty value for empty points
99 * @return string WKT with the set of parameters passed by the GIS editor
100 * @access public
102 public abstract function generateWkt($gis_data, $index, $empty = '');
105 * Returns OpenLayers.Bounds object that correspond to the bounds of GIS data.
107 * @param string $srid spatial reference ID
108 * @param array $scale_data data related to scaling
110 * @return string OpenLayers.Bounds object that
111 * correspond to the bounds of GIS data
112 * @access protected
114 protected function getBoundsForOl($srid, $scale_data)
116 return 'bound = new OpenLayers.Bounds(); '
117 . 'bound.extend(new OpenLayers.LonLat('
118 . $scale_data['minX'] . ', ' . $scale_data['minY']
119 . ').transform(new OpenLayers.Projection("EPSG:'
120 . $srid . '"), map.getProjectionObject())); '
121 . 'bound.extend(new OpenLayers.LonLat('
122 . $scale_data['maxX'] . ', ' . $scale_data['maxY']
123 . ').transform(new OpenLayers.Projection("EPSG:'
124 . $srid . '"), map.getProjectionObject()));';
128 * Updates the min, max values with the given point set.
130 * @param string $point_set point set
131 * @param array $min_max existing min, max values
133 * @return array the updated min, max values
134 * @access protected
136 protected function setMinMax($point_set, $min_max)
138 // Separate each point
139 $points = explode(",", $point_set);
141 foreach ($points as $point) {
142 // Extract coordinates of the point
143 $cordinates = explode(" ", $point);
145 $x = (float) $cordinates[0];
146 if (! isset($min_max['maxX']) || $x > $min_max['maxX']) {
147 $min_max['maxX'] = $x;
149 if (! isset($min_max['minX']) || $x < $min_max['minX']) {
150 $min_max['minX'] = $x;
152 $y = (float) $cordinates[1];
153 if (! isset($min_max['maxY']) || $y > $min_max['maxY']) {
154 $min_max['maxY'] = $y;
156 if (! isset($min_max['minY']) || $y < $min_max['minY']) {
157 $min_max['minY'] = $y;
160 return $min_max;
164 * Generates parameters for the GIS data editor from the value of the GIS column.
165 * This method performs common work.
166 * More specific work is performed by each of the geom classes.
168 * @param string $value value of the GIS column
170 * @return array parameters for the GIS editor from the value of the GIS column
171 * @access protected
173 protected function generateParams($value)
175 $geom_types = '(POINT|MULTIPOINT|LINESTRING|MULTILINESTRING'
176 . '|POLYGON|MULTIPOLYGON|GEOMETRYCOLLECTION)';
177 $srid = 0;
178 $wkt = '';
180 if (preg_match("/^'" . $geom_types . "\(.*\)',[0-9]*$/i", $value)) {
181 $last_comma = /*overload*/mb_strripos($value, ",");
182 $srid = trim(/*overload*/mb_substr($value, $last_comma + 1));
183 $wkt = trim(/*overload*/mb_substr($value, 1, $last_comma - 2));
184 } elseif (preg_match("/^" . $geom_types . "\(.*\)$/i", $value)) {
185 $wkt = $value;
187 return array('srid' => $srid, 'wkt' => $wkt);
191 * Extracts points, scales and returns them as an array.
193 * @param string $point_set string of comma separated points
194 * @param array $scale_data data related to scaling
195 * @param boolean $linear if true, as a 1D array, else as a 2D array
197 * @return array scaled points
198 * @access protected
200 protected function extractPoints($point_set, $scale_data, $linear = false)
202 $points_arr = array();
204 // Separate each point
205 $points = explode(",", $point_set);
207 foreach ($points as $point) {
208 // Extract coordinates of the point
209 $cordinates = explode(" ", $point);
211 if (isset($cordinates[0]) && trim($cordinates[0]) != ''
212 && isset($cordinates[1]) && trim($cordinates[1]) != ''
214 if ($scale_data != null) {
215 $x = ($cordinates[0] - $scale_data['x']) * $scale_data['scale'];
216 $y = $scale_data['height']
217 - ($cordinates[1] - $scale_data['y']) * $scale_data['scale'];
218 } else {
219 $x = trim($cordinates[0]);
220 $y = trim($cordinates[1]);
222 } else {
223 $x = '';
224 $y = '';
227 if (! $linear) {
228 $points_arr[] = array($x, $y);
229 } else {
230 $points_arr[] = $x;
231 $points_arr[] = $y;
235 return $points_arr;
239 * Generates JavaScript for adding an array of polygons to OpenLayers.
241 * @param array $polygons x and y coordinates for each polygon
242 * @param string $srid spatial reference id
244 * @return string JavaScript for adding an array of polygons to OpenLayers
245 * @access protected
247 protected function getPolygonArrayForOpenLayers($polygons, $srid)
249 $ol_array = 'new Array(';
250 foreach ($polygons as $polygon) {
251 $rings = explode("),(", $polygon);
252 $ol_array .= $this->getPolygonForOpenLayers($rings, $srid) . ', ';
255 $ol_array = /*overload*/mb_substr(
256 $ol_array,
258 /*overload*/mb_strlen($ol_array) - 2
260 $ol_array .= ')';
262 return $ol_array;
266 * Generates JavaScript for adding points for OpenLayers polygon.
268 * @param array $polygon x and y coordinates for each line
269 * @param string $srid spatial reference id
271 * @return string JavaScript for adding points for OpenLayers polygon
272 * @access protected
274 protected function getPolygonForOpenLayers($polygon, $srid)
276 return 'new OpenLayers.Geometry.Polygon('
277 . $this->getLineArrayForOpenLayers($polygon, $srid, false)
278 . ')';
282 * Generates JavaScript for adding an array of LineString
283 * or LineRing to OpenLayers.
285 * @param array $lines x and y coordinates for each line
286 * @param string $srid spatial reference id
287 * @param bool $is_line_string whether it's an array of LineString
289 * @return string JavaScript for adding an array of LineString
290 * or LineRing to OpenLayers
291 * @access protected
293 protected function getLineArrayForOpenLayers($lines, $srid,
294 $is_line_string = true
296 $ol_array = 'new Array(';
297 foreach ($lines as $line) {
298 $points_arr = $this->extractPoints($line, null);
299 $ol_array .= $this->getLineForOpenLayers(
300 $points_arr, $srid, $is_line_string
302 $ol_array .= ', ';
305 $ol_array = /*overload*/mb_substr(
306 $ol_array,
308 /*overload*/mb_strlen($ol_array) - 2
310 $ol_array .= ')';
312 return $ol_array;
316 * Generates JavaScript for adding a LineString or LineRing to OpenLayers.
318 * @param array $points_arr x and y coordinates for each point
319 * @param string $srid spatial reference id
320 * @param bool $is_line_string whether it's a LineString
322 * @return string JavaScript for adding a LineString or LineRing to OpenLayers
323 * @access protected
325 protected function getLineForOpenLayers($points_arr, $srid,
326 $is_line_string = true
328 return 'new OpenLayers.Geometry.'
329 . ($is_line_string ? 'LineString' : 'LinearRing') . '('
330 . $this->getPointsArrayForOpenLayers($points_arr, $srid)
331 . ')';
335 * Generates JavaScript for adding an array of points to OpenLayers.
337 * @param array $points_arr x and y coordinates for each point
338 * @param string $srid spatial reference id
340 * @return string JavaScript for adding an array of points to OpenLayers
341 * @access protected
343 protected function getPointsArrayForOpenLayers($points_arr, $srid)
345 $ol_array = 'new Array(';
346 foreach ($points_arr as $point) {
347 $ol_array .= $this->getPointForOpenLayers($point, $srid) . ', ';
350 $ol_array = /*overload*/mb_substr(
351 $ol_array,
353 /*overload*/mb_strlen($ol_array) - 2
355 $ol_array .= ')';
357 return $ol_array;
361 * Generates JavaScript for adding a point to OpenLayers.
363 * @param array $point array containing the x and y coordinates of the point
364 * @param string $srid spatial reference id
366 * @return string JavaScript for adding points to OpenLayers
367 * @access protected
369 protected function getPointForOpenLayers($point, $srid)
371 return '(new OpenLayers.Geometry.Point(' . $point[0] . ',' . $point[1] . '))'
372 . '.transform(new OpenLayers.Projection("EPSG:'
373 . $srid . '"), map.getProjectionObject())';