Welcome to mirror list, hosted at ThFree Co, Russian Federation.

GIS_Geometry.class.php « gis « libraries - github.com/phpmyadmin/phpmyadmin.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: b5b7b789527bd1b785f9e386aa71374f5cefd0dd (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
<?php
/* vim: set expandtab sw=4 ts=4 sts=4: */
/**
 * Base class for all GIS data type classes
 *
 * @package PhpMyAdmin-GIS
 */

if (! defined('PHPMYADMIN')) {
    exit;
}

/**
 * Base class for all GIS data type classes.
 *
 * @package PhpMyAdmin-GIS
 */
abstract class PMA_GIS_Geometry
{
    /**
     * Prepares and returns the code related to a row in the GIS dataset as SVG.
     *
     * @param string $spatial    GIS data object
     * @param string $label      label for the GIS data object
     * @param string $color      color for the GIS data object
     * @param array  $scale_data data related to scaling
     *
     * @return string the code related to a row in the GIS dataset
     * @access public
     */
    public abstract function prepareRowAsSvg($spatial, $label, $color, $scale_data);

    /**
     * Adds to the PNG image object, the data related to a row in the GIS dataset.
     *
     * @param string $spatial    GIS data object
     * @param string $label      label for the GIS data object
     * @param string $color      color for the GIS data object
     * @param array  $scale_data array containing data related to scaling
     * @param object $image      image object
     *
     * @return object the modified image object
     * @access public
     */
    public abstract function prepareRowAsPng($spatial, $label, $color,
        $scale_data, $image
    );

    /**
     * Adds to the TCPDF instance, the data related to a row in the GIS dataset.
     *
     * @param string $spatial    GIS data object
     * @param string $label      label for the GIS data object
     * @param string $color      color for the GIS data object
     * @param array  $scale_data array containing data related to scaling
     * @param TCPDF  $pdf        TCPDF instance
     *
     * @return TCPDF the modified TCPDF instance
     * @access public
     */
    public abstract function prepareRowAsPdf($spatial, $label, $color,
        $scale_data, $pdf
    );

    /**
     * Prepares the JavaScript related to a row in the GIS dataset
     * to visualize it with OpenLayers.
     *
     * @param string $spatial    GIS data object
     * @param int    $srid       spatial reference ID
     * @param string $label      label for the GIS data object
     * @param string $color      color for the GIS data object
     * @param array  $scale_data array containing data related to scaling
     *
     * @return string the JavaScript related to a row in the GIS dataset
     * @access public
     */
    public abstract function prepareRowAsOl($spatial, $srid, $label,
        $color, $scale_data
    );

    /**
     * Scales each row.
     *
     * @param string $spatial spatial data of a row
     *
     * @return array array containing the min, max values for x and y coordinates
     * @access public
     */
    public abstract function scaleRow($spatial);

    /**
     * Generates the WKT with the set of parameters passed by the GIS editor.
     *
     * @param array  $gis_data GIS data
     * @param int    $index    index into the parameter object
     * @param string $empty    value for empty points
     *
     * @return string WKT with the set of parameters passed by the GIS editor
     * @access public
     */
    public abstract function generateWkt($gis_data, $index, $empty = '');

    /**
     * Returns OpenLayers.Bounds object that correspond to the bounds of GIS data.
     *
     * @param string $srid       spatial reference ID
     * @param array  $scale_data data related to scaling
     *
     * @return string OpenLayers.Bounds object that
     *                correspond to the bounds of GIS data
     * @access protected
     */
    protected function getBoundsForOl($srid, $scale_data)
    {
        return 'bound = new OpenLayers.Bounds(); '
            . 'bound.extend(new OpenLayers.LonLat('
            . $scale_data['minX'] . ', ' . $scale_data['minY']
            . ').transform(new OpenLayers.Projection("EPSG:'
            . $srid . '"), map.getProjectionObject())); '
            . 'bound.extend(new OpenLayers.LonLat('
            . $scale_data['maxX'] . ', ' . $scale_data['maxY']
            . ').transform(new OpenLayers.Projection("EPSG:'
            . $srid . '"), map.getProjectionObject()));';
    }

    /**
     * Updates the min, max values with the given point set.
     *
     * @param string $point_set point set
     * @param array  $min_max   existing min, max values
     *
     * @return array the updated min, max values
     * @access protected
     */
    protected function setMinMax($point_set, $min_max)
    {
        // Separate each point
        $points = explode(",", $point_set);

        foreach ($points as $point) {
            // Extract coordinates of the point
            $cordinates = explode(" ", $point);

            $x = (float) $cordinates[0];
            if (! isset($min_max['maxX']) || $x > $min_max['maxX']) {
                $min_max['maxX'] = $x;
            }
            if (! isset($min_max['minX']) || $x < $min_max['minX']) {
                $min_max['minX'] = $x;
            }
            $y = (float) $cordinates[1];
            if (! isset($min_max['maxY']) || $y > $min_max['maxY']) {
                $min_max['maxY'] = $y;
            }
            if (! isset($min_max['minY']) || $y < $min_max['minY']) {
                $min_max['minY'] = $y;
            }
        }
        return $min_max;
    }

    /**
     * Generates parameters for the GIS data editor from the value of the GIS column.
     * This method performs common work.
     * More specific work is performed by each of the geom classes.
     *
     * @param string $value value of the GIS column
     *
     * @return array parameters for the GIS editor from the value of the GIS column
     * @access protected
     */
    protected function generateParams($value)
    {
        $geom_types = '(POINT|MULTIPOINT|LINESTRING|MULTILINESTRING'
            . '|POLYGON|MULTIPOLYGON|GEOMETRYCOLLECTION)';
        $srid = 0;
        $wkt = '';

        if (preg_match("/^'" . $geom_types . "\(.*\)',[0-9]*$/i", $value)) {
            $last_comma = /*overload*/mb_strripos($value, ",");
            $srid = trim(/*overload*/mb_substr($value, $last_comma + 1));
            $wkt = trim(/*overload*/mb_substr($value, 1, $last_comma - 2));
        } elseif (preg_match("/^" . $geom_types . "\(.*\)$/i", $value)) {
            $wkt = $value;
        }
        return array('srid' => $srid, 'wkt' => $wkt);
    }

    /**
     * Extracts points, scales and returns them as an array.
     *
     * @param string  $point_set  string of comma separated points
     * @param array   $scale_data data related to scaling
     * @param boolean $linear     if true, as a 1D array, else as a 2D array
     *
     * @return array scaled points
     * @access protected
     */
    protected function extractPoints($point_set, $scale_data, $linear = false)
    {
        $points_arr = array();

        // Separate each point
        $points = explode(",", $point_set);

        foreach ($points as $point) {
            // Extract coordinates of the point
            $cordinates = explode(" ", $point);

            if (isset($cordinates[0]) && trim($cordinates[0]) != ''
                && isset($cordinates[1]) && trim($cordinates[1]) != ''
            ) {
                if ($scale_data != null) {
                    $x = ($cordinates[0] - $scale_data['x']) * $scale_data['scale'];
                    $y = $scale_data['height']
                        - ($cordinates[1] - $scale_data['y']) * $scale_data['scale'];
                } else {
                    $x = trim($cordinates[0]);
                    $y = trim($cordinates[1]);
                }
            } else {
                $x = '';
                $y = '';
            }

            if (! $linear) {
                $points_arr[] = array($x, $y);
            } else {
                $points_arr[] = $x;
                $points_arr[] = $y;
            }
        }

        return $points_arr;
    }

    /**
     * Generates JavaScript for adding an array of polygons to OpenLayers.
     *
     * @param array  $polygons x and y coordinates for each polygon
     * @param string $srid     spatial reference id
     *
     * @return string JavaScript for adding an array of polygons to OpenLayers
     * @access protected
     */
    protected function getPolygonArrayForOpenLayers($polygons, $srid)
    {
        $ol_array = 'new Array(';
        foreach ($polygons as $polygon) {
            $rings = explode("),(", $polygon);
            $ol_array .= $this->getPolygonForOpenLayers($rings, $srid) . ', ';
        }

        $ol_array = /*overload*/mb_substr(
            $ol_array,
            0,
            /*overload*/mb_strlen($ol_array) - 2
        );
        $ol_array .= ')';

        return $ol_array;
    }

    /**
     * Generates JavaScript for adding points for OpenLayers polygon.
     *
     * @param array  $polygon x and y coordinates for each line
     * @param string $srid    spatial reference id
     *
     * @return string JavaScript for adding points for OpenLayers polygon
     * @access protected
     */
    protected function getPolygonForOpenLayers($polygon, $srid)
    {
        return 'new OpenLayers.Geometry.Polygon('
            . $this->getLineArrayForOpenLayers($polygon, $srid, false)
            . ')';
    }

    /**
     * Generates JavaScript for adding an array of LineString
     * or LineRing to OpenLayers.
     *
     * @param array  $lines          x and y coordinates for each line
     * @param string $srid           spatial reference id
     * @param bool   $is_line_string whether it's an array of LineString
     *
     * @return string JavaScript for adding an array of LineString
     *                or LineRing to OpenLayers
     * @access protected
     */
    protected function getLineArrayForOpenLayers($lines, $srid,
        $is_line_string = true
    ) {
        $ol_array = 'new Array(';
        foreach ($lines as $line) {
            $points_arr = $this->extractPoints($line, null);
            $ol_array .= $this->getLineForOpenLayers(
                $points_arr, $srid, $is_line_string
            );
            $ol_array .= ', ';
        }

        $ol_array = /*overload*/mb_substr(
            $ol_array,
            0,
            /*overload*/mb_strlen($ol_array) - 2
        );
        $ol_array .= ')';

        return $ol_array;
    }

    /**
     * Generates JavaScript for adding a LineString or LineRing to OpenLayers.
     *
     * @param array  $points_arr     x and y coordinates for each point
     * @param string $srid           spatial reference id
     * @param bool   $is_line_string whether it's a LineString
     *
     * @return string JavaScript for adding a LineString or LineRing to OpenLayers
     * @access protected
     */
    protected function getLineForOpenLayers($points_arr, $srid,
        $is_line_string = true
    ) {
        return 'new OpenLayers.Geometry.'
            . ($is_line_string ? 'LineString' : 'LinearRing') . '('
            . $this->getPointsArrayForOpenLayers($points_arr, $srid)
            . ')';
    }

    /**
     * Generates JavaScript for adding an array of points to OpenLayers.
     *
     * @param array  $points_arr x and y coordinates for each point
     * @param string $srid       spatial reference id
     *
     * @return string JavaScript for adding an array of points to OpenLayers
     * @access protected
     */
    protected function getPointsArrayForOpenLayers($points_arr, $srid)
    {
        $ol_array = 'new Array(';
        foreach ($points_arr as $point) {
            $ol_array .= $this->getPointForOpenLayers($point, $srid) . ', ';
        }

        $ol_array = /*overload*/mb_substr(
            $ol_array,
            0,
            /*overload*/mb_strlen($ol_array) - 2
        );
        $ol_array .= ')';

        return $ol_array;
    }

    /**
     * Generates JavaScript for adding a point to OpenLayers.
     *
     * @param array  $point array containing the x and y coordinates of the point
     * @param string $srid  spatial reference id
     *
     * @return string JavaScript for adding points to OpenLayers
     * @access protected
     */
    protected function getPointForOpenLayers($point, $srid)
    {
        return '(new OpenLayers.Geometry.Point(' . $point[0] . ',' . $point[1] . '))'
            . '.transform(new OpenLayers.Projection("EPSG:'
            . $srid . '"), map.getProjectionObject())';
    }
}
?>