Fix format strings
[phpmyadmin/madhuracj.git] / libraries / gis / pma_gis_linestring.php
blob7ce635a78ed8ba88b3e6726fdfce28c1bfa1b8f1
1 <?php
2 /**
3 * Handles the visualization of GIS LINESTRING objects.
5 * @package PhpMyAdmin-GIS
6 */
7 class PMA_GIS_Linestring 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 // Trim to remove leading 'LINESTRING(' and trailing ')'
44 $linesrting = substr($spatial, 11, (strlen($spatial) - 12));
45 return $this->setMinMax($linesrting, array());
48 /**
49 * Adds to the PNG image object, the data related to a row in the GIS dataset.
51 * @param string $spatial GIS LINESTRING object
52 * @param string $label Label for the GIS LINESTRING object
53 * @param string $line_color Color for the GIS LINESTRING object
54 * @param array $scale_data Array containing data related to scaling
55 * @param image $image Image object
57 * @return the modified image object
59 public function prepareRowAsPng($spatial, $label, $line_color, $scale_data, $image)
61 // allocate colors
62 $black = imagecolorallocate($image, 0, 0, 0);
63 $red = hexdec(substr($line_color, 1, 2));
64 $green = hexdec(substr($line_color, 3, 2));
65 $blue = hexdec(substr($line_color, 4, 2));
66 $color = imagecolorallocate($image, $red, $green, $blue);
68 // Trim to remove leading 'LINESTRING(' and trailing ')'
69 $linesrting = substr($spatial, 11, (strlen($spatial) - 12));
70 $points_arr = $this->extractPoints($linesrting, $scale_data);
72 foreach ($points_arr as $point) {
73 if (! isset($temp_point)) {
74 $temp_point = $point;
75 } else {
76 // draw line section
77 imageline($image, $temp_point[0], $temp_point[1], $point[0], $point[1], $color);
78 $temp_point = $point;
81 // print label if applicable
82 if (isset($label) && trim($label) != '') {
83 imagestring($image, 1, $points_arr[1][0], $points_arr[1][1], trim($label), $black);
85 return $image;
88 /**
89 * Adds to the TCPDF instance, the data related to a row in the GIS dataset.
91 * @param string $spatial GIS LINESTRING object
92 * @param string $label Label for the GIS LINESTRING object
93 * @param string $line_color Color for the GIS LINESTRING object
94 * @param array $scale_data Array containing data related to scaling
95 * @param image $pdf TCPDF instance
97 * @return the modified TCPDF instance
99 public function prepareRowAsPdf($spatial, $label, $line_color, $scale_data, $pdf)
101 // allocate colors
102 $red = hexdec(substr($line_color, 1, 2));
103 $green = hexdec(substr($line_color, 3, 2));
104 $blue = hexdec(substr($line_color, 4, 2));
105 $line = array('width' => 1.5, 'color' => array($red, $green, $blue));
107 // Trim to remove leading 'LINESTRING(' and trailing ')'
108 $linesrting = substr($spatial, 11, (strlen($spatial) - 12));
109 $points_arr = $this->extractPoints($linesrting, $scale_data);
111 foreach ($points_arr as $point) {
112 if (! isset($temp_point)) {
113 $temp_point = $point;
114 } else {
115 // draw line section
116 $pdf->Line($temp_point[0], $temp_point[1], $point[0], $point[1], $line);
117 $temp_point = $point;
120 // print label
121 if (isset($label) && trim($label) != '') {
122 $pdf->SetXY($points_arr[1][0], $points_arr[1][1]);
123 $pdf->SetFontSize(5);
124 $pdf->Cell(0, 0, trim($label));
126 return $pdf;
130 * Prepares and returns the code related to a row in the GIS dataset as SVG.
132 * @param string $spatial GIS LINESTRING object
133 * @param string $label Label for the GIS LINESTRING object
134 * @param string $line_color Color for the GIS LINESTRING object
135 * @param array $scale_data Array containing data related to scaling
137 * @return the code related to a row in the GIS dataset
139 public function prepareRowAsSvg($spatial, $label, $line_color, $scale_data)
141 $line_options = array(
142 'name' => $label,
143 'id' => $label . rand(),
144 'class' => 'linestring vector',
145 'fill' => 'none',
146 'stroke' => $line_color,
147 'stroke-width'=> 2,
150 // Trim to remove leading 'LINESTRING(' and trailing ')'
151 $linesrting = substr($spatial, 11, (strlen($spatial) - 12));
152 $points_arr = $this->extractPoints($linesrting, $scale_data);
154 $row = '<polyline points="';
155 foreach ($points_arr as $point) {
156 $row .= $point[0] . ',' . $point[1] . ' ';
158 $row .= '"';
159 foreach ($line_options as $option => $val) {
160 $row .= ' ' . $option . '="' . trim($val) . '"';
162 $row .= '/>';
164 return $row;
168 * Prepares JavaScript related to a row in the GIS dataset
169 * to visualize it with OpenLayers.
171 * @param string $spatial GIS LINESTRING object
172 * @param int $srid Spatial reference ID
173 * @param string $label Label for the GIS LINESTRING object
174 * @param string $line_color Color for the GIS LINESTRING object
175 * @param array $scale_data Array containing data related to scaling
177 * @return JavaScript related to a row in the GIS dataset
179 public function prepareRowAsOl($spatial, $srid, $label, $line_color, $scale_data)
181 $style_options = array(
182 'strokeColor' => $line_color,
183 'strokeWidth' => 2,
184 'label' => $label,
185 'fontSize' => 10,
187 if ($srid == 0) {
188 $srid = 4326;
190 $result = $this->getBoundsForOl($srid, $scale_data);
192 // Trim to remove leading 'LINESTRING(' and trailing ')'
193 $linesrting = substr($spatial, 11, (strlen($spatial) - 12));
194 $points_arr = $this->extractPoints($linesrting, null);
196 $row = 'new Array(';
197 foreach ($points_arr as $point) {
198 $row .= '(new OpenLayers.Geometry.Point(' . $point[0] . ', '
199 . $point[1] . ')).transform(new OpenLayers.Projection("EPSG:'
200 . $srid . '"), map.getProjectionObject()), ';
202 $row = substr($row, 0, strlen($row) - 2);
203 $row .= ')';
205 $result .= 'vectorLayer.addFeatures(new OpenLayers.Feature.Vector('
206 . 'new OpenLayers.Geometry.LineString(' . $row . '), null, '
207 . json_encode($style_options) . '));';
208 return $result;
212 * Generate the WKT with the set of parameters passed by the GIS editor.
214 * @param array $gis_data GIS data
215 * @param int $index Index into the parameter object
216 * @param string $empty Value for empty points
218 * @return WKT with the set of parameters passed by the GIS editor
220 public function generateWkt($gis_data, $index, $empty = '')
222 $no_of_points = isset($gis_data[$index]['LINESTRING']['no_of_points'])
223 ? $gis_data[$index]['LINESTRING']['no_of_points'] : 2;
224 if ($no_of_points < 2) {
225 $no_of_points = 2;
227 $wkt = 'LINESTRING(';
228 for ($i = 0; $i < $no_of_points; $i++) {
229 $wkt .= ((isset($gis_data[$index]['LINESTRING'][$i]['x'])
230 && trim($gis_data[$index]['LINESTRING'][$i]['x']) != '')
231 ? $gis_data[$index]['LINESTRING'][$i]['x'] : $empty)
232 . ' ' . ((isset($gis_data[$index]['LINESTRING'][$i]['y'])
233 && trim($gis_data[$index]['LINESTRING'][$i]['y']) != '')
234 ? $gis_data[$index]['LINESTRING'][$i]['y'] : $empty) .',';
236 $wkt = substr($wkt, 0, strlen($wkt) - 1);
237 $wkt .= ')';
238 return $wkt;
242 * Generate parameters for the GIS data editor from the value of the GIS column.
244 * @param string $value of the GIS column
245 * @param index $index of the geometry
247 * @return parameters for the GIS data editor from the value of the GIS column
249 public function generateParams($value, $index = -1)
251 if ($index == -1) {
252 $index = 0;
253 $params = array();
254 $data = PMA_GIS_Geometry::generateParams($value);
255 $params['srid'] = $data['srid'];
256 $wkt = $data['wkt'];
257 } else {
258 $params[$index]['gis_type'] = 'LINESTRING';
259 $wkt = $value;
262 // Trim to remove leading 'LINESTRING(' and trailing ')'
263 $linestring = substr($wkt, 11, (strlen($wkt) - 12));
264 $points_arr = $this->extractPoints($linestring, null);
266 $no_of_points = count($points_arr);
267 $params[$index]['LINESTRING']['no_of_points'] = $no_of_points;
268 for ($i = 0; $i < $no_of_points; $i++) {
269 $params[$index]['LINESTRING'][$i]['x'] = $points_arr[$i][0];
270 $params[$index]['LINESTRING'][$i]['y'] = $points_arr[$i][1];
273 return $params;