2 /* vim: set expandtab sw=4 ts=4 sts=4: */
4 * Handles actions related to GIS LINESTRING objects
6 * @package PhpMyAdmin-GIS
9 if (! defined('PHPMYADMIN')) {
14 * Handles actions related to GIS LINESTRING objects
16 * @package PhpMyAdmin-GIS
18 class PMA_GIS_Linestring
extends PMA_GIS_Geometry
20 // Hold the singleton instance of the class
21 private static $_instance;
24 * A private constructor; prevents direct creation of object.
28 private function __construct()
33 * Returns the singleton.
35 * @return object the singleton
38 public static function singleton()
40 if (!isset(self
::$_instance)) {
42 self
::$_instance = new $class;
45 return self
::$_instance;
51 * @param string $spatial spatial data of a row
53 * @return array an array containing the min, max values for x and y cordinates
56 public function scaleRow($spatial)
58 // Trim to remove leading 'LINESTRING(' and trailing ')'
59 $linesrting = substr($spatial, 11, (strlen($spatial) - 12));
60 return $this->setMinMax($linesrting, array());
64 * Adds to the PNG image object, the data related to a row in the GIS dataset.
66 * @param string $spatial GIS LINESTRING object
67 * @param string $label Label for the GIS LINESTRING object
68 * @param string $line_color Color for the GIS LINESTRING object
69 * @param array $scale_data Array containing data related to scaling
70 * @param object $image Image object
72 * @return object the modified image object
75 public function prepareRowAsPng($spatial, $label, $line_color,
79 $black = imagecolorallocate($image, 0, 0, 0);
80 $red = hexdec(substr($line_color, 1, 2));
81 $green = hexdec(substr($line_color, 3, 2));
82 $blue = hexdec(substr($line_color, 4, 2));
83 $color = imagecolorallocate($image, $red, $green, $blue);
85 // Trim to remove leading 'LINESTRING(' and trailing ')'
86 $linesrting = substr($spatial, 11, (strlen($spatial) - 12));
87 $points_arr = $this->extractPoints($linesrting, $scale_data);
89 foreach ($points_arr as $point) {
90 if (! isset($temp_point)) {
95 $image, $temp_point[0], $temp_point[1],
96 $point[0], $point[1], $color
101 // print label if applicable
102 if (isset($label) && trim($label) != '') {
104 $image, 1, $points_arr[1][0],
105 $points_arr[1][1], trim($label), $black
112 * Adds to the TCPDF instance, the data related to a row in the GIS dataset.
114 * @param string $spatial GIS LINESTRING object
115 * @param string $label Label for the GIS LINESTRING object
116 * @param string $line_color Color for the GIS LINESTRING object
117 * @param array $scale_data Array containing data related to scaling
118 * @param object $pdf TCPDF instance
120 * @return object the modified TCPDF instance
123 public function prepareRowAsPdf($spatial, $label, $line_color, $scale_data, $pdf)
126 $red = hexdec(substr($line_color, 1, 2));
127 $green = hexdec(substr($line_color, 3, 2));
128 $blue = hexdec(substr($line_color, 4, 2));
129 $line = array('width' => 1.5, 'color' => array($red, $green, $blue));
131 // Trim to remove leading 'LINESTRING(' and trailing ')'
132 $linesrting = substr($spatial, 11, (strlen($spatial) - 12));
133 $points_arr = $this->extractPoints($linesrting, $scale_data);
135 foreach ($points_arr as $point) {
136 if (! isset($temp_point)) {
137 $temp_point = $point;
141 $temp_point[0], $temp_point[1],
142 $point[0], $point[1], $line
144 $temp_point = $point;
148 if (isset($label) && trim($label) != '') {
149 $pdf->SetXY($points_arr[1][0], $points_arr[1][1]);
150 $pdf->SetFontSize(5);
151 $pdf->Cell(0, 0, trim($label));
157 * Prepares and returns the code related to a row in the GIS dataset as SVG.
159 * @param string $spatial GIS LINESTRING object
160 * @param string $label Label for the GIS LINESTRING object
161 * @param string $line_color Color for the GIS LINESTRING object
162 * @param array $scale_data Array containing data related to scaling
164 * @return string the code related to a row in the GIS dataset
167 public function prepareRowAsSvg($spatial, $label, $line_color, $scale_data)
169 $line_options = array(
171 'id' => $label . rand(),
172 'class' => 'linestring vector',
174 'stroke' => $line_color,
178 // Trim to remove leading 'LINESTRING(' and trailing ')'
179 $linesrting = substr($spatial, 11, (strlen($spatial) - 12));
180 $points_arr = $this->extractPoints($linesrting, $scale_data);
182 $row = '<polyline points="';
183 foreach ($points_arr as $point) {
184 $row .= $point[0] . ',' . $point[1] . ' ';
187 foreach ($line_options as $option => $val) {
188 $row .= ' ' . $option . '="' . trim($val) . '"';
196 * Prepares JavaScript related to a row in the GIS dataset
197 * to visualize it with OpenLayers.
199 * @param string $spatial GIS LINESTRING object
200 * @param int $srid Spatial reference ID
201 * @param string $label Label for the GIS LINESTRING object
202 * @param string $line_color Color for the GIS LINESTRING object
203 * @param array $scale_data Array containing data related to scaling
205 * @return string JavaScript related to a row in the GIS dataset
208 public function prepareRowAsOl($spatial, $srid, $label, $line_color, $scale_data)
210 $style_options = array(
211 'strokeColor' => $line_color,
219 $result = $this->getBoundsForOl($srid, $scale_data);
221 // Trim to remove leading 'LINESTRING(' and trailing ')'
222 $linesrting = substr($spatial, 11, (strlen($spatial) - 12));
223 $points_arr = $this->extractPoints($linesrting, null);
225 $result .= 'vectorLayer.addFeatures(new OpenLayers.Feature.Vector('
226 . $this->getLineForOpenLayers($points_arr, $srid)
227 . ', null, ' . json_encode($style_options) . '));';
232 * Generate the WKT with the set of parameters passed by the GIS editor.
234 * @param array $gis_data GIS data
235 * @param int $index Index into the parameter object
236 * @param string $empty Value for empty points
238 * @return string WKT with the set of parameters passed by the GIS editor
241 public function generateWkt($gis_data, $index, $empty = '')
243 $no_of_points = isset($gis_data[$index]['LINESTRING']['no_of_points'])
244 ?
$gis_data[$index]['LINESTRING']['no_of_points'] : 2;
245 if ($no_of_points < 2) {
248 $wkt = 'LINESTRING(';
249 for ($i = 0; $i < $no_of_points; $i++
) {
250 $wkt .= ((isset($gis_data[$index]['LINESTRING'][$i]['x'])
251 && trim($gis_data[$index]['LINESTRING'][$i]['x']) != '')
252 ?
$gis_data[$index]['LINESTRING'][$i]['x'] : $empty)
253 . ' ' . ((isset($gis_data[$index]['LINESTRING'][$i]['y'])
254 && trim($gis_data[$index]['LINESTRING'][$i]['y']) != '')
255 ?
$gis_data[$index]['LINESTRING'][$i]['y'] : $empty) .',';
257 $wkt = substr($wkt, 0, strlen($wkt) - 1);
263 * Generate parameters for the GIS data editor from the value of the GIS column.
265 * @param string $value of the GIS column
266 * @param int $index of the geometry
268 * @return array params for the GIS data editor from the value of the GIS column
271 public function generateParams($value, $index = -1)
276 $data = PMA_GIS_Geometry
::generateParams($value);
277 $params['srid'] = $data['srid'];
280 $params[$index]['gis_type'] = 'LINESTRING';
284 // Trim to remove leading 'LINESTRING(' and trailing ')'
285 $linestring = substr($wkt, 11, (strlen($wkt) - 12));
286 $points_arr = $this->extractPoints($linestring, null);
288 $no_of_points = count($points_arr);
289 $params[$index]['LINESTRING']['no_of_points'] = $no_of_points;
290 for ($i = 0; $i < $no_of_points; $i++
) {
291 $params[$index]['LINESTRING'][$i]['x'] = $points_arr[$i][0];
292 $params[$index]['LINESTRING'][$i]['y'] = $points_arr[$i][1];