Advisor: mark that 'Rate of reading fixed position' may be wrong, requires further...
[phpmyadmin/thilanka.git] / libraries / gis / pma_gis_geometry.php
blobc49c1a1eb8c153615e3ccd77ae57d195ff17c036
1 <?php
2 /**
3 * Base class for all GIS data type classes.
5 * @package phpMyAdmin-GIS
6 */
7 abstract class PMA_GIS_Geometry
9 /**
10 * Prepares and returns the code related to a row in the GIS dataset as SVG.
12 * @param string $spatial GIS data object
13 * @param string $label Label for the GIS data object
14 * @param string $color Color for the GIS data object
15 * @param array $scale_data Data related to scaling
17 * @return the code related to a row in the GIS dataset
19 public abstract function prepareRowAsSvg($spatial, $label, $color, $scale_data);
21 /**
22 * Adds to the PNG image object, the data related to a row in the GIS dataset.
24 * @param string $spatial GIS data object
25 * @param string $label Label for the GIS data object
26 * @param string $color Color for the GIS data object
27 * @param array $scale_data Array containing data related to scaling
28 * @param image $image Image object
30 * @return the modified image object
32 public abstract function prepareRowAsPng($spatial, $label, $color, $scale_data, $image);
34 /**
35 * Adds to the TCPDF instance, the data related to a row in the GIS dataset.
37 * @param string $spatial GIS data object
38 * @param string $label Label for the GIS data object
39 * @param string $color Color for the GIS data object
40 * @param array $scale_data Array containing data related to scaling
41 * @param image $pdf TCPDF instance
43 * @return the modified TCPDF instance
45 public abstract function prepareRowAsPdf($spatial, $label, $color, $scale_data, $pdf);
47 /**
48 * Prepares the JavaScript related to a row in the GIS dataset
49 * to visualize it with OpenLayers.
51 * @param string $spatial GIS data object
52 * @param int $srid Spatial reference ID
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
57 * @return the JavaScript related to a row in the GIS dataset
59 public abstract function prepareRowAsOl($spatial, $srid, $label, $color, $scale_data);
61 /**
62 * Scales each row.
64 * @param string $spatial spatial data of a row
66 * @return array containing the min, max values for x and y cordinates
68 public abstract function scaleRow($spatial);
70 /**
71 * Generate the WKT with the set of parameters passed by the GIS editor.
73 * @param array $gis_data GIS data
74 * @param int $index Index into the parameter object
75 * @param string $empty Value for empty points
77 * @return WKT with the set of parameters passed by the GIS editor
79 public abstract function generateWkt($gis_data, $index, $empty);
81 /**
82 * Returns OpenLayers.Bounds object that correspond to the bounds of GIS data.
84 * @param string $srid Spatial reference ID
85 * @param array $scale_data Data related to scaling
87 * @return OpenLayers.Bounds object that correspond to the bounds of GIS data
89 protected function getBoundsForOl($srid, $scale_data)
91 return 'bound = new OpenLayers.Bounds(); bound.extend(new OpenLayers.LonLat('
92 . $scale_data['minX'] . ', ' . $scale_data['minY']
93 . ').transform(new OpenLayers.Projection("EPSG:'
94 . $srid . '"), map.getProjectionObject())); bound.extend(new OpenLayers.LonLat('
95 . $scale_data['maxX'] . ', ' . $scale_data['maxY']
96 . ').transform(new OpenLayers.Projection("EPSG:'
97 . $srid . '"), map.getProjectionObject()));';
101 * Update the min, max values with the given point set.
103 * @param string $point_set Point set
104 * @param array $min_max Existing min, max values
106 * @return the updated min, max values
108 protected function setMinMax($point_set, $min_max)
110 // Seperate each point
111 $points = explode(",", $point_set);
113 foreach ($points as $point) {
114 // Extract cordinates of the point
115 $cordinates = explode(" ", $point);
117 $x = (float) $cordinates[0];
118 if (! isset($min_max['maxX']) || $x > $min_max['maxX']) {
119 $min_max['maxX'] = $x;
121 if (! isset($min_max['minX']) || $x < $min_max['minX']) {
122 $min_max['minX'] = $x;
124 $y = (float) $cordinates[1];
125 if (! isset($min_max['maxY']) || $y > $min_max['maxY']) {
126 $min_max['maxY'] = $y;
128 if (! isset($min_max['minY']) || $y < $min_max['minY']) {
129 $min_max['minY'] = $y;
132 return $min_max;
136 * Generate parameters for the GIS data editor from the value of the GIS column.
137 * This method performs common work.
138 * More specific work is performed by each of the geom classes.
140 * @param $gis_string $value of the GIS column
142 * @return array parameters for the GIS editor from the value of the GIS column
144 protected function generateParams($value)
146 $geom_types = '(POINT|MULTIPOINT|LINESTRING|MULTILINESTRING|POLYGON|MULTIPOLYGON|GEOMETRYCOLLECTION)';
147 $srid = 0;
148 $wkt = '';
149 if (preg_match("/^'" . $geom_types . "\(.*\)',[0-9]*$/i", $value)) {
150 $last_comma = strripos($value, ",");
151 $srid = trim(substr($value, $last_comma + 1));
152 $wkt = trim(substr($value, 1, $last_comma - 2));
153 } elseif (preg_match("/^" . $geom_types . "\(.*\)$/i", $value)) {
154 $wkt = $value;
156 return array('srid' => $srid, 'wkt' => $wkt);
160 * Extracts points, scales and returns them as an array.
162 * @param string $point_set String of comma sperated points
163 * @param array $scale_data Data related to scaling
164 * @param boolean $linear If true, as a 1D array, else as a 2D array
166 * @return scaled points
168 protected function extractPoints($point_set, $scale_data, $linear = false)
170 $points_arr = array();
172 // Seperate each point
173 $points = explode(",", $point_set);
175 foreach ($points as $point) {
176 // Extract cordinates of the point
177 $cordinates = explode(" ", $point);
179 if (isset($cordinates[0]) && trim($cordinates[0]) != ''
180 && isset($cordinates[1]) && trim($cordinates[1]) != ''
182 if ($scale_data != null) {
183 $x = ($cordinates[0] - $scale_data['x']) * $scale_data['scale'];
184 $y = $scale_data['height'] - ($cordinates[1] - $scale_data['y']) * $scale_data['scale'];
185 } else {
186 $x = trim($cordinates[0]);
187 $y = trim($cordinates[1]);
189 } else {
190 $x = '';
191 $y = '';
195 if (! $linear) {
196 $points_arr[] = array($x, $y);
197 } else {
198 $points_arr[] = $x;
199 $points_arr[] = $y;
203 return $points_arr;
207 * Generates JavaScriipt for adding points for OpenLayers polygon.
209 * @param string $polygon points of a polygon in WKT form
210 * @param string $srid spatial reference id
212 * @return JavaScriipt for adding points for OpenLayers polygon
214 protected function addPointsForOpenLayersPolygon($polygon, $srid)
216 $row = 'new OpenLayers.Geometry.Polygon(new Array(';
217 // If the polygon doesnt have an inner polygon
218 if (strpos($polygon, "),(") === false) {
219 $points_arr = $this->extractPoints($polygon, null);
220 $row .= 'new OpenLayers.Geometry.LinearRing(new Array(';
221 foreach ($points_arr as $point) {
222 $row .= '(new OpenLayers.Geometry.Point('
223 . $point[0] . ', ' . $point[1] . '))'
224 . '.transform(new OpenLayers.Projection("EPSG:'
225 . $srid . '"), map.getProjectionObject()), ';
227 $row = substr($row, 0, strlen($row) - 2);
228 $row .= '))';
229 } else {
230 // Seperate outer and inner polygons
231 $parts = explode("),(", $polygon);
232 foreach ($parts as $ring) {
233 $points_arr = $this->extractPoints($ring, null);
234 $row .= 'new OpenLayers.Geometry.LinearRing(new Array(';
235 foreach ($points_arr as $point) {
236 $row .= '(new OpenLayers.Geometry.Point('
237 . $point[0] . ', ' . $point[1] . '))'
238 . '.transform(new OpenLayers.Projection("EPSG:'
239 . $srid . '"), map.getProjectionObject()), ';
241 $row = substr($row, 0, strlen($row) - 2);
242 $row .= ')), ';
244 $row = substr($row, 0, strlen($row) - 2);
246 $row .= ')), ';
247 return $row;