Translated using Weblate (Norwegian Bokmål)
[phpmyadmin.git] / libraries / gis / GISMultilinestring.php
blob398815ef48d62c84684c126928483169aab760a0
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 namespace PMA\libraries\gis;
11 use \TCPDF;
13 /**
14 * Handles actions related to GIS MULTILINESTRING objects
16 * @package PhpMyAdmin-GIS
18 class GISMultilinestring extends GISGeometry
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 GISMultilinestring 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
62 = mb_substr(
63 $spatial,
64 17,
65 mb_strlen($spatial) - 19
67 // Separate each linestring
68 $linestirngs = explode("),(", $multilinestirng);
70 foreach ($linestirngs as $linestring) {
71 $min_max = $this->setMinMax($linestring, $min_max);
74 return $min_max;
77 /**
78 * Adds to the PNG image object, the data related to a row in the GIS dataset.
80 * @param string $spatial GIS MULTILINESTRING object
81 * @param string $label Label for the GIS MULTILINESTRING object
82 * @param string $line_color Color for the GIS MULTILINESTRING object
83 * @param array $scale_data Array containing data related to scaling
84 * @param object $image Image object
86 * @return object the modified image object
87 * @access public
89 public function prepareRowAsPng(
90 $spatial,
91 $label,
92 $line_color,
93 $scale_data,
94 $image
95 ) {
96 // allocate colors
97 $black = imagecolorallocate($image, 0, 0, 0);
98 $red = hexdec(mb_substr($line_color, 1, 2));
99 $green = hexdec(mb_substr($line_color, 3, 2));
100 $blue = hexdec(mb_substr($line_color, 4, 2));
101 $color = imagecolorallocate($image, $red, $green, $blue);
103 // Trim to remove leading 'MULTILINESTRING((' and trailing '))'
104 $multilinestirng
105 = mb_substr(
106 $spatial,
108 mb_strlen($spatial) - 19
110 // Separate each linestring
111 $linestirngs = explode("),(", $multilinestirng);
113 $first_line = true;
114 foreach ($linestirngs as $linestring) {
115 $points_arr = $this->extractPoints($linestring, $scale_data);
116 foreach ($points_arr as $point) {
117 if (!isset($temp_point)) {
118 $temp_point = $point;
119 } else {
120 // draw line section
121 imageline(
122 $image,
123 $temp_point[0],
124 $temp_point[1],
125 $point[0],
126 $point[1],
127 $color
129 $temp_point = $point;
132 unset($temp_point);
133 // print label if applicable
134 if (isset($label) && trim($label) != '' && $first_line) {
135 imagestring(
136 $image,
138 $points_arr[1][0],
139 $points_arr[1][1],
140 trim($label),
141 $black
144 $first_line = false;
147 return $image;
151 * Adds to the TCPDF instance, the data related to a row in the GIS dataset.
153 * @param string $spatial GIS MULTILINESTRING object
154 * @param string $label Label for the GIS MULTILINESTRING object
155 * @param string $line_color Color for the GIS MULTILINESTRING object
156 * @param array $scale_data Array containing data related to scaling
157 * @param TCPDF $pdf TCPDF instance
159 * @return TCPDF the modified TCPDF instance
160 * @access public
162 public function prepareRowAsPdf($spatial, $label, $line_color, $scale_data, $pdf)
164 // allocate colors
165 $red = hexdec(mb_substr($line_color, 1, 2));
166 $green = hexdec(mb_substr($line_color, 3, 2));
167 $blue = hexdec(mb_substr($line_color, 4, 2));
168 $line = array('width' => 1.5, 'color' => array($red, $green, $blue));
170 // Trim to remove leading 'MULTILINESTRING((' and trailing '))'
171 $multilinestirng
172 = mb_substr(
173 $spatial,
175 mb_strlen($spatial) - 19
177 // Separate each linestring
178 $linestirngs = explode("),(", $multilinestirng);
180 $first_line = true;
181 foreach ($linestirngs as $linestring) {
182 $points_arr = $this->extractPoints($linestring, $scale_data);
183 foreach ($points_arr as $point) {
184 if (!isset($temp_point)) {
185 $temp_point = $point;
186 } else {
187 // draw line section
188 $pdf->Line(
189 $temp_point[0],
190 $temp_point[1],
191 $point[0],
192 $point[1],
193 $line
195 $temp_point = $point;
198 unset($temp_point);
199 // print label
200 if (isset($label) && trim($label) != '' && $first_line) {
201 $pdf->SetXY($points_arr[1][0], $points_arr[1][1]);
202 $pdf->SetFontSize(5);
203 $pdf->Cell(0, 0, trim($label));
205 $first_line = false;
208 return $pdf;
212 * Prepares and returns the code related to a row in the GIS dataset as SVG.
214 * @param string $spatial GIS MULTILINESTRING object
215 * @param string $label Label for the GIS MULTILINESTRING object
216 * @param string $line_color Color for the GIS MULTILINESTRING object
217 * @param array $scale_data Array containing data related to scaling
219 * @return string the code related to a row in the GIS dataset
220 * @access public
222 public function prepareRowAsSvg($spatial, $label, $line_color, $scale_data)
224 $line_options = array(
225 'name' => $label,
226 'class' => 'linestring vector',
227 'fill' => 'none',
228 'stroke' => $line_color,
229 'stroke-width' => 2,
232 // Trim to remove leading 'MULTILINESTRING((' and trailing '))'
233 $multilinestirng
234 = mb_substr(
235 $spatial,
237 mb_strlen($spatial) - 19
239 // Separate each linestring
240 $linestirngs = explode("),(", $multilinestirng);
242 $row = '';
243 foreach ($linestirngs as $linestring) {
244 $points_arr = $this->extractPoints($linestring, $scale_data);
246 $row .= '<polyline points="';
247 foreach ($points_arr as $point) {
248 $row .= $point[0] . ',' . $point[1] . ' ';
250 $row .= '"';
251 $line_options['id'] = $label . rand();
252 foreach ($line_options as $option => $val) {
253 $row .= ' ' . $option . '="' . trim($val) . '"';
255 $row .= '/>';
258 return $row;
262 * Prepares JavaScript related to a row in the GIS dataset
263 * to visualize it with OpenLayers.
265 * @param string $spatial GIS MULTILINESTRING object
266 * @param int $srid Spatial reference ID
267 * @param string $label Label for the GIS MULTILINESTRING object
268 * @param string $line_color Color for the GIS MULTILINESTRING object
269 * @param array $scale_data Array containing data related to scaling
271 * @return string JavaScript related to a row in the GIS dataset
272 * @access public
274 public function prepareRowAsOl($spatial, $srid, $label, $line_color, $scale_data)
276 $style_options = array(
277 'strokeColor' => $line_color,
278 'strokeWidth' => 2,
279 'label' => $label,
280 'fontSize' => 10,
282 if ($srid == 0) {
283 $srid = 4326;
285 $row = $this->getBoundsForOl($srid, $scale_data);
287 // Trim to remove leading 'MULTILINESTRING((' and trailing '))'
288 $multilinestirng
289 = mb_substr(
290 $spatial,
292 mb_strlen($spatial) - 19
294 // Separate each linestring
295 $linestirngs = explode("),(", $multilinestirng);
297 $row .= 'vectorLayer.addFeatures(new OpenLayers.Feature.Vector('
298 . 'new OpenLayers.Geometry.MultiLineString('
299 . $this->getLineArrayForOpenLayers($linestirngs, $srid)
300 . '), null, ' . json_encode($style_options) . '));';
302 return $row;
306 * Generate the WKT with the set of parameters passed by the GIS editor.
308 * @param array $gis_data GIS data
309 * @param int $index Index into the parameter object
310 * @param string $empty Value for empty points
312 * @return string WKT with the set of parameters passed by the GIS editor
313 * @access public
315 public function generateWkt($gis_data, $index, $empty = '')
317 $data_row = $gis_data[$index]['MULTILINESTRING'];
319 $no_of_lines = isset($data_row['no_of_lines'])
320 ? $data_row['no_of_lines'] : 1;
321 if ($no_of_lines < 1) {
322 $no_of_lines = 1;
325 $wkt = 'MULTILINESTRING(';
326 for ($i = 0; $i < $no_of_lines; $i++) {
327 $no_of_points = isset($data_row[$i]['no_of_points'])
328 ? $data_row[$i]['no_of_points'] : 2;
329 if ($no_of_points < 2) {
330 $no_of_points = 2;
332 $wkt .= '(';
333 for ($j = 0; $j < $no_of_points; $j++) {
334 $wkt .= ((isset($data_row[$i][$j]['x'])
335 && trim($data_row[$i][$j]['x']) != '')
336 ? $data_row[$i][$j]['x'] : $empty)
337 . ' ' . ((isset($data_row[$i][$j]['y'])
338 && trim($data_row[$i][$j]['y']) != '')
339 ? $data_row[$i][$j]['y'] : $empty) . ',';
341 $wkt
342 = mb_substr(
343 $wkt,
344 0, mb_strlen($wkt) - 1
346 $wkt .= '),';
348 $wkt
349 = mb_substr(
350 $wkt,
352 mb_strlen($wkt) - 1
354 $wkt .= ')';
356 return $wkt;
360 * Generate the WKT for the data from ESRI shape files.
362 * @param array $row_data GIS data
364 * @return string the WKT for the data from ESRI shape files
365 * @access public
367 public function getShape($row_data)
369 $wkt = 'MULTILINESTRING(';
370 for ($i = 0; $i < $row_data['numparts']; $i++) {
371 $wkt .= '(';
372 foreach ($row_data['parts'][$i]['points'] as $point) {
373 $wkt .= $point['x'] . ' ' . $point['y'] . ',';
375 $wkt
376 = mb_substr(
377 $wkt,
379 mb_strlen($wkt) - 1
381 $wkt .= '),';
383 $wkt
384 = mb_substr(
385 $wkt,
387 mb_strlen($wkt) - 1
389 $wkt .= ')';
391 return $wkt;
395 * Generate parameters for the GIS data editor from the value of the GIS column.
397 * @param string $value Value of the GIS column
398 * @param int $index Index of the geometry
400 * @return array params for the GIS data editor from the value of the GIS column
401 * @access public
403 public function generateParams($value, $index = -1)
405 $params = array();
406 if ($index == -1) {
407 $index = 0;
408 $data = GISGeometry::generateParams($value);
409 $params['srid'] = $data['srid'];
410 $wkt = $data['wkt'];
411 } else {
412 $params[$index]['gis_type'] = 'MULTILINESTRING';
413 $wkt = $value;
416 // Trim to remove leading 'MULTILINESTRING((' and trailing '))'
417 $multilinestirng
418 = mb_substr(
419 $wkt,
421 mb_strlen($wkt) - 19
423 // Separate each linestring
424 $linestirngs = explode("),(", $multilinestirng);
425 $params[$index]['MULTILINESTRING']['no_of_lines'] = count($linestirngs);
427 $j = 0;
428 foreach ($linestirngs as $linestring) {
429 $points_arr = $this->extractPoints($linestring, null);
430 $no_of_points = count($points_arr);
431 $params[$index]['MULTILINESTRING'][$j]['no_of_points'] = $no_of_points;
432 for ($i = 0; $i < $no_of_points; $i++) {
433 $params[$index]['MULTILINESTRING'][$j][$i]['x'] = $points_arr[$i][0];
434 $params[$index]['MULTILINESTRING'][$j][$i]['y'] = $points_arr[$i][1];
436 $j++;
439 return $params;