~ubuntu-branches/ubuntu/saucy/phpmyadmin/saucy-proposed

« back to all changes in this revision

Viewing changes to libraries/gis/pma_gis_multilinestring.php

  • Committer: Package Import Robot
  • Author(s): Thijs Kinkhorst
  • Date: 2013-05-16 20:53:50 UTC
  • mfrom: (1.2.40) (55.1.8 experimental)
  • Revision ID: package-import@ubuntu.com-20130516205350-sam3ls8m02vn3967
Tags: 4:4.0.1-1
* New upstream release.
* Update to debhelper 9, policy 3.9.4. 

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
<?php
 
2
/* vim: set expandtab sw=4 ts=4 sts=4: */
 
3
/**
 
4
 * Handles actions related to GIS MULTILINESTRING objects
 
5
 *
 
6
 * @package PhpMyAdmin-GIS
 
7
 */
 
8
 
 
9
if (! defined('PHPMYADMIN')) {
 
10
    exit;
 
11
}
 
12
 
 
13
/**
 
14
 * Handles actions related to GIS MULTILINESTRING objects
 
15
 *
 
16
 * @package PhpMyAdmin-GIS
 
17
 */
 
18
class PMA_GIS_Multilinestring extends PMA_GIS_Geometry
 
19
{
 
20
    // Hold the singleton instance of the class
 
21
    private static $_instance;
 
22
 
 
23
    /**
 
24
     * A private constructor; prevents direct creation of object.
 
25
     *
 
26
     * @access private
 
27
     */
 
28
    private function __construct()
 
29
    {
 
30
    }
 
31
 
 
32
    /**
 
33
     * Returns the singleton.
 
34
     *
 
35
     * @return object the singleton
 
36
     * @access public
 
37
     */
 
38
    public static function singleton()
 
39
    {
 
40
        if (!isset(self::$_instance)) {
 
41
            $class = __CLASS__;
 
42
            self::$_instance = new $class;
 
43
        }
 
44
 
 
45
        return self::$_instance;
 
46
    }
 
47
 
 
48
    /**
 
49
     * Scales each row.
 
50
     *
 
51
     * @param string $spatial spatial data of a row
 
52
     *
 
53
     * @return array an array containing the min, max values for x and y cordinates
 
54
     * @access public
 
55
     */
 
56
    public function scaleRow($spatial)
 
57
    {
 
58
        $min_max = array();
 
59
 
 
60
        // Trim to remove leading 'MULTILINESTRING((' and trailing '))'
 
61
        $multilinestirng = substr($spatial, 17, (strlen($spatial) - 19));
 
62
        // Seperate each linestring
 
63
        $linestirngs = explode("),(", $multilinestirng);
 
64
 
 
65
        foreach ($linestirngs as $linestring) {
 
66
            $min_max = $this->setMinMax($linestring, $min_max);
 
67
        }
 
68
 
 
69
        return $min_max;
 
70
    }
 
71
 
 
72
    /**
 
73
     * Adds to the PNG image object, the data related to a row in the GIS dataset.
 
74
     *
 
75
     * @param string $spatial    GIS MULTILINESTRING object
 
76
     * @param string $label      Label for the GIS MULTILINESTRING object
 
77
     * @param string $line_color Color for the GIS MULTILINESTRING object
 
78
     * @param array  $scale_data Array containing data related to scaling
 
79
     * @param object $image      Image object
 
80
     *
 
81
     * @return object the modified image object
 
82
     * @access public
 
83
     */
 
84
    public function prepareRowAsPng($spatial, $label, $line_color,
 
85
        $scale_data, $image
 
86
    ) {
 
87
        // allocate colors
 
88
        $black = imagecolorallocate($image, 0, 0, 0);
 
89
        $red   = hexdec(substr($line_color, 1, 2));
 
90
        $green = hexdec(substr($line_color, 3, 2));
 
91
        $blue  = hexdec(substr($line_color, 4, 2));
 
92
        $color = imagecolorallocate($image, $red, $green, $blue);
 
93
 
 
94
        // Trim to remove leading 'MULTILINESTRING((' and trailing '))'
 
95
        $multilinestirng = substr($spatial, 17, (strlen($spatial) - 19));
 
96
        // Seperate each linestring
 
97
        $linestirngs = explode("),(", $multilinestirng);
 
98
 
 
99
        $first_line = true;
 
100
        foreach ($linestirngs as $linestring) {
 
101
            $points_arr = $this->extractPoints($linestring, $scale_data);
 
102
            foreach ($points_arr as $point) {
 
103
                if (! isset($temp_point)) {
 
104
                    $temp_point = $point;
 
105
                } else {
 
106
                    // draw line section
 
107
                    imageline(
 
108
                        $image, $temp_point[0], $temp_point[1],
 
109
                        $point[0], $point[1], $color
 
110
                    );
 
111
                    $temp_point = $point;
 
112
                }
 
113
            }
 
114
            unset($temp_point);
 
115
            // print label if applicable
 
116
            if (isset($label) && trim($label) != '' && $first_line) {
 
117
                imagestring(
 
118
                    $image, 1, $points_arr[1][0],
 
119
                    $points_arr[1][1], trim($label), $black
 
120
                );
 
121
            }
 
122
            $first_line = false;
 
123
        }
 
124
        return $image;
 
125
    }
 
126
 
 
127
    /**
 
128
     * Adds to the TCPDF instance, the data related to a row in the GIS dataset.
 
129
     *
 
130
     * @param string $spatial    GIS MULTILINESTRING object
 
131
     * @param string $label      Label for the GIS MULTILINESTRING object
 
132
     * @param string $line_color Color for the GIS MULTILINESTRING object
 
133
     * @param array  $scale_data Array containing data related to scaling
 
134
     * @param object $pdf        TCPDF instance
 
135
     *
 
136
     * @return object the modified TCPDF instance
 
137
     * @access public
 
138
     */
 
139
    public function prepareRowAsPdf($spatial, $label, $line_color, $scale_data, $pdf)
 
140
    {
 
141
        // allocate colors
 
142
        $red   = hexdec(substr($line_color, 1, 2));
 
143
        $green = hexdec(substr($line_color, 3, 2));
 
144
        $blue  = hexdec(substr($line_color, 4, 2));
 
145
        $line  = array('width' => 1.5, 'color' => array($red, $green, $blue));
 
146
 
 
147
        // Trim to remove leading 'MULTILINESTRING((' and trailing '))'
 
148
        $multilinestirng = substr($spatial, 17, (strlen($spatial) - 19));
 
149
        // Seperate each linestring
 
150
        $linestirngs = explode("),(", $multilinestirng);
 
151
 
 
152
        $first_line = true;
 
153
        foreach ($linestirngs as $linestring) {
 
154
            $points_arr = $this->extractPoints($linestring, $scale_data);
 
155
            foreach ($points_arr as $point) {
 
156
                if (! isset($temp_point)) {
 
157
                    $temp_point = $point;
 
158
                } else {
 
159
                    // draw line section
 
160
                    $pdf->Line(
 
161
                        $temp_point[0], $temp_point[1], $point[0], $point[1], $line
 
162
                    );
 
163
                    $temp_point = $point;
 
164
                }
 
165
            }
 
166
            unset($temp_point);
 
167
            // print label
 
168
            if (isset($label) && trim($label) != '' && $first_line) {
 
169
                $pdf->SetXY($points_arr[1][0], $points_arr[1][1]);
 
170
                $pdf->SetFontSize(5);
 
171
                $pdf->Cell(0, 0, trim($label));
 
172
            }
 
173
            $first_line = false;
 
174
        }
 
175
        return $pdf;
 
176
    }
 
177
 
 
178
    /**
 
179
     * Prepares and returns the code related to a row in the GIS dataset as SVG.
 
180
     *
 
181
     * @param string $spatial    GIS MULTILINESTRING object
 
182
     * @param string $label      Label for the GIS MULTILINESTRING object
 
183
     * @param string $line_color Color for the GIS MULTILINESTRING object
 
184
     * @param array  $scale_data Array containing data related to scaling
 
185
     *
 
186
     * @return string the code related to a row in the GIS dataset
 
187
     * @access public
 
188
     */
 
189
    public function prepareRowAsSvg($spatial, $label, $line_color, $scale_data)
 
190
    {
 
191
        $line_options = array(
 
192
            'name'        => $label,
 
193
            'class'       => 'linestring vector',
 
194
            'fill'        => 'none',
 
195
            'stroke'      => $line_color,
 
196
            'stroke-width'=> 2,
 
197
        );
 
198
 
 
199
        // Trim to remove leading 'MULTILINESTRING((' and trailing '))'
 
200
        $multilinestirng = substr($spatial, 17, (strlen($spatial) - 19));
 
201
        // Seperate each linestring
 
202
        $linestirngs = explode("),(", $multilinestirng);
 
203
 
 
204
        $row = '';
 
205
        foreach ($linestirngs as $linestring) {
 
206
            $points_arr = $this->extractPoints($linestring, $scale_data);
 
207
 
 
208
            $row .= '<polyline points="';
 
209
            foreach ($points_arr as $point) {
 
210
                $row .= $point[0] . ',' . $point[1] . ' ';
 
211
            }
 
212
            $row .= '"';
 
213
            $line_options['id'] = $label . rand();
 
214
            foreach ($line_options as $option => $val) {
 
215
                $row .= ' ' . $option . '="' . trim($val) . '"';
 
216
            }
 
217
            $row .= '/>';
 
218
        }
 
219
 
 
220
        return $row;
 
221
    }
 
222
 
 
223
    /**
 
224
     * Prepares JavaScript related to a row in the GIS dataset
 
225
     * to visualize it with OpenLayers.
 
226
     *
 
227
     * @param string $spatial    GIS MULTILINESTRING object
 
228
     * @param int    $srid       Spatial reference ID
 
229
     * @param string $label      Label for the GIS MULTILINESTRING object
 
230
     * @param string $line_color Color for the GIS MULTILINESTRING object
 
231
     * @param array  $scale_data Array containing data related to scaling
 
232
     *
 
233
     * @return string JavaScript related to a row in the GIS dataset
 
234
     * @access public
 
235
     */
 
236
    public function prepareRowAsOl($spatial, $srid, $label, $line_color, $scale_data)
 
237
    {
 
238
        $style_options = array(
 
239
            'strokeColor' => $line_color,
 
240
            'strokeWidth' => 2,
 
241
            'label'       => $label,
 
242
            'fontSize'    => 10,
 
243
        );
 
244
        if ($srid == 0) {
 
245
            $srid = 4326;
 
246
        }
 
247
        $row = $this->getBoundsForOl($srid, $scale_data);
 
248
 
 
249
        // Trim to remove leading 'MULTILINESTRING((' and trailing '))'
 
250
        $multilinestirng = substr($spatial, 17, (strlen($spatial) - 19));
 
251
        // Seperate each linestring
 
252
        $linestirngs = explode("),(", $multilinestirng);
 
253
 
 
254
        $row .= 'vectorLayer.addFeatures(new OpenLayers.Feature.Vector('
 
255
            . 'new OpenLayers.Geometry.MultiLineString('
 
256
            . $this->getLineArrayForOpenLayers($linestirngs, $srid)
 
257
            . '), null, ' . json_encode($style_options) . '));';
 
258
        return $row;
 
259
    }
 
260
 
 
261
    /**
 
262
     * Generate the WKT with the set of parameters passed by the GIS editor.
 
263
     *
 
264
     * @param array  $gis_data GIS data
 
265
     * @param int    $index    Index into the parameter object
 
266
     * @param string $empty    Value for empty points
 
267
     *
 
268
     * @return string WKT with the set of parameters passed by the GIS editor
 
269
     * @access public
 
270
     */
 
271
    public function generateWkt($gis_data, $index, $empty = '')
 
272
    {
 
273
        $data_row = $gis_data[$index]['MULTILINESTRING'];
 
274
 
 
275
        $no_of_lines = isset($data_row['no_of_lines'])
 
276
            ? $data_row['no_of_lines'] : 1;
 
277
        if ($no_of_lines < 1) {
 
278
            $no_of_lines = 1;
 
279
        }
 
280
        $wkt = 'MULTILINESTRING(';
 
281
        for ($i = 0; $i < $no_of_lines; $i++) {
 
282
            $no_of_points = isset($data_row[$i]['no_of_points'])
 
283
                ? $data_row[$i]['no_of_points'] : 2;
 
284
            if ($no_of_points < 2) {
 
285
                $no_of_points = 2;
 
286
            }
 
287
            $wkt .= '(';
 
288
            for ($j = 0; $j < $no_of_points; $j++) {
 
289
                $wkt .= ((isset($data_row[$i][$j]['x'])
 
290
                    && trim($data_row[$i][$j]['x']) != '')
 
291
                    ? $data_row[$i][$j]['x'] : $empty)
 
292
                    . ' ' . ((isset($data_row[$i][$j]['y'])
 
293
                    && trim($data_row[$i][$j]['y']) != '')
 
294
                    ? $data_row[$i][$j]['y'] : $empty) . ',';
 
295
            }
 
296
            $wkt = substr($wkt, 0, strlen($wkt) - 1);
 
297
            $wkt .= '),';
 
298
        }
 
299
        $wkt = substr($wkt, 0, strlen($wkt) - 1);
 
300
        $wkt .= ')';
 
301
        return $wkt;
 
302
    }
 
303
 
 
304
    /**
 
305
     * Generate the WKT for the data from ESRI shape files.
 
306
     *
 
307
     * @param array $row_data GIS data
 
308
     *
 
309
     * @return string the WKT for the data from ESRI shape files
 
310
     * @access public
 
311
     */
 
312
    public function getShape($row_data)
 
313
    {
 
314
        $wkt = 'MULTILINESTRING(';
 
315
        for ($i = 0; $i < $row_data['numparts']; $i++) {
 
316
            $wkt .= '(';
 
317
            foreach ($row_data['parts'][$i]['points'] as $point) {
 
318
                $wkt .= $point['x'] . ' ' . $point['y'] . ',';
 
319
            }
 
320
            $wkt = substr($wkt, 0, strlen($wkt) - 1);
 
321
            $wkt .= '),';
 
322
        }
 
323
        $wkt = substr($wkt, 0, strlen($wkt) - 1);
 
324
        $wkt .= ')';
 
325
        return $wkt;
 
326
    }
 
327
 
 
328
    /**
 
329
     * Generate parameters for the GIS data editor from the value of the GIS column.
 
330
     *
 
331
     * @param string $value of the GIS column
 
332
     * @param index  $index of the geometry
 
333
     *
 
334
     * @return array params for the GIS data editor from the value of the GIS column
 
335
     * @access public
 
336
     */
 
337
    public function generateParams($value, $index = -1)
 
338
    {
 
339
        if ($index == -1) {
 
340
            $index = 0;
 
341
            $params = array();
 
342
            $data = PMA_GIS_Geometry::generateParams($value);
 
343
            $params['srid'] = $data['srid'];
 
344
            $wkt = $data['wkt'];
 
345
        } else {
 
346
            $params[$index]['gis_type'] = 'MULTILINESTRING';
 
347
            $wkt = $value;
 
348
        }
 
349
 
 
350
        // Trim to remove leading 'MULTILINESTRING((' and trailing '))'
 
351
        $multilinestirng = substr($wkt, 17, (strlen($wkt) - 19));
 
352
        // Seperate each linestring
 
353
        $linestirngs = explode("),(", $multilinestirng);
 
354
        $params[$index]['MULTILINESTRING']['no_of_lines'] = count($linestirngs);
 
355
 
 
356
        $j = 0;
 
357
        foreach ($linestirngs as $linestring) {
 
358
            $points_arr = $this->extractPoints($linestring, null);
 
359
            $no_of_points = count($points_arr);
 
360
            $params[$index]['MULTILINESTRING'][$j]['no_of_points'] = $no_of_points;
 
361
            for ($i = 0; $i < $no_of_points; $i++) {
 
362
                $params[$index]['MULTILINESTRING'][$j][$i]['x'] = $points_arr[$i][0];
 
363
                $params[$index]['MULTILINESTRING'][$j][$i]['y'] = $points_arr[$i][1];
 
364
            }
 
365
            $j++;
 
366
        }
 
367
        return $params;
 
368
    }
 
369
}
 
370
?>