https-bitbucket.org-bityvip /phpMyAdmin/libraries/gis/pma_gis_geometrycollection.php

Language PHP Lines 308
MD5 Hash 02aa2f087feccc1ca005a51bde040f3b Estimated Cost $4,255 (why?)
Repository https://bitbucket.org/izubizarreta/https-bitbucket.org-bityvip.git View Raw File View Project SPDX
  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
<?php
/**
 * Handles the visualization of GIS GEOMETRYCOLLECTION objects.
 *
 * @package PhpMyAdmin-GIS
 */
class PMA_GIS_Geometrycollection extends PMA_GIS_Geometry
{
    // Hold the singleton instance of the class
    private static $_instance;

    /**
     * A private constructor; prevents direct creation of object.
     */
    private function __construct()
    {
    }

    /**
     * Returns the singleton.
     *
     * @return the singleton
     */
    public static function singleton()
    {
        if (!isset(self::$_instance)) {
            $class = __CLASS__;
            self::$_instance = new $class;
        }

        return self::$_instance;
    }

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

        // Trim to remove leading 'GEOMETRYCOLLECTION(' and trailing ')'
        $goem_col = substr($spatial, 19, (strlen($spatial) - 20));

        // Split the geometry collection object to get its constituents.
        $sub_parts = $this->_explodeGeomCol($goem_col);

        foreach ($sub_parts as $sub_part) {
            $type_pos = stripos($sub_part, '(');
            $type = substr($sub_part, 0, $type_pos);

            $gis_obj = PMA_GIS_Factory::factory($type);
            if (! $gis_obj) {
                continue;
            }
            $scale_data = $gis_obj->scaleRow($sub_part);

            // Upadate minimum/maximum values for x and y cordinates.
            $c_maxX = (float) $scale_data['maxX'];
            if (! isset($min_max['maxX']) || $c_maxX > $min_max['maxX']) {
                $min_max['maxX'] = $c_maxX;
            }

            $c_minX = (float) $scale_data['minX'];
            if (! isset($min_max['minX']) || $c_minX < $min_max['minX']) {
                $min_max['minX'] = $c_minX;
            }

            $c_maxY = (float) $scale_data['maxY'];
            if (! isset($min_max['maxY']) || $c_maxY > $min_max['maxY']) {
                $min_max['maxY'] = $c_maxY;
            }

            $c_minY = (float) $scale_data['minY'];
            if (! isset($min_max['minY']) || $c_minY < $min_max['minY']) {
                $min_max['minY'] = $c_minY;
            }
        }
        return $min_max;
    }

    /**
     * Adds to the PNG image object, the data related to a row in the GIS dataset.
     *
     * @param string $spatial    GIS GEOMETRYCOLLECTION object
     * @param string $label      Label for the GIS GEOMETRYCOLLECTION object
     * @param string $color      Color for the GIS GEOMETRYCOLLECTION object
     * @param array  $scale_data Array containing data related to scaling
     * @param image  $image      Image object
     *
     * @return the modified image object
     */
    public function prepareRowAsPng($spatial, $label, $color, $scale_data, $image)
    {
        // Trim to remove leading 'GEOMETRYCOLLECTION(' and trailing ')'
        $goem_col = substr($spatial, 19, (strlen($spatial) - 20));
        // Split the geometry collection object to get its constituents.
        $sub_parts = $this->_explodeGeomCol($goem_col);

        foreach ($sub_parts as $sub_part) {
            $type_pos = stripos($sub_part, '(');
            $type = substr($sub_part, 0, $type_pos);

            $gis_obj = PMA_GIS_Factory::factory($type);
            if (! $gis_obj) {
                continue;
            }
            $image = $gis_obj->prepareRowAsPng($sub_part, $label, $color, $scale_data, $image);
        }
        return $image;
    }

    /**
     * Adds to the TCPDF instance, the data related to a row in the GIS dataset.
     *
     * @param string $spatial    GIS GEOMETRYCOLLECTION object
     * @param string $label      Label for the GIS GEOMETRYCOLLECTION object
     * @param string $color      Color for the GIS GEOMETRYCOLLECTION object
     * @param array  $scale_data Array containing data related to scaling
     * @param image  $pdf        TCPDF instance
     *
     * @return the modified TCPDF instance
     */
    public function prepareRowAsPdf($spatial, $label, $color, $scale_data, $pdf)
    {
        // Trim to remove leading 'GEOMETRYCOLLECTION(' and trailing ')'
        $goem_col = substr($spatial, 19, (strlen($spatial) - 20));
        // Split the geometry collection object to get its constituents.
        $sub_parts = $this->_explodeGeomCol($goem_col);

        foreach ($sub_parts as $sub_part) {
            $type_pos = stripos($sub_part, '(');
            $type = substr($sub_part, 0, $type_pos);

            $gis_obj = PMA_GIS_Factory::factory($type);
            if (! $gis_obj) {
                continue;
            }
            $pdf = $gis_obj->prepareRowAsPdf($sub_part, $label, $color, $scale_data, $pdf);
        }
        return $pdf;
    }

    /**
     * Prepares and returns the code related to a row in the GIS dataset as SVG.
     *
     * @param string $spatial    GIS GEOMETRYCOLLECTION object
     * @param string $label      Label for the GIS GEOMETRYCOLLECTION object
     * @param string $color      Color for the GIS GEOMETRYCOLLECTION object
     * @param array  $scale_data Array containing data related to scaling
     *
     * @return the code related to a row in the GIS dataset
     */
    public function prepareRowAsSvg($spatial, $label, $color, $scale_data)
    {
        $row = '';

        // Trim to remove leading 'GEOMETRYCOLLECTION(' and trailing ')'
        $goem_col = substr($spatial, 19, (strlen($spatial) - 20));
        // Split the geometry collection object to get its constituents.
        $sub_parts = $this->_explodeGeomCol($goem_col);

        foreach ($sub_parts as $sub_part) {
            $type_pos = stripos($sub_part, '(');
            $type = substr($sub_part, 0, $type_pos);

            $gis_obj = PMA_GIS_Factory::factory($type);
            if (! $gis_obj) {
                continue;
            }
            $row .= $gis_obj->prepareRowAsSvg($sub_part, $label, $color, $scale_data);
        }
        return $row;
    }

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

        // Trim to remove leading 'GEOMETRYCOLLECTION(' and trailing ')'
        $goem_col = substr($spatial, 19, (strlen($spatial) - 20));
        // Split the geometry collection object to get its constituents.
        $sub_parts = $this->_explodeGeomCol($goem_col);

        foreach ($sub_parts as $sub_part) {
            $type_pos = stripos($sub_part, '(');
            $type = substr($sub_part, 0, $type_pos);

            $gis_obj = PMA_GIS_Factory::factory($type);
            if (! $gis_obj) {
                continue;
            }
            $row .= $gis_obj->prepareRowAsOl($sub_part, $srid, $label, $color, $scale_data);
        }
        return $row;
    }

    /**
     * Split the GEOMETRYCOLLECTION object and get its constituents.
     *
     * @param string $goem_col Geometry collection string
     *
     * @return the constituents of the geometry collection object
     */
    private function _explodeGeomCol($goem_col)
    {
        $sub_parts = array();
        $br_count = 0;
        $start = 0;
        $count = 0;
        foreach (str_split($goem_col) as $char) {
            if ($char == '(') {
                $br_count++;
            } elseif ($char == ')') {
                $br_count--;
                if ($br_count == 0) {
                    $sub_parts[] = substr($goem_col, $start, ($count + 1 - $start));
                    $start = $count + 2;
                }
            }
            $count++;
        }
        return $sub_parts;
    }

    /**
     * Generate 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 WKT with the set of parameters passed by the GIS editor
     */
    public function generateWkt($gis_data, $index, $empty = '')
    {
        $geom_count = (isset($gis_data['GEOMETRYCOLLECTION']['geom_count']))
            ? $gis_data['GEOMETRYCOLLECTION']['geom_count'] : 1;
        $wkt = 'GEOMETRYCOLLECTION(';
        for ($i = 0; $i < $geom_count; $i++) {
            if (isset($gis_data[$i]['gis_type'])) {
                $type = $gis_data[$i]['gis_type'];
                $gis_obj = PMA_GIS_Factory::factory($type);
                if (! $gis_obj) {
                    continue;
                }
                $wkt .= $gis_obj->generateWkt($gis_data, $i, $empty) . ',';
            }
        }
        if (isset($gis_data[0]['gis_type'])) {
            $wkt = substr($wkt, 0, strlen($wkt) - 1);
        }
        $wkt .= ')';
        return $wkt;
    }

    /** Generate parameters for the GIS data editor from the value of the GIS column.
     *
     * @param string $value of the GIS column
     * @param index  $index of the geometry
     *
     * @return  parameters for the GIS data editor from the value of the GIS column
     */
    public function generateParams($value)
    {
        $params = array();
        $data = PMA_GIS_Geometry::generateParams($value);
        $params['srid'] = $data['srid'];
        $wkt = $data['wkt'];

        // Trim to remove leading 'GEOMETRYCOLLECTION(' and trailing ')'
        $goem_col = substr($wkt, 19, (strlen($wkt) - 20));
        // Split the geometry collection object to get its constituents.
        $sub_parts = $this->_explodeGeomCol($goem_col);
        $params['GEOMETRYCOLLECTION']['geom_count'] = count($sub_parts);

        $i = 0;
        foreach ($sub_parts as $sub_part) {
            $type_pos = stripos($sub_part, '(');
            $type = substr($sub_part, 0, $type_pos);

            $gis_obj = PMA_GIS_Factory::factory($type);
            if (! $gis_obj) {
                continue;
            }
            $params = array_merge($params, $gis_obj->generateParams($sub_part, $i));
            $i++;
        }
        return $params;
    }
}
?>
Back to Top