UPDATE 4.4.0.0
[phpmyadmin.git] / libraries / gis / GIS_Multilinestring.class.php
blob5ebb55ee1a1536c459a7bbf41bdb30deb75c198a
1 <?php
2 /* vim: set expandtab sw=4 ts=4 sts=4: */
3 /**
4 * Handles actions related to GIS MULTILINESTRING objects
6 * @package PhpMyAdmin-GIS
7 */
9 if (! defined('PHPMYADMIN')) {
10 exit;
13 /**
14 * Handles actions related to GIS MULTILINESTRING objects
16 * @package PhpMyAdmin-GIS
18 class PMA_GIS_Multilinestring extends PMA_GIS_Geometry
20 // Hold the singleton instance of the class
21 private static $_instance;
23 /**
24 * A private constructor; prevents direct creation of object.
26 * @access private
28 private function __construct()
32 /**
33 * Returns the singleton.
35 * @return PMA_GIS_Multilinestring the singleton
36 * @access public
38 public static function singleton()
40 if (!isset(self::$_instance)) {
41 $class = __CLASS__;
42 self::$_instance = new $class;
45 return self::$_instance;
48 /**
49 * Scales each row.
51 * @param string $spatial spatial data of a row
53 * @return array an array containing the min, max values for x and y coordinates
54 * @access public
56 public function scaleRow($spatial)
58 $min_max = array();
60 // Trim to remove leading 'MULTILINESTRING((' and trailing '))'
61 $multilinestirng = /*overload*/mb_substr(
62 $spatial,
63 17,
64 /*overload*/mb_strlen($spatial) - 19
66 // Separate each linestring
67 $linestirngs = explode("),(", $multilinestirng);
69 foreach ($linestirngs as $linestring) {
70 $min_max = $this->setMinMax($linestring, $min_max);
73 return $min_max;
76 /**
77 * Adds to the PNG image object, the data related to a row in the GIS dataset.
79 * @param string $spatial GIS MULTILINESTRING object
80 * @param string $label Label for the GIS MULTILINESTRING object
81 * @param string $line_color Color for the GIS MULTILINESTRING object
82 * @param array $scale_data Array containing data related to scaling
83 * @param object $image Image object
85 * @return object the modified image object
86 * @access public
88 public function prepareRowAsPng($spatial, $label, $line_color,
89 $scale_data, $image
90 ) {
91 // allocate colors
92 $black = imagecolorallocate($image, 0, 0, 0);
93 $red = hexdec(/*overload*/mb_substr($line_color, 1, 2));
94 $green = hexdec(/*overload*/mb_substr($line_color, 3, 2));
95 $blue = hexdec(/*overload*/mb_substr($line_color, 4, 2));
96 $color = imagecolorallocate($image, $red, $green, $blue);
98 // Trim to remove leading 'MULTILINESTRING((' and trailing '))'
99 $multilinestirng = /*overload*/mb_substr(
100 $spatial,
102 /*overload*/mb_strlen($spatial) - 19
104 // Separate each linestring
105 $linestirngs = explode("),(", $multilinestirng);
107 $first_line = true;
108 foreach ($linestirngs as $linestring) {
109 $points_arr = $this->extractPoints($linestring, $scale_data);
110 foreach ($points_arr as $point) {
111 if (! isset($temp_point)) {
112 $temp_point = $point;
113 } else {
114 // draw line section
115 imageline(
116 $image, $temp_point[0], $temp_point[1],
117 $point[0], $point[1], $color
119 $temp_point = $point;
122 unset($temp_point);
123 // print label if applicable
124 if (isset($label) && trim($label) != '' && $first_line) {
125 imagestring(
126 $image, 1, $points_arr[1][0],
127 $points_arr[1][1], trim($label), $black
130 $first_line = false;
132 return $image;
136 * Adds to the TCPDF instance, the data related to a row in the GIS dataset.
138 * @param string $spatial GIS MULTILINESTRING object
139 * @param string $label Label for the GIS MULTILINESTRING object
140 * @param string $line_color Color for the GIS MULTILINESTRING object
141 * @param array $scale_data Array containing data related to scaling
142 * @param TCPDF $pdf TCPDF instance
144 * @return TCPDF the modified TCPDF instance
145 * @access public
147 public function prepareRowAsPdf($spatial, $label, $line_color, $scale_data, $pdf)
149 // allocate colors
150 $red = hexdec(/*overload*/mb_substr($line_color, 1, 2));
151 $green = hexdec(/*overload*/mb_substr($line_color, 3, 2));
152 $blue = hexdec(/*overload*/mb_substr($line_color, 4, 2));
153 $line = array('width' => 1.5, 'color' => array($red, $green, $blue));
155 // Trim to remove leading 'MULTILINESTRING((' and trailing '))'
156 $multilinestirng = /*overload*/mb_substr(
157 $spatial,
158 17, /*overload*/mb_strlen($spatial) - 19
160 // Separate each linestring
161 $linestirngs = explode("),(", $multilinestirng);
163 $first_line = true;
164 foreach ($linestirngs as $linestring) {
165 $points_arr = $this->extractPoints($linestring, $scale_data);
166 foreach ($points_arr as $point) {
167 if (! isset($temp_point)) {
168 $temp_point = $point;
169 } else {
170 // draw line section
171 $pdf->Line(
172 $temp_point[0], $temp_point[1], $point[0], $point[1], $line
174 $temp_point = $point;
177 unset($temp_point);
178 // print label
179 if (isset($label) && trim($label) != '' && $first_line) {
180 $pdf->SetXY($points_arr[1][0], $points_arr[1][1]);
181 $pdf->SetFontSize(5);
182 $pdf->Cell(0, 0, trim($label));
184 $first_line = false;
186 return $pdf;
190 * Prepares and returns the code related to a row in the GIS dataset as SVG.
192 * @param string $spatial GIS MULTILINESTRING object
193 * @param string $label Label for the GIS MULTILINESTRING object
194 * @param string $line_color Color for the GIS MULTILINESTRING object
195 * @param array $scale_data Array containing data related to scaling
197 * @return string the code related to a row in the GIS dataset
198 * @access public
200 public function prepareRowAsSvg($spatial, $label, $line_color, $scale_data)
202 $line_options = array(
203 'name' => $label,
204 'class' => 'linestring vector',
205 'fill' => 'none',
206 'stroke' => $line_color,
207 'stroke-width'=> 2,
210 // Trim to remove leading 'MULTILINESTRING((' and trailing '))'
211 $multilinestirng = /*overload*/mb_substr(
212 $spatial,
214 /*overload*/mb_strlen($spatial) - 19
216 // Separate each linestring
217 $linestirngs = explode("),(", $multilinestirng);
219 $row = '';
220 foreach ($linestirngs as $linestring) {
221 $points_arr = $this->extractPoints($linestring, $scale_data);
223 $row .= '<polyline points="';
224 foreach ($points_arr as $point) {
225 $row .= $point[0] . ',' . $point[1] . ' ';
227 $row .= '"';
228 $line_options['id'] = $label . rand();
229 foreach ($line_options as $option => $val) {
230 $row .= ' ' . $option . '="' . trim($val) . '"';
232 $row .= '/>';
235 return $row;
239 * Prepares JavaScript related to a row in the GIS dataset
240 * to visualize it with OpenLayers.
242 * @param string $spatial GIS MULTILINESTRING object
243 * @param int $srid Spatial reference ID
244 * @param string $label Label for the GIS MULTILINESTRING object
245 * @param string $line_color Color for the GIS MULTILINESTRING object
246 * @param array $scale_data Array containing data related to scaling
248 * @return string JavaScript related to a row in the GIS dataset
249 * @access public
251 public function prepareRowAsOl($spatial, $srid, $label, $line_color, $scale_data)
253 $style_options = array(
254 'strokeColor' => $line_color,
255 'strokeWidth' => 2,
256 'label' => $label,
257 'fontSize' => 10,
259 if ($srid == 0) {
260 $srid = 4326;
262 $row = $this->getBoundsForOl($srid, $scale_data);
264 // Trim to remove leading 'MULTILINESTRING((' and trailing '))'
265 $multilinestirng = /*overload*/mb_substr(
266 $spatial,
268 /*overload*/mb_strlen($spatial) - 19
270 // Separate each linestring
271 $linestirngs = explode("),(", $multilinestirng);
273 $row .= 'vectorLayer.addFeatures(new OpenLayers.Feature.Vector('
274 . 'new OpenLayers.Geometry.MultiLineString('
275 . $this->getLineArrayForOpenLayers($linestirngs, $srid)
276 . '), null, ' . json_encode($style_options) . '));';
277 return $row;
281 * Generate the WKT with the set of parameters passed by the GIS editor.
283 * @param array $gis_data GIS data
284 * @param int $index Index into the parameter object
285 * @param string $empty Value for empty points
287 * @return string WKT with the set of parameters passed by the GIS editor
288 * @access public
290 public function generateWkt($gis_data, $index, $empty = '')
292 $data_row = $gis_data[$index]['MULTILINESTRING'];
294 $no_of_lines = isset($data_row['no_of_lines'])
295 ? $data_row['no_of_lines'] : 1;
296 if ($no_of_lines < 1) {
297 $no_of_lines = 1;
300 $wkt = 'MULTILINESTRING(';
301 for ($i = 0; $i < $no_of_lines; $i++) {
302 $no_of_points = isset($data_row[$i]['no_of_points'])
303 ? $data_row[$i]['no_of_points'] : 2;
304 if ($no_of_points < 2) {
305 $no_of_points = 2;
307 $wkt .= '(';
308 for ($j = 0; $j < $no_of_points; $j++) {
309 $wkt .= ((isset($data_row[$i][$j]['x'])
310 && trim($data_row[$i][$j]['x']) != '')
311 ? $data_row[$i][$j]['x'] : $empty)
312 . ' ' . ((isset($data_row[$i][$j]['y'])
313 && trim($data_row[$i][$j]['y']) != '')
314 ? $data_row[$i][$j]['y'] : $empty) . ',';
316 $wkt = /*overload*/mb_substr($wkt, 0, /*overload*/mb_strlen($wkt) - 1);
317 $wkt .= '),';
319 $wkt = /*overload*/mb_substr($wkt, 0, /*overload*/mb_strlen($wkt) - 1);
320 $wkt .= ')';
321 return $wkt;
325 * Generate the WKT for the data from ESRI shape files.
327 * @param array $row_data GIS data
329 * @return string the WKT for the data from ESRI shape files
330 * @access public
332 public function getShape($row_data)
334 $wkt = 'MULTILINESTRING(';
335 for ($i = 0; $i < $row_data['numparts']; $i++) {
336 $wkt .= '(';
337 foreach ($row_data['parts'][$i]['points'] as $point) {
338 $wkt .= $point['x'] . ' ' . $point['y'] . ',';
340 $wkt = /*overload*/mb_substr($wkt, 0, /*overload*/mb_strlen($wkt) - 1);
341 $wkt .= '),';
343 $wkt = /*overload*/mb_substr($wkt, 0, /*overload*/mb_strlen($wkt) - 1);
344 $wkt .= ')';
345 return $wkt;
349 * Generate parameters for the GIS data editor from the value of the GIS column.
351 * @param string $value Value of the GIS column
352 * @param int $index Index of the geometry
354 * @return array params for the GIS data editor from the value of the GIS column
355 * @access public
357 public function generateParams($value, $index = -1)
359 $params = array();
360 if ($index == -1) {
361 $index = 0;
362 $data = PMA_GIS_Geometry::generateParams($value);
363 $params['srid'] = $data['srid'];
364 $wkt = $data['wkt'];
365 } else {
366 $params[$index]['gis_type'] = 'MULTILINESTRING';
367 $wkt = $value;
370 // Trim to remove leading 'MULTILINESTRING((' and trailing '))'
371 $multilinestirng = /*overload*/mb_substr(
372 $wkt,
374 /*overload*/mb_strlen($wkt) - 19
376 // Separate each linestring
377 $linestirngs = explode("),(", $multilinestirng);
378 $params[$index]['MULTILINESTRING']['no_of_lines'] = count($linestirngs);
380 $j = 0;
381 foreach ($linestirngs as $linestring) {
382 $points_arr = $this->extractPoints($linestring, null);
383 $no_of_points = count($points_arr);
384 $params[$index]['MULTILINESTRING'][$j]['no_of_points'] = $no_of_points;
385 for ($i = 0; $i < $no_of_points; $i++) {
386 $params[$index]['MULTILINESTRING'][$j][$i]['x'] = $points_arr[$i][0];
387 $params[$index]['MULTILINESTRING'][$j][$i]['y'] = $points_arr[$i][1];
389 $j++;
391 return $params;