gis_data_editor.php 15.3 KB
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 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434
<?php
/* vim: set expandtab sw=4 ts=4 sts=4: */
/**
 * Editor for Geometry data types.
 *
 * @package PhpMyAdmin
 */

use PhpMyAdmin\Core;
use PhpMyAdmin\Gis\GisFactory;
use PhpMyAdmin\Gis\GisVisualization;
use PhpMyAdmin\Response;
use PhpMyAdmin\Url;

/**
 * Escapes special characters if the variable is set.
 * Returns an empty string otherwise.
 *
 * @param string $variable variable to be escaped
 *
 * @return string escaped variable
 */
function escape($variable)
{
    return isset($variable) ? htmlspecialchars($variable) : '';
}

require_once 'libraries/common.inc.php';

if (! isset($_POST['field'])) {
    PhpMyAdmin\Util::checkParameters(array('field'));
}

// Get data if any posted
$gis_data = array();
if (Core::isValid($_POST['gis_data'], 'array')) {
    $gis_data = $_POST['gis_data'];
}

$gis_types = array(
    'POINT',
    'MULTIPOINT',
    'LINESTRING',
    'MULTILINESTRING',
    'POLYGON',
    'MULTIPOLYGON',
    'GEOMETRYCOLLECTION'
);

// Extract type from the initial call and make sure that it's a valid one.
// Extract from field's values if available, if not use the column type passed.
if (! isset($gis_data['gis_type'])) {
    if (isset($_POST['type']) && $_POST['type'] != '') {
        $gis_data['gis_type'] = mb_strtoupper($_POST['type']);
    }
    if (isset($_POST['value']) && trim($_POST['value']) != '') {
        $start = (substr($_POST['value'], 0, 1) == "'") ? 1 : 0;
        $gis_data['gis_type'] = mb_substr(
            $_POST['value'],
            $start,
            mb_strpos($_POST['value'], "(") - $start
        );
    }
    if ((! isset($gis_data['gis_type']))
        || (! in_array($gis_data['gis_type'], $gis_types))
    ) {
        $gis_data['gis_type'] = $gis_types[0];
    }
}
$geom_type = htmlspecialchars($gis_data['gis_type']);

// Generate parameters from value passed.
$gis_obj = GisFactory::factory($geom_type);
if (isset($_POST['value'])) {
    $gis_data = array_merge(
        $gis_data, $gis_obj->generateParams($_POST['value'])
    );
}

// Generate Well Known Text
$srid = (isset($gis_data['srid']) && $gis_data['srid'] != '')
    ? htmlspecialchars($gis_data['srid']) : 0;
$wkt = $gis_obj->generateWkt($gis_data, 0);
$wkt_with_zero = $gis_obj->generateWkt($gis_data, 0, '0');
$result = "'" . $wkt . "'," . $srid;

// Generate SVG based visualization
$visualizationSettings = array(
    'width' => 450,
    'height' => 300,
    'spatialColumn' => 'wkt'
);
$data = array(array('wkt' => $wkt_with_zero, 'srid' => $srid));
$visualization = GisVisualization::getByData($data, $visualizationSettings)
    ->toImage('svg');

$open_layers = GisVisualization::getByData($data, $visualizationSettings)
    ->asOl();

// If the call is to update the WKT and visualization make an AJAX response
if (isset($_POST['generate']) && $_POST['generate'] == true) {
    $extra_data = array(
        'result'        => $result,
        'visualization' => $visualization,
        'openLayers'    => $open_layers,
    );
    $response = Response::getInstance();
    $response->addJSON($extra_data);
    exit;
}

ob_start();

echo '<form id="gis_data_editor_form" action="gis_data_editor.php" method="post">';
echo '<input type="hidden" id="pmaThemeImage"'
    , ' value="' , $GLOBALS['pmaThemeImage'] , '" />';
echo '<div id="gis_data_editor">';

echo '<h3>';
printf(
    __('Value for the column "%s"'),
    htmlspecialchars($_POST['field'])
);
echo '</h3>';

echo '<input type="hidden" name="field" value="'
    , htmlspecialchars($_POST['field']) , '" />';
// The input field to which the final result should be added
// and corresponding null checkbox
if (isset($_POST['input_name'])) {
    echo '<input type="hidden" name="input_name" value="'
        , htmlspecialchars($_POST['input_name']) , '" />';
}
echo Url::getHiddenInputs();

echo '<!-- Visualization section -->';
echo '<div id="placeholder" '
    , ($srid != 0 ? 'class="hide' : '') , '">';
echo $visualization;
echo '</div>';

echo '<div id="openlayersmap" '
    , ($srid == 0 ? 'class="hide' : '') , '">';
echo '</div>';

echo '<div class="choice floatright">';
echo '<input type="checkbox" id="choice" value="useBaseLayer"'
    , ($srid != 0 ? ' checked="checked"' : '') , '/>';
echo '<label for="choice">' ,  __("Use OpenStreetMaps as Base Layer") , '</label>';
echo '</div>';

echo '<script language="javascript" type="text/javascript">';
echo $open_layers;
echo '</script>';
echo '<!-- End of visualization section -->';


echo '<!-- Header section - Inclueds GIS type selector and input field for SRID -->';
echo '<div id="gis_data_header">';
echo '<select name="gis_data[gis_type]" class="gis_type">';
foreach ($gis_types as $gis_type) {
    echo '<option value="' , $gis_type , '"';
    if ($geom_type == $gis_type) {
        echo ' selected="selected"';
    }
    echo '>' , $gis_type , '</option>';
}
echo '</select>';
echo '&nbsp;&nbsp;&nbsp;&nbsp;';
/* l10n: Spatial Reference System Identifier */
echo '<label for="srid">' ,  __('SRID:') , '</label>';
echo '<input name="gis_data[srid]" type="text" value="' , $srid , '" />';
echo '</div>';
echo '<!-- End of header section -->';

echo '<!-- Data section -->';
echo '<div id="gis_data">';

$geom_count = 1;
if ($geom_type == 'GEOMETRYCOLLECTION') {
    $geom_count = (isset($gis_data[$geom_type]['geom_count']))
        ? intval($gis_data[$geom_type]['geom_count']) : 1;
    if (isset($gis_data[$geom_type]['add_geom'])) {
        $geom_count++;
    }
    echo '<input type="hidden" name="gis_data[GEOMETRYCOLLECTION][geom_count]"'
        , ' value="' , $geom_count , '" />';
}

for ($a = 0; $a < $geom_count; $a++) {
    if (! isset($gis_data[$a])) {
        continue;
    }

    if ($geom_type == 'GEOMETRYCOLLECTION') {
        echo '<br/><br/>';
        printf(__('Geometry %d:'), $a + 1);
        echo '<br/>';
        if (isset($gis_data[$a]['gis_type'])) {
            $type = htmlspecialchars($gis_data[$a]['gis_type']);
        } else {
            $type = $gis_types[0];
        }
        echo '<select name="gis_data[' , $a , '][gis_type]" class="gis_type">';
        foreach (array_slice($gis_types, 0, 6) as $gis_type) {
            echo '<option value="' , $gis_type , '"';
            if ($type == $gis_type) {
                echo ' selected="selected"';
            }
            echo '>' , $gis_type , '</option>';
        }
        echo '</select>';
    } else {
        $type = $geom_type;
    }

    if ($type == 'POINT') {
        echo '<br/>';
        echo __('Point:');
        echo '<label for="x">' , __("X") , '</label>';
        echo '<input name="gis_data[' , $a , '][POINT][x]" type="text"'
            , ' value="' , escape($gis_data[$a]['POINT']['x']) , '" />';
        echo '<label for="y">' , __("Y") , '</label>';
        echo '<input name="gis_data[' , $a , '][POINT][y]" type="text"'
            , ' value="' , escape($gis_data[$a]['POINT']['y']) , '" />';

    } elseif ($type == 'MULTIPOINT' || $type == 'LINESTRING') {
        $no_of_points = isset($gis_data[$a][$type]['no_of_points'])
            ? intval($gis_data[$a][$type]['no_of_points']) : 1;
        if ($type == 'LINESTRING' && $no_of_points < 2) {
            $no_of_points = 2;
        }
        if ($type == 'MULTIPOINT' && $no_of_points < 1) {
            $no_of_points = 1;
        }

        if (isset($gis_data[$a][$type]['add_point'])) {
            $no_of_points++;
        }
        echo '<input type="hidden" value="' , $no_of_points , '"'
            , ' name="gis_data[' , $a , '][' , $type , '][no_of_points]" />';

        for ($i = 0; $i < $no_of_points; $i++) {
            echo '<br/>';
            printf(__('Point %d'), $i + 1);
            echo ': ';
            echo '<label for="x">' ,  __("X") , '</label>';
            echo '<input type="text"'
                , ' name="gis_data[' , $a , '][' , $type , '][' , $i , '][x]"'
                , ' value="' , escape($gis_data[$a][$type][$i]['x']) , '" />';
            echo '<label for="y">' , __("Y") , '</label>';
            echo '<input type="text"'
                , ' name="gis_data[' , $a , '][' , $type , '][' , $i , '][y]"'
                , ' value="' , escape($gis_data[$a][$type][$i]['y']) , '" />';
        }
        echo '<input type="submit"'
            , ' name="gis_data[' , $a , '][' , $type , '][add_point]"'
            , ' class="add addPoint" value="' , __("Add a point") , '" />';

    } elseif ($type == 'MULTILINESTRING' || $type == 'POLYGON') {
        $no_of_lines = isset($gis_data[$a][$type]['no_of_lines'])
            ? intval($gis_data[$a][$type]['no_of_lines']) : 1;
        if ($no_of_lines < 1) {
            $no_of_lines = 1;
        }
        if (isset($gis_data[$a][$type]['add_line'])) {
            $no_of_lines++;
        }
        echo '<input type="hidden" value="' , $no_of_lines , '"'
            , ' name="gis_data[' , $a , '][' , $type , '][no_of_lines]" />';

        for ($i = 0; $i < $no_of_lines; $i++) {
            echo '<br/>';
            if ($type == 'MULTILINESTRING') {
                printf(__('Linestring %d:'), $i + 1);
            } else {
                if ($i == 0) {
                    echo __('Outer ring:');
                } else {
                    printf(__('Inner ring %d:'), $i);
                }
            }

            $no_of_points = isset($gis_data[$a][$type][$i]['no_of_points'])
                ? intval($gis_data[$a][$type][$i]['no_of_points']) : 2;
            if ($type == 'MULTILINESTRING' && $no_of_points < 2) {
                $no_of_points = 2;
            }
            if ($type == 'POLYGON' && $no_of_points < 4) {
                $no_of_points = 4;
            }
            if (isset($gis_data[$a][$type][$i]['add_point'])) {
                $no_of_points++;
            }
            echo '<input type="hidden" value="' , $no_of_points , '"'
                , ' name="gis_data[' , $a , '][' , $type , '][' , $i
                , '][no_of_points]" />';

            for ($j = 0; $j < $no_of_points; $j++) {
                echo('<br/>');
                printf(__('Point %d'), $j + 1);
                echo ': ';
                echo '<label for="x">' ,  __("X") , '</label>';
                echo '<input type="text" name="gis_data[' , $a , '][' , $type . ']['
                    , $i , '][' , $j , '][x]" value="'
                    , escape($gis_data[$a][$type][$i][$j]['x']) , '" />';
                echo '<label for="y">' , __("Y") , '</label>';
                echo '<input type="text" name="gis_data[' , $a , '][' , $type , ']['
                    , $i , '][' , $j , '][y]"' , ' value="'
                    , escape($gis_data[$a][$type][$i][$j]['y']) , '" />';
            }
            echo '<input type="submit" name="gis_data[' , $a , '][' , $type , ']['
                , $i , '][add_point]"'
                , ' class="add addPoint" value="' , __("Add a point") , '" />';
        }
        $caption = ($type == 'MULTILINESTRING')
            ? __('Add a linestring')
            : __('Add an inner ring');
        echo '<br/>';
        echo '<input type="submit"'
            , ' name="gis_data[' , $a , '][' , $type , '][add_line]"'
            , ' class="add addLine" value="' , $caption , '" />';

    } elseif ($type == 'MULTIPOLYGON') {
        $no_of_polygons = isset($gis_data[$a][$type]['no_of_polygons'])
            ? intval($gis_data[$a][$type]['no_of_polygons']) : 1;
        if ($no_of_polygons < 1) {
            $no_of_polygons = 1;
        }
        if (isset($gis_data[$a][$type]['add_polygon'])) {
            $no_of_polygons++;
        }
        echo '<input type="hidden"'
            , ' name="gis_data[' , $a , '][' , $type , '][no_of_polygons]"'
            , ' value="' , $no_of_polygons , '" />';

        for ($k = 0; $k < $no_of_polygons; $k++) {
            echo '<br/>';
            printf(__('Polygon %d:'), $k + 1);
            $no_of_lines = isset($gis_data[$a][$type][$k]['no_of_lines'])
                ? intval($gis_data[$a][$type][$k]['no_of_lines']) : 1;
            if ($no_of_lines < 1) {
                $no_of_lines = 1;
            }
            if (isset($gis_data[$a][$type][$k]['add_line'])) {
                $no_of_lines++;
            }
            echo '<input type="hidden"'
                , ' name="gis_data[' , $a , '][' , $type , '][' , $k
                , '][no_of_lines]"' , ' value="' , $no_of_lines , '" />';

            for ($i = 0; $i < $no_of_lines; $i++) {
                echo '<br/><br/>';
                if ($i == 0) {
                    echo __('Outer ring:');
                } else {
                    printf(__('Inner ring %d:'), $i);
                }

                $no_of_points = isset($gis_data[$a][$type][$k][$i]['no_of_points'])
                    ? intval($gis_data[$a][$type][$k][$i]['no_of_points']) : 4;
                if ($no_of_points < 4) {
                    $no_of_points = 4;
                }
                if (isset($gis_data[$a][$type][$k][$i]['add_point'])) {
                    $no_of_points++;
                }
                echo '<input type="hidden"'
                    , ' name="gis_data[' , $a , '][' , $type , '][' , $k , '][' , $i
                    , '][no_of_points]"' , ' value="' , $no_of_points , '" />';

                for ($j = 0; $j < $no_of_points; $j++) {
                    echo '<br/>';
                    printf(__('Point %d'), $j + 1);
                    echo ': ';
                    echo '<label for="x">' ,  __("X") , '</label>';
                    echo '<input type="text"'
                        , ' name="gis_data[' , $a , '][' , $type , '][' , $k , ']['
                        , $i , '][' , $j , '][x]"'
                        , ' value="' , escape($gis_data[$a][$type][$k][$i][$j]['x'])
                        , '" />';
                    echo '<label for="y">' , __("Y") , '</label>';
                    echo '<input type="text"'
                        , ' name="gis_data[' , $a , '][' , $type , '][' , $k , ']['
                        , $i , '][' , $j , '][y]"'
                        , ' value="' , escape($gis_data[$a][$type][$k][$i][$j]['y'])
                        , '" />';
                }
                echo '<input type="submit"'
                    , ' name="gis_data[' , $a , '][' , $type , '][' , $k , '][' , $i
                    , '][add_point]"'
                    , ' class="add addPoint" value="' , __("Add a point") , '" />';
            }
            echo '<br/>';
            echo '<input type="submit"'
                , ' name="gis_data[' , $a , '][' , $type , '][' , $k , '][add_line]"'
                , ' class="add addLine" value="' , __('Add an inner ring') , '" />';
            echo '<br/>';
        }
        echo '<br/>';
        echo '<input type="submit"'
            , ' name="gis_data[' , $a , '][' , $type , '][add_polygon]"'
            , ' class="add addPolygon" value="' ,  __('Add a polygon') , '" />';
    }
}
if ($geom_type == 'GEOMETRYCOLLECTION') {
    echo '<br/><br/>';
    echo '<input type="submit" name="gis_data[GEOMETRYCOLLECTION][add_geom]"'
        , 'class="add addGeom" value="' , __("Add geometry") , '" />';
}
echo '</div>';
echo '<!-- End of data section -->';

echo '<br/>';
echo '<input type="submit" name="gis_data[save]" value="' , __('Go') , '" />';

echo '<div id="gis_data_output">';
echo '<h3>' , __('Output') , '</h3>';
echo '<p>';
echo __(
    'Choose "GeomFromText" from the "Function" column and paste the'
    . ' string below into the "Value" field.'
);
echo '</p>';
echo '<textarea id="gis_data_textarea" cols="95" rows="5">';
echo htmlspecialchars($result);
echo '</textarea>';
echo '</div>';

echo '</div>';
echo '</form>';

Response::getInstance()->addJSON('gis_editor', ob_get_contents());
ob_end_clean();