Translated using Weblate (Portuguese)
[phpmyadmin.git] / gis_data_editor.php
blob5a3759a247ae0e69a3fa2fd6128adf033e0748e8
1 <?php
2 /* vim: set expandtab sw=4 ts=4 sts=4: */
3 /**
4 * Editor for Geometry data types.
6 * @package PhpMyAdmin
7 */
9 /**
10 * Escapes special characters if the variable is set.
11 * Returns an empty string otherwise.
13 * @param string $variable variable to be escaped
15 * @return escaped variable
17 function escape($variable)
19 return isset($variable) ? htmlspecialchars($variable) : '';
22 require_once 'libraries/common.inc.php';
23 require_once 'libraries/gis/pma_gis_factory.php';
24 require_once 'libraries/gis/pma_gis_visualization.php';
26 // Get data if any posted
27 $gis_data = array();
28 if (PMA_isValid($_REQUEST['gis_data'], 'array')) {
29 $gis_data = $_REQUEST['gis_data'];
32 $gis_types = array(
33 'POINT',
34 'MULTIPOINT',
35 'LINESTRING',
36 'MULTILINESTRING',
37 'POLYGON',
38 'MULTIPOLYGON',
39 'GEOMETRYCOLLECTION'
42 // Extract type from the initial call and make sure that it's a valid one.
43 // Extract from field's values if availbale, if not use the column type passed.
44 if (! isset($gis_data['gis_type'])) {
45 if (isset($_REQUEST['type']) && $_REQUEST['type'] != '') {
46 $gis_data['gis_type'] = strtoupper($_REQUEST['type']);
48 if (isset($_REQUEST['value']) && trim($_REQUEST['value']) != '') {
49 $start = (substr($_REQUEST['value'], 0, 1) == "'") ? 1 : 0;
50 $gis_data['gis_type'] = substr(
51 $_REQUEST['value'], $start, strpos($_REQUEST['value'], "(") - $start
54 if ((! isset($gis_data['gis_type']))
55 || (! in_array($gis_data['gis_type'], $gis_types))
56 ) {
57 $gis_data['gis_type'] = $gis_types[0];
60 $geom_type = $gis_data['gis_type'];
62 // Generate parameters from value passed.
63 $gis_obj = PMA_GIS_Factory::factory($geom_type);
64 if (isset($_REQUEST['value'])) {
65 $gis_data = array_merge(
66 $gis_data, $gis_obj->generateParams($_REQUEST['value'])
70 // Generate Well Known Text
71 $srid = (isset($gis_data['srid']) && $gis_data['srid'] != '')
72 ? htmlspecialchars($gis_data['srid']) : 0;
73 $wkt = $gis_obj->generateWkt($gis_data, 0);
74 $wkt_with_zero = $gis_obj->generateWkt($gis_data, 0, '0');
75 $result = "'" . $wkt . "'," . $srid;
77 // Generate PNG or SVG based visualization
78 $format = (PMA_USR_BROWSER_AGENT == 'IE' && PMA_USR_BROWSER_VER <= 8)
79 ? 'png' : 'svg';
80 $visualizationSettings = array(
81 'width' => 450,
82 'height' => 300,
83 'spatialColumn' => 'wkt'
85 $data = array(array('wkt' => $wkt_with_zero, 'srid' => $srid));
86 $visualization = PMA_GIS_visualizationResults(
87 $data, $visualizationSettings, $format
89 $open_layers = PMA_GIS_visualizationResults($data, $visualizationSettings, 'ol');
91 // If the call is to update the WKT and visualization make an AJAX response
92 if (isset($_REQUEST['generate']) && $_REQUEST['generate'] == true) {
93 $extra_data = array(
94 'result' => $result,
95 'visualization' => $visualization,
96 'openLayers' => $open_layers,
98 $response = PMA_Response::getInstance();
99 $response->addJSON($extra_data);
100 exit;
103 ob_start();
105 echo '<form id="gis_data_editor_form" action="gis_data_editor.php" method="post">';
106 echo '<input type="hidden" id="pmaThemeImage"'
107 . ' value="' . $GLOBALS['pmaThemeImage'] . '" />';
108 echo '<div id="gis_data_editor">';
110 echo '<h3>';
111 printf(
112 __('Value for the column "%s"'),
113 htmlspecialchars($_REQUEST['field'])
115 echo '</h3>';
117 echo '<input type="hidden" name="field" value="'
118 . htmlspecialchars($_REQUEST['field']) . '" />';
119 // The input field to which the final result should be added
120 // and corresponding null checkbox
121 if (isset($_REQUEST['input_name'])) {
122 echo '<input type="hidden" name="input_name" value="'
123 . htmlspecialchars($_REQUEST['input_name']) . '" />';
125 echo PMA_URL_getHiddenInputs();
127 echo '<!-- Visualization section -->';
128 echo '<div id="placeholder" style="width:450px;height:300px;'
129 . ($srid != 0 ? 'display:none;' : '') . '">';
130 echo $visualization;
131 echo '</div>';
133 echo '<div id="openlayersmap" style="width:450px;height:300px;'
134 . ($srid == 0 ? 'display:none;' : '') . '">';
135 echo '</div>';
137 echo '<div class="choice" style="float:right;clear:right;">';
138 echo '<input type="checkbox" id="choice" value="useBaseLayer"'
139 . ($srid != 0 ? ' checked="checked"' : '') . '/>';
140 echo '<label for="choice">' . __("Use OpenStreetMaps as Base Layer") . '</label>';
141 echo '</div>';
143 echo '<script language="javascript" type="text/javascript">';
144 echo $open_layers;
145 echo '</script>';
146 echo '<!-- End of visualization section -->';
149 echo '<!-- Header section - Inclueds GIS type selector and input field for SRID -->';
150 echo '<div id="gis_data_header">';
151 echo '<select name="gis_data[gis_type]" class="gis_type">';
152 foreach ($gis_types as $gis_type) {
153 echo '<option value="' . $gis_type . '"';
154 if ($geom_type == $gis_type) {
155 echo ' selected="selected"';
157 echo '>' . $gis_type . '</option>';
159 echo '</select>';
160 echo '&nbsp;&nbsp;&nbsp;&nbsp;';
161 /* l10n: Spatial Reference System Identifier */
162 echo '<label for="srid">' . __('SRID:') . '</label>';
163 echo '<input name="gis_data[srid]" type="text" value="' . $srid . '" />';
164 echo '</div>';
165 echo '<!-- End of header section -->';
167 echo '<!-- Data section -->';
168 echo '<div id="gis_data">';
170 $geom_count = 1;
171 if ($geom_type == 'GEOMETRYCOLLECTION') {
172 $geom_count = (isset($gis_data[$geom_type]['geom_count']))
173 ? $gis_data[$geom_type]['geom_count'] : 1;
174 if (isset($gis_data[$geom_type]['add_geom'])) {
175 $geom_count++;
177 echo '<input type="hidden" name="gis_data[GEOMETRYCOLLECTION][geom_count]"'
178 . ' value="' . $geom_count . '" />';
181 for ($a = 0; $a < $geom_count; $a++) {
183 if ($geom_type == 'GEOMETRYCOLLECTION') {
184 echo '<br/><br/>';
185 printf(__('Geometry %d:'), $a + 1);
186 echo '<br/>';
187 if (isset($gis_data[$a]['gis_type'])) {
188 $type = $gis_data[$a]['gis_type'];
189 } else {
190 $type = $gis_types[0];
192 echo '<select name="gis_data[' . $a . '][gis_type]" class="gis_type">';
193 foreach (array_slice($gis_types, 0, 6) as $gis_type) {
194 echo '<option value="' . $gis_type . '"';
195 if ($type == $gis_type) {
196 echo ' selected="selected"';
198 echo '>' . $gis_type . '</option>';
200 echo '</select>';
201 } else {
202 $type = $geom_type;
205 if ($type == 'POINT') {
206 echo '<br/>';
207 echo __('Point:');
208 echo '<label for="x">' . __("X") . '</label>';
209 echo '<input name="gis_data[' . $a . '][POINT][x]" type="text"'
210 . ' value="' . escape($gis_data[$a]['POINT']['x']) . '" />';
211 echo '<label for="y">' . __("Y") . '</label>';
212 echo '<input name="gis_data[' . $a . '][POINT][y]" type="text"'
213 . ' value="' . escape($gis_data[$a]['POINT']['y']) . '" />';
215 } elseif ($type == 'MULTIPOINT' || $type == 'LINESTRING') {
216 $no_of_points = isset($gis_data[$a][$type]['no_of_points'])
217 ? $gis_data[$a][$type]['no_of_points'] : 1;
218 if ($type == 'LINESTRING' && $no_of_points < 2) {
219 $no_of_points = 2;
221 if ($type == 'MULTIPOINT' && $no_of_points < 1) {
222 $no_of_points = 1;
225 if (isset($gis_data[$a][$type]['add_point'])) {
226 $no_of_points++;
228 echo '<input type="hidden" value="' . $no_of_points . '"'
229 . ' name="gis_data[' . $a . '][' . $type . '][no_of_points]" />';
231 for ($i = 0; $i < $no_of_points; $i++) {
232 echo '<br/>';
233 printf(__('Point %d'), $i + 1);
234 echo ': ';
235 echo '<label for="x">' . __("X") . '</label>';
236 echo '<input type="text"'
237 . ' name="gis_data[' . $a . '][' . $type . '][' . $i . '][x]"'
238 . ' value="' . escape($gis_data[$a][$type][$i]['x']) . '" />';
239 echo '<label for="y">' . __("Y") . '</label>';
240 echo '<input type="text"'
241 . ' name="gis_data[' . $a . '][' . $type . '][' . $i . '][y]"'
242 . ' value="' . escape($gis_data[$a][$type][$i]['y']). '" />';
244 echo '<input type="submit"'
245 . ' name="gis_data[' . $a . '][' . $type . '][add_point]"'
246 . ' class="add addPoint" value="' . __("Add a point") . '" />';
248 } elseif ($type == 'MULTILINESTRING' || $type == 'POLYGON') {
249 $no_of_lines = isset($gis_data[$a][$type]['no_of_lines'])
250 ? $gis_data[$a][$type]['no_of_lines'] : 1;
251 if ($no_of_lines < 1) {
252 $no_of_lines = 1;
254 if (isset($gis_data[$a][$type]['add_line'])) {
255 $no_of_lines++;
257 echo '<input type="hidden" value="' . $no_of_lines . '"'
258 . ' name="gis_data[' . $a . '][' . $type . '][no_of_lines]" />';
260 for ($i = 0; $i < $no_of_lines; $i++) {
261 echo '<br/>';
262 if ($type == 'MULTILINESTRING') {
263 printf(__('Linestring %d:'), $i + 1);
264 } else {
265 if ($i == 0) {
266 echo __('Outer ring:');
267 } else {
268 printf(__('Inner ring %d:'), $i);
272 $no_of_points = isset($gis_data[$a][$type][$i]['no_of_points'])
273 ? $gis_data[$a][$type][$i]['no_of_points'] : 2;
274 if ($type == 'MULTILINESTRING' && $no_of_points < 2) {
275 $no_of_points = 2;
277 if ($type == 'POLYGON' && $no_of_points < 4) {
278 $no_of_points = 4;
280 if (isset($gis_data[$a][$type][$i]['add_point'])) {
281 $no_of_points++;
283 echo '<input type="hidden" value="' . $no_of_points . '"'
284 . ' name="gis_data[' . $a . '][' . $type . '][' . $i . '][no_of_points]" />';
286 for ($j = 0; $j < $no_of_points; $j++) {
287 echo('<br/>');
288 printf(__('Point %d'), $j + 1);
289 echo ': ';
290 echo '<label for="x">' . __("X") . '</label>';
291 echo '<input type="text"'
292 . ' name="gis_data[' . $a . '][' . $type . '][' . $i . '][' . $j . '][x]"'
293 . ' value="' . escape($gis_data[$a][$type][$i][$j]['x']) . '" />';
294 echo '<label for="y">' . __("Y") . '</label>';
295 echo '<input type="text"'
296 . ' name="gis_data[' . $a . '][' . $type . '][' . $i . '][' . $j . '][y]"'
297 . ' value="' . escape($gis_data[$a][$type][$i][$j]['x']) . '" />';
299 echo '<input type="submit"'
300 . ' name="gis_data[' . $a . '][' . $type . '][' . $i . '][add_point]"'
301 . ' class="add addPoint" value="' . __("Add a point") . '" />';
303 $caption = ($type == 'MULTILINESTRING')
304 ? __('Add a linestring')
305 : __('Add an inner ring');
306 echo '<br/>';
307 echo '<input type="submit"'
308 . ' name="gis_data[' . $a . '][' . $type . '][add_line]"'
309 . ' class="add addLine" value="' . $caption . '" />';
311 } elseif ($type == 'MULTIPOLYGON') {
312 $no_of_polygons = isset($gis_data[$a][$type]['no_of_polygons'])
313 ? $gis_data[$a][$type]['no_of_polygons'] : 1;
314 if ($no_of_polygons < 1) {
315 $no_of_polygons = 1;
317 if (isset($gis_data[$a][$type]['add_polygon'])) {
318 $no_of_polygons++;
320 echo '<input type="hidden"'
321 . ' name="gis_data[' . $a . '][' . $type . '][no_of_polygons]"'
322 . ' value="' . $no_of_polygons . '" />';
324 for ($k = 0; $k < $no_of_polygons; $k++) {
325 echo '<br/>';
326 printf(__('Polygon %d:'), $k + 1);
327 $no_of_lines = isset($gis_data[$a][$type][$k]['no_of_lines'])
328 ? $gis_data[$a][$type][$k]['no_of_lines'] : 1;
329 if ($no_of_lines < 1) {
330 $no_of_lines = 1;
332 if (isset($gis_data[$a][$type][$k]['add_line'])) {
333 $no_of_lines++;
335 echo '<input type="hidden"'
336 . ' name="gis_data[' . $a . '][' . $type . '][' . $k . '][no_of_lines]"'
337 . ' value="' . $no_of_lines . '" />';
339 for ($i = 0; $i < $no_of_lines; $i++) {
340 echo '<br/><br/>';
341 if ($i == 0) {
342 echo __('Outer ring:');
343 } else {
344 printf(__('Inner ring %d:'), $i);
347 $no_of_points = isset($gis_data[$a][$type][$k][$i]['no_of_points'])
348 ? $gis_data[$a][$type][$k][$i]['no_of_points'] : 4;
349 if ($no_of_points < 4) {
350 $no_of_points = 4;
352 if (isset($gis_data[$a][$type][$k][$i]['add_point'])) {
353 $no_of_points++;
355 echo '<input type="hidden"'
356 . ' name="gis_data[' . $a . '][' . $type . '][' . $k . '][' . $i . '][no_of_points]"'
357 . ' value="' . $no_of_points . '" />';
359 for ($j = 0; $j < $no_of_points; $j++) {
360 echo '<br/>';
361 printf(__('Point %d'), $j + 1);
362 echo ': ';
363 echo '<label for="x">' . __("X") . '</label>';
364 echo '<input type="text"'
365 . ' name="gis_data[' . $a . '][' . $type . '][' . $k . '][' . $i . '][' . $j . '][x]"'
366 . ' value="' . escape($gis_data[$a][$type][$k][$i][$j]['x']). '" />';
367 echo '<label for="y">' . __("Y") . '</label>';
368 echo '<input type="text"'
369 . ' name="gis_data[' . $a . '][' . $type . '][' . $k . '][' . $i . '][' . $j . '][y]"'
370 . ' value="' . escape($gis_data[$a][$type][$k][$i][$j]['y']) . '" />';
372 echo '<input type="submit"'
373 . ' name="gis_data[' . $a . '][' . $type . '][' . $k . '][' . $i . '][add_point]"'
374 . ' class="add addPoint" value="' . __("Add a point") . '" />';
376 echo '<br/>';
377 echo '<input type="submit"'
378 . ' name="gis_data[' . $a . '][' . $type . '][' . $k . '][add_line]"'
379 . ' class="add addLine" value="' . __('Add an inner ring'). '" />';
380 echo '<br/>';
382 echo '<br/>';
383 echo '<input type="submit"'
384 . ' name="gis_data[' . $a . '][' . $type . '][add_polygon]"'
385 . ' class="add addPolygon" value="' . __('Add a polygon') . '" />';
388 if ($geom_type == 'GEOMETRYCOLLECTION') {
389 echo '<br/><br/>';
390 echo '<input type="submit" name="gis_data[GEOMETRYCOLLECTION][add_geom]"'
391 . 'class="add addGeom" value="' . __("Add geometry") . '" />';
393 echo '</div>';
394 echo '<!-- End of data section -->';
396 echo '<br/>';
397 echo '<input type="submit" name="gis_data[save]" value="' . __('Go') . '" />';
399 echo '<div id="gis_data_output">';
400 echo '<h3>' . __('Output') . '</h3>';
401 echo '<p>';
402 echo __(
403 'Choose "GeomFromText" from the "Function" column and paste the'
404 . ' string below into the "Value" field'
406 echo '</p>';
407 echo '<textarea id="gis_data_textarea" cols="95" rows="5">';
408 echo $result;
409 echo '</textarea>';
410 echo '</div>';
412 echo '</div>';
413 echo '</form>';
415 PMA_Response::getInstance()->addJSON('gis_editor', ob_get_contents());
416 ob_end_clean();