2 /* vim: set expandtab sw=4 ts=4 sts=4: */
4 * Editor for Geometry data types.
8 use PMA\libraries\gis\GISFactory
;
9 use PMA\libraries\gis\GISVisualization
;
10 use PMA\libraries\PMA_String
;
13 * Escapes special characters if the variable is set.
14 * Returns an empty string otherwise.
16 * @param string $variable variable to be escaped
18 * @return string escaped variable
20 function escape($variable)
22 return isset($variable) ?
htmlspecialchars($variable) : '';
25 require_once 'libraries/common.inc.php';
27 // Get data if any posted
29 if (PMA_isValid($_REQUEST['gis_data'], 'array')) {
30 $gis_data = $_REQUEST['gis_data'];
43 /** @var String $pmaString */
44 $pmaString = $GLOBALS['PMA_String'];
46 // Extract type from the initial call and make sure that it's a valid one.
47 // Extract from field's values if available, if not use the column type passed.
48 if (! isset($gis_data['gis_type'])) {
49 if (isset($_REQUEST['type']) && $_REQUEST['type'] != '') {
50 $gis_data['gis_type'] = /*overload*/mb_strtoupper($_REQUEST['type']);
52 if (isset($_REQUEST['value']) && trim($_REQUEST['value']) != '') {
53 $start = (substr($_REQUEST['value'], 0, 1) == "'") ?
1 : 0;
54 $gis_data['gis_type'] = /*overload*/mb_substr(
57 /*overload*/mb_strpos($_REQUEST['value'], "(") - $start
60 if ((! isset($gis_data['gis_type']))
61 ||
(! in_array($gis_data['gis_type'], $gis_types))
63 $gis_data['gis_type'] = $gis_types[0];
66 $geom_type = $gis_data['gis_type'];
68 // Generate parameters from value passed.
69 $gis_obj = GISFactory
::factory($geom_type);
70 if (isset($_REQUEST['value'])) {
71 $gis_data = array_merge(
72 $gis_data, $gis_obj->generateParams($_REQUEST['value'])
76 // Generate Well Known Text
77 $srid = (isset($gis_data['srid']) && $gis_data['srid'] != '')
78 ?
htmlspecialchars($gis_data['srid']) : 0;
79 $wkt = $gis_obj->generateWkt($gis_data, 0);
80 $wkt_with_zero = $gis_obj->generateWkt($gis_data, 0, '0');
81 $result = "'" . $wkt . "'," . $srid;
83 // Generate PNG or SVG based visualization
84 $format = (PMA_USR_BROWSER_AGENT
== 'IE' && PMA_USR_BROWSER_VER
<= 8)
86 $visualizationSettings = array(
89 'spatialColumn' => 'wkt'
91 $data = array(array('wkt' => $wkt_with_zero, 'srid' => $srid));
92 $visualization = GISVisualization
::getByData($data, $visualizationSettings)
95 $open_layers = GISVisualization
::getByData($data, $visualizationSettings)
98 // If the call is to update the WKT and visualization make an AJAX response
99 if (isset($_REQUEST['generate']) && $_REQUEST['generate'] == true) {
102 'visualization' => $visualization,
103 'openLayers' => $open_layers,
105 $response = PMA\libraries\Response
::getInstance();
106 $response->addJSON($extra_data);
112 echo '<form id="gis_data_editor_form" action="gis_data_editor.php" method="post">';
113 echo '<input type="hidden" id="pmaThemeImage"'
114 . ' value="' . $GLOBALS['pmaThemeImage'] . '" />';
115 echo '<div id="gis_data_editor">';
119 __('Value for the column "%s"'),
120 htmlspecialchars($_REQUEST['field'])
124 echo '<input type="hidden" name="field" value="'
125 . htmlspecialchars($_REQUEST['field']) . '" />';
126 // The input field to which the final result should be added
127 // and corresponding null checkbox
128 if (isset($_REQUEST['input_name'])) {
129 echo '<input type="hidden" name="input_name" value="'
130 . htmlspecialchars($_REQUEST['input_name']) . '" />';
132 echo PMA_URL_getHiddenInputs();
134 echo '<!-- Visualization section -->';
135 echo '<div id="placeholder" style="width:450px;height:300px;'
136 . ($srid != 0 ?
'display:none;' : '') . '">';
140 echo '<div id="openlayersmap" style="width:450px;height:300px;'
141 . ($srid == 0 ?
'display:none;' : '') . '">';
144 echo '<div class="choice" style="float:right;clear:right;">';
145 echo '<input type="checkbox" id="choice" value="useBaseLayer"'
146 . ($srid != 0 ?
' checked="checked"' : '') . '/>';
147 echo '<label for="choice">' . __("Use OpenStreetMaps as Base Layer") . '</label>';
150 echo '<script language="javascript" type="text/javascript">';
153 echo '<!-- End of visualization section -->';
156 echo '<!-- Header section - Inclueds GIS type selector and input field for SRID -->';
157 echo '<div id="gis_data_header">';
158 echo '<select name="gis_data[gis_type]" class="gis_type">';
159 foreach ($gis_types as $gis_type) {
160 echo '<option value="' . $gis_type . '"';
161 if ($geom_type == $gis_type) {
162 echo ' selected="selected"';
164 echo '>' . $gis_type . '</option>';
167 echo ' ';
168 /* l10n: Spatial Reference System Identifier */
169 echo '<label for="srid">' . __('SRID:') . '</label>';
170 echo '<input name="gis_data[srid]" type="text" value="' . $srid . '" />';
172 echo '<!-- End of header section -->';
174 echo '<!-- Data section -->';
175 echo '<div id="gis_data">';
178 if ($geom_type == 'GEOMETRYCOLLECTION') {
179 $geom_count = (isset($gis_data[$geom_type]['geom_count']))
180 ?
$gis_data[$geom_type]['geom_count'] : 1;
181 if (isset($gis_data[$geom_type]['add_geom'])) {
184 echo '<input type="hidden" name="gis_data[GEOMETRYCOLLECTION][geom_count]"'
185 . ' value="' . $geom_count . '" />';
188 for ($a = 0; $a < $geom_count; $a++
) {
190 if ($geom_type == 'GEOMETRYCOLLECTION') {
192 printf(__('Geometry %d:'), $a +
1);
194 if (isset($gis_data[$a]['gis_type'])) {
195 $type = $gis_data[$a]['gis_type'];
197 $type = $gis_types[0];
199 echo '<select name="gis_data[' . $a . '][gis_type]" class="gis_type">';
200 foreach (array_slice($gis_types, 0, 6) as $gis_type) {
201 echo '<option value="' . $gis_type . '"';
202 if ($type == $gis_type) {
203 echo ' selected="selected"';
205 echo '>' . $gis_type . '</option>';
212 if ($type == 'POINT') {
215 echo '<label for="x">' . __("X") . '</label>';
216 echo '<input name="gis_data[' . $a . '][POINT][x]" type="text"'
217 . ' value="' . escape($gis_data[$a]['POINT']['x']) . '" />';
218 echo '<label for="y">' . __("Y") . '</label>';
219 echo '<input name="gis_data[' . $a . '][POINT][y]" type="text"'
220 . ' value="' . escape($gis_data[$a]['POINT']['y']) . '" />';
222 } elseif ($type == 'MULTIPOINT' ||
$type == 'LINESTRING') {
223 $no_of_points = isset($gis_data[$a][$type]['no_of_points'])
224 ?
$gis_data[$a][$type]['no_of_points'] : 1;
225 if ($type == 'LINESTRING' && $no_of_points < 2) {
228 if ($type == 'MULTIPOINT' && $no_of_points < 1) {
232 if (isset($gis_data[$a][$type]['add_point'])) {
235 echo '<input type="hidden" value="' . $no_of_points . '"'
236 . ' name="gis_data[' . $a . '][' . $type . '][no_of_points]" />';
238 for ($i = 0; $i < $no_of_points; $i++
) {
240 printf(__('Point %d'), $i +
1);
242 echo '<label for="x">' . __("X") . '</label>';
243 echo '<input type="text"'
244 . ' name="gis_data[' . $a . '][' . $type . '][' . $i . '][x]"'
245 . ' value="' . escape($gis_data[$a][$type][$i]['x']) . '" />';
246 echo '<label for="y">' . __("Y") . '</label>';
247 echo '<input type="text"'
248 . ' name="gis_data[' . $a . '][' . $type . '][' . $i . '][y]"'
249 . ' value="' . escape($gis_data[$a][$type][$i]['y']) . '" />';
251 echo '<input type="submit"'
252 . ' name="gis_data[' . $a . '][' . $type . '][add_point]"'
253 . ' class="add addPoint" value="' . __("Add a point") . '" />';
255 } elseif ($type == 'MULTILINESTRING' ||
$type == 'POLYGON') {
256 $no_of_lines = isset($gis_data[$a][$type]['no_of_lines'])
257 ?
$gis_data[$a][$type]['no_of_lines'] : 1;
258 if ($no_of_lines < 1) {
261 if (isset($gis_data[$a][$type]['add_line'])) {
264 echo '<input type="hidden" value="' . $no_of_lines . '"'
265 . ' name="gis_data[' . $a . '][' . $type . '][no_of_lines]" />';
267 for ($i = 0; $i < $no_of_lines; $i++
) {
269 if ($type == 'MULTILINESTRING') {
270 printf(__('Linestring %d:'), $i +
1);
273 echo __('Outer ring:');
275 printf(__('Inner ring %d:'), $i);
279 $no_of_points = isset($gis_data[$a][$type][$i]['no_of_points'])
280 ?
$gis_data[$a][$type][$i]['no_of_points'] : 2;
281 if ($type == 'MULTILINESTRING' && $no_of_points < 2) {
284 if ($type == 'POLYGON' && $no_of_points < 4) {
287 if (isset($gis_data[$a][$type][$i]['add_point'])) {
290 echo '<input type="hidden" value="' . $no_of_points . '"'
291 . ' name="gis_data[' . $a . '][' . $type . '][' . $i
292 . '][no_of_points]" />';
294 for ($j = 0; $j < $no_of_points; $j++
) {
296 printf(__('Point %d'), $j +
1);
298 echo '<label for="x">' . __("X") . '</label>';
299 echo '<input type="text" name="gis_data[' . $a . '][' . $type . ']['
300 . $i . '][' . $j . '][x]" value="'
301 . escape($gis_data[$a][$type][$i][$j]['x']) . '" />';
302 echo '<label for="y">' . __("Y") . '</label>';
303 echo '<input type="text" name="gis_data[' . $a . '][' . $type . ']['
304 . $i . '][' . $j . '][y]"' . ' value="'
305 . escape($gis_data[$a][$type][$i][$j]['y']) . '" />';
307 echo '<input type="submit" name="gis_data[' . $a . '][' . $type . ']['
308 . $i . '][add_point]"'
309 . ' class="add addPoint" value="' . __("Add a point") . '" />';
311 $caption = ($type == 'MULTILINESTRING')
312 ?
__('Add a linestring')
313 : __('Add an inner ring');
315 echo '<input type="submit"'
316 . ' name="gis_data[' . $a . '][' . $type . '][add_line]"'
317 . ' class="add addLine" value="' . $caption . '" />';
319 } elseif ($type == 'MULTIPOLYGON') {
320 $no_of_polygons = isset($gis_data[$a][$type]['no_of_polygons'])
321 ?
$gis_data[$a][$type]['no_of_polygons'] : 1;
322 if ($no_of_polygons < 1) {
325 if (isset($gis_data[$a][$type]['add_polygon'])) {
328 echo '<input type="hidden"'
329 . ' name="gis_data[' . $a . '][' . $type . '][no_of_polygons]"'
330 . ' value="' . $no_of_polygons . '" />';
332 for ($k = 0; $k < $no_of_polygons; $k++
) {
334 printf(__('Polygon %d:'), $k +
1);
335 $no_of_lines = isset($gis_data[$a][$type][$k]['no_of_lines'])
336 ?
$gis_data[$a][$type][$k]['no_of_lines'] : 1;
337 if ($no_of_lines < 1) {
340 if (isset($gis_data[$a][$type][$k]['add_line'])) {
343 echo '<input type="hidden"'
344 . ' name="gis_data[' . $a . '][' . $type . '][' . $k
345 . '][no_of_lines]"' . ' value="' . $no_of_lines . '" />';
347 for ($i = 0; $i < $no_of_lines; $i++
) {
350 echo __('Outer ring:');
352 printf(__('Inner ring %d:'), $i);
355 $no_of_points = isset($gis_data[$a][$type][$k][$i]['no_of_points'])
356 ?
$gis_data[$a][$type][$k][$i]['no_of_points'] : 4;
357 if ($no_of_points < 4) {
360 if (isset($gis_data[$a][$type][$k][$i]['add_point'])) {
363 echo '<input type="hidden"'
364 . ' name="gis_data[' . $a . '][' . $type . '][' . $k . '][' . $i
365 . '][no_of_points]"' . ' value="' . $no_of_points . '" />';
367 for ($j = 0; $j < $no_of_points; $j++
) {
369 printf(__('Point %d'), $j +
1);
371 echo '<label for="x">' . __("X") . '</label>';
372 echo '<input type="text"'
373 . ' name="gis_data[' . $a . '][' . $type . '][' . $k . ']['
374 . $i . '][' . $j . '][x]"'
375 . ' value="' . escape($gis_data[$a][$type][$k][$i][$j]['x'])
377 echo '<label for="y">' . __("Y") . '</label>';
378 echo '<input type="text"'
379 . ' name="gis_data[' . $a . '][' . $type . '][' . $k . ']['
380 . $i . '][' . $j . '][y]"'
381 . ' value="' . escape($gis_data[$a][$type][$k][$i][$j]['y'])
384 echo '<input type="submit"'
385 . ' name="gis_data[' . $a . '][' . $type . '][' . $k . '][' . $i
387 . ' class="add addPoint" value="' . __("Add a point") . '" />';
390 echo '<input type="submit"'
391 . ' name="gis_data[' . $a . '][' . $type . '][' . $k . '][add_line]"'
392 . ' class="add addLine" value="' . __('Add an inner ring') . '" />';
396 echo '<input type="submit"'
397 . ' name="gis_data[' . $a . '][' . $type . '][add_polygon]"'
398 . ' class="add addPolygon" value="' . __('Add a polygon') . '" />';
401 if ($geom_type == 'GEOMETRYCOLLECTION') {
403 echo '<input type="submit" name="gis_data[GEOMETRYCOLLECTION][add_geom]"'
404 . 'class="add addGeom" value="' . __("Add geometry") . '" />';
407 echo '<!-- End of data section -->';
410 echo '<input type="submit" name="gis_data[save]" value="' . __('Go') . '" />';
412 echo '<div id="gis_data_output">';
413 echo '<h3>' . __('Output') . '</h3>';
416 'Choose "GeomFromText" from the "Function" column and paste the'
417 . ' string below into the "Value" field.'
420 echo '<textarea id="gis_data_textarea" cols="95" rows="5">';
428 PMA\libraries\Response
::getInstance()->addJSON('gis_editor', ob_get_contents());