Upgraded phpmyadmin to 4.0.4 (All Languages) - No modifications yet
[openemr.git] / phpmyadmin / libraries / gis / pma_gis_geometry.php
blob206d7c459461a3bdcf4bb368dd358ed4ab6dfe17
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 object $pdf TCPDF instance
58 * @return object 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 cordinates
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 // Seperate each point
139 $points = explode(",", $point_set);
141 foreach ($points as $point) {
142 // Extract cordinates 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 = '';
179 if (preg_match("/^'" . $geom_types . "\(.*\)',[0-9]*$/i", $value)) {
180 $last_comma = strripos($value, ",");
181 $srid = trim(substr($value, $last_comma + 1));
182 $wkt = trim(substr($value, 1, $last_comma - 2));
183 } elseif (preg_match("/^" . $geom_types . "\(.*\)$/i", $value)) {
184 $wkt = $value;
186 return array('srid' => $srid, 'wkt' => $wkt);
190 * Extracts points, scales and returns them as an array.
192 * @param string $point_set string of comma sperated points
193 * @param array $scale_data data related to scaling
194 * @param boolean $linear if true, as a 1D array, else as a 2D array
196 * @return array scaled points
197 * @access protected
199 protected function extractPoints($point_set, $scale_data, $linear = false)
201 $points_arr = array();
203 // Seperate each point
204 $points = explode(",", $point_set);
206 foreach ($points as $point) {
207 // Extract cordinates of the point
208 $cordinates = explode(" ", $point);
210 if (isset($cordinates[0]) && trim($cordinates[0]) != ''
211 && isset($cordinates[1]) && trim($cordinates[1]) != ''
213 if ($scale_data != null) {
214 $x = ($cordinates[0] - $scale_data['x']) * $scale_data['scale'];
215 $y = $scale_data['height']
216 - ($cordinates[1] - $scale_data['y']) * $scale_data['scale'];
217 } else {
218 $x = trim($cordinates[0]);
219 $y = trim($cordinates[1]);
221 } else {
222 $x = '';
223 $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) . ', ';
254 $ol_array = substr($ol_array, 0, strlen($ol_array) - 2);
255 $ol_array .= ')';
257 return $ol_array;
261 * Generates JavaScript for adding points for OpenLayers polygon.
263 * @param array $polygon x and y coordinates for each line
264 * @param string $srid spatial reference id
266 * @return string JavaScript for adding points for OpenLayers polygon
267 * @access protected
269 protected function getPolygonForOpenLayers($polygon, $srid)
271 return 'new OpenLayers.Geometry.Polygon('
272 . $this->getLineArrayForOpenLayers($polygon, $srid, false)
273 . ')';
277 * Generates JavaScript for adding an array of LineString
278 * or LineRing to OpenLayers.
280 * @param array $lines x and y coordinates for each line
281 * @param string $srid spatial reference id
282 * @param bool $is_line_string whether it's an array of LineString
284 * @return string JavaScript for adding an array of LineString
285 * or LineRing to OpenLayers
286 * @access protected
288 protected function getLineArrayForOpenLayers($lines, $srid,
289 $is_line_string = true
291 $ol_array = 'new Array(';
292 foreach ($lines as $line) {
293 $points_arr = $this->extractPoints($line, null);
294 $ol_array .= $this->getLineForOpenLayers(
295 $points_arr, $srid, $is_line_string
297 $ol_array .= ', ';
299 $ol_array = substr($ol_array, 0, strlen($ol_array) - 2);
300 $ol_array .= ')';
302 return $ol_array;
306 * Generates JavaScript for adding a LineString or LineRing to OpenLayers.
308 * @param array $points_arr x and y coordinates for each point
309 * @param string $srid spatial reference id
310 * @param bool $is_line_string whether it's a LineString
312 * @return string JavaScript for adding a LineString or LineRing to OpenLayers
313 * @access protected
315 protected function getLineForOpenLayers($points_arr, $srid,
316 $is_line_string = true
318 return 'new OpenLayers.Geometry.'
319 . ($is_line_string ? 'LineString' : 'LinearRing') . '('
320 . $this->getPointsArrayForOpenLayers($points_arr, $srid)
321 . ')';
325 * Generates JavaScript for adding an array of points to OpenLayers.
327 * @param array $points_arr x and y coordinates for each point
328 * @param string $srid spatial reference id
330 * @return string JavaScript for adding an array of points to OpenLayers
331 * @access protected
333 protected function getPointsArrayForOpenLayers($points_arr, $srid)
335 $ol_array = 'new Array(';
336 foreach ($points_arr as $point) {
337 $ol_array .= $this->getPointForOpenLayers($point, $srid) . ', ';
339 $ol_array = substr($ol_array, 0, strlen($ol_array) - 2);
340 $ol_array .= ')';
342 return $ol_array;
346 * Generates JavaScript for adding a point to OpenLayers.
348 * @param array $point array containing the x and y coordinates of the point
349 * @param string $srid spatial reference id
351 * @return string JavaScript for adding points to OpenLayers
352 * @access protected
354 protected function getPointForOpenLayers($point, $srid)
356 return '(new OpenLayers.Geometry.Point(' . $point[0] . ',' . $point[1] . '))'
357 . '.transform(new OpenLayers.Projection("EPSG:'
358 . $srid . '"), map.getProjectionObject())';