Merge remote-tracking branch 'origin/master'
[phpmyadmin.git] / gis_data_editor.php
blobd38db8d319ff2ea26683e19c549aaedacaf900a6
1 <?php
2 /* vim: set expandtab sw=4 ts=4 sts=4: */
3 /**
4 * Editor for Geometry data types.
6 * @package PhpMyAdmin
7 */
8 use PMA\libraries\gis\GISFactory;
9 use PMA\libraries\gis\GISVisualization;
11 /**
12 * Escapes special characters if the variable is set.
13 * Returns an empty string otherwise.
15 * @param string $variable variable to be escaped
17 * @return string escaped variable
19 function escape($variable)
21 return isset($variable) ? htmlspecialchars($variable) : '';
24 require_once 'libraries/common.inc.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 available, 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'] = mb_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'] = mb_substr(
51 $_REQUEST['value'],
52 $start,
53 mb_strpos($_REQUEST['value'], "(") - $start
56 if ((! isset($gis_data['gis_type']))
57 || (! in_array($gis_data['gis_type'], $gis_types))
58 ) {
59 $gis_data['gis_type'] = $gis_types[0];
62 $geom_type = $gis_data['gis_type'];
64 // Generate parameters from value passed.
65 $gis_obj = GISFactory::factory($geom_type);
66 if (isset($_REQUEST['value'])) {
67 $gis_data = array_merge(
68 $gis_data, $gis_obj->generateParams($_REQUEST['value'])
72 // Generate Well Known Text
73 $srid = (isset($gis_data['srid']) && $gis_data['srid'] != '')
74 ? htmlspecialchars($gis_data['srid']) : 0;
75 $wkt = $gis_obj->generateWkt($gis_data, 0);
76 $wkt_with_zero = $gis_obj->generateWkt($gis_data, 0, '0');
77 $result = "'" . $wkt . "'," . $srid;
79 // Generate SVG based visualization
80 $visualizationSettings = array(
81 'width' => 450,
82 'height' => 300,
83 'spatialColumn' => 'wkt'
85 $data = array(array('wkt' => $wkt_with_zero, 'srid' => $srid));
86 $visualization = GISVisualization::getByData($data, $visualizationSettings)
87 ->toImage('svg');
89 $open_layers = GISVisualization::getByData($data, $visualizationSettings)
90 ->asOl();
92 // If the call is to update the WKT and visualization make an AJAX response
93 if (isset($_REQUEST['generate']) && $_REQUEST['generate'] == true) {
94 $extra_data = array(
95 'result' => $result,
96 'visualization' => $visualization,
97 'openLayers' => $open_layers,
99 $response = PMA\libraries\Response::getInstance();
100 $response->addJSON($extra_data);
101 exit;
104 ob_start();
106 echo '<form id="gis_data_editor_form" action="gis_data_editor.php" method="post">';
107 echo '<input type="hidden" id="pmaThemeImage"'
108 , ' value="' , $GLOBALS['pmaThemeImage'] , '" />';
109 echo '<div id="gis_data_editor">';
111 echo '<h3>';
112 printf(
113 __('Value for the column "%s"'),
114 htmlspecialchars($_REQUEST['field'])
116 echo '</h3>';
118 echo '<input type="hidden" name="field" value="'
119 , htmlspecialchars($_REQUEST['field']) , '" />';
120 // The input field to which the final result should be added
121 // and corresponding null checkbox
122 if (isset($_REQUEST['input_name'])) {
123 echo '<input type="hidden" name="input_name" value="'
124 , htmlspecialchars($_REQUEST['input_name']) , '" />';
126 echo PMA_URL_getHiddenInputs();
128 echo '<!-- Visualization section -->';
129 echo '<div id="placeholder" style="width:450px;height:300px;'
130 , ($srid != 0 ? 'display:none;' : '') , '">';
131 echo $visualization;
132 echo '</div>';
134 echo '<div id="openlayersmap" style="width:450px;height:300px;'
135 , ($srid == 0 ? 'display:none;' : '') , '">';
136 echo '</div>';
138 echo '<div class="choice" style="float:right;clear:right;">';
139 echo '<input type="checkbox" id="choice" value="useBaseLayer"'
140 , ($srid != 0 ? ' checked="checked"' : '') , '/>';
141 echo '<label for="choice">' , __("Use OpenStreetMaps as Base Layer") , '</label>';
142 echo '</div>';
144 echo '<script language="javascript" type="text/javascript">';
145 echo $open_layers;
146 echo '</script>';
147 echo '<!-- End of visualization section -->';
150 echo '<!-- Header section - Inclueds GIS type selector and input field for SRID -->';
151 echo '<div id="gis_data_header">';
152 echo '<select name="gis_data[gis_type]" class="gis_type">';
153 foreach ($gis_types as $gis_type) {
154 echo '<option value="' , $gis_type , '"';
155 if ($geom_type == $gis_type) {
156 echo ' selected="selected"';
158 echo '>' , $gis_type , '</option>';
160 echo '</select>';
161 echo '&nbsp;&nbsp;&nbsp;&nbsp;';
162 /* l10n: Spatial Reference System Identifier */
163 echo '<label for="srid">' , __('SRID:') , '</label>';
164 echo '<input name="gis_data[srid]" type="text" value="' , $srid , '" />';
165 echo '</div>';
166 echo '<!-- End of header section -->';
168 echo '<!-- Data section -->';
169 echo '<div id="gis_data">';
171 $geom_count = 1;
172 if ($geom_type == 'GEOMETRYCOLLECTION') {
173 $geom_count = (isset($gis_data[$geom_type]['geom_count']))
174 ? $gis_data[$geom_type]['geom_count'] : 1;
175 if (isset($gis_data[$geom_type]['add_geom'])) {
176 $geom_count++;
178 echo '<input type="hidden" name="gis_data[GEOMETRYCOLLECTION][geom_count]"'
179 , ' value="' , $geom_count , '" />';
182 for ($a = 0; $a < $geom_count; $a++) {
184 if ($geom_type == 'GEOMETRYCOLLECTION') {
185 echo '<br/><br/>';
186 printf(__('Geometry %d:'), $a + 1);
187 echo '<br/>';
188 if (isset($gis_data[$a]['gis_type'])) {
189 $type = $gis_data[$a]['gis_type'];
190 } else {
191 $type = $gis_types[0];
193 echo '<select name="gis_data[' , $a , '][gis_type]" class="gis_type">';
194 foreach (array_slice($gis_types, 0, 6) as $gis_type) {
195 echo '<option value="' , $gis_type , '"';
196 if ($type == $gis_type) {
197 echo ' selected="selected"';
199 echo '>' , $gis_type , '</option>';
201 echo '</select>';
202 } else {
203 $type = $geom_type;
206 if ($type == 'POINT') {
207 echo '<br/>';
208 echo __('Point:');
209 echo '<label for="x">' , __("X") , '</label>';
210 echo '<input name="gis_data[' , $a , '][POINT][x]" type="text"'
211 , ' value="' , escape($gis_data[$a]['POINT']['x']) , '" />';
212 echo '<label for="y">' , __("Y") , '</label>';
213 echo '<input name="gis_data[' , $a , '][POINT][y]" type="text"'
214 , ' value="' , escape($gis_data[$a]['POINT']['y']) , '" />';
216 } elseif ($type == 'MULTIPOINT' || $type == 'LINESTRING') {
217 $no_of_points = isset($gis_data[$a][$type]['no_of_points'])
218 ? $gis_data[$a][$type]['no_of_points'] : 1;
219 if ($type == 'LINESTRING' && $no_of_points < 2) {
220 $no_of_points = 2;
222 if ($type == 'MULTIPOINT' && $no_of_points < 1) {
223 $no_of_points = 1;
226 if (isset($gis_data[$a][$type]['add_point'])) {
227 $no_of_points++;
229 echo '<input type="hidden" value="' , $no_of_points , '"'
230 , ' name="gis_data[' , $a , '][' , $type , '][no_of_points]" />';
232 for ($i = 0; $i < $no_of_points; $i++) {
233 echo '<br/>';
234 printf(__('Point %d'), $i + 1);
235 echo ': ';
236 echo '<label for="x">' , __("X") , '</label>';
237 echo '<input type="text"'
238 , ' name="gis_data[' , $a , '][' , $type , '][' , $i , '][x]"'
239 , ' value="' , escape($gis_data[$a][$type][$i]['x']) , '" />';
240 echo '<label for="y">' , __("Y") , '</label>';
241 echo '<input type="text"'
242 , ' name="gis_data[' , $a , '][' , $type , '][' , $i , '][y]"'
243 , ' value="' , escape($gis_data[$a][$type][$i]['y']) , '" />';
245 echo '<input type="submit"'
246 , ' name="gis_data[' , $a , '][' , $type , '][add_point]"'
247 , ' class="add addPoint" value="' , __("Add a point") , '" />';
249 } elseif ($type == 'MULTILINESTRING' || $type == 'POLYGON') {
250 $no_of_lines = isset($gis_data[$a][$type]['no_of_lines'])
251 ? $gis_data[$a][$type]['no_of_lines'] : 1;
252 if ($no_of_lines < 1) {
253 $no_of_lines = 1;
255 if (isset($gis_data[$a][$type]['add_line'])) {
256 $no_of_lines++;
258 echo '<input type="hidden" value="' , $no_of_lines , '"'
259 , ' name="gis_data[' , $a , '][' , $type , '][no_of_lines]" />';
261 for ($i = 0; $i < $no_of_lines; $i++) {
262 echo '<br/>';
263 if ($type == 'MULTILINESTRING') {
264 printf(__('Linestring %d:'), $i + 1);
265 } else {
266 if ($i == 0) {
267 echo __('Outer ring:');
268 } else {
269 printf(__('Inner ring %d:'), $i);
273 $no_of_points = isset($gis_data[$a][$type][$i]['no_of_points'])
274 ? $gis_data[$a][$type][$i]['no_of_points'] : 2;
275 if ($type == 'MULTILINESTRING' && $no_of_points < 2) {
276 $no_of_points = 2;
278 if ($type == 'POLYGON' && $no_of_points < 4) {
279 $no_of_points = 4;
281 if (isset($gis_data[$a][$type][$i]['add_point'])) {
282 $no_of_points++;
284 echo '<input type="hidden" value="' , $no_of_points , '"'
285 , ' name="gis_data[' , $a , '][' , $type , '][' , $i
286 , '][no_of_points]" />';
288 for ($j = 0; $j < $no_of_points; $j++) {
289 echo('<br/>');
290 printf(__('Point %d'), $j + 1);
291 echo ': ';
292 echo '<label for="x">' , __("X") , '</label>';
293 echo '<input type="text" name="gis_data[' , $a , '][' , $type . ']['
294 , $i , '][' , $j , '][x]" value="'
295 , escape($gis_data[$a][$type][$i][$j]['x']) , '" />';
296 echo '<label for="y">' , __("Y") , '</label>';
297 echo '<input type="text" name="gis_data[' , $a , '][' , $type , ']['
298 , $i , '][' , $j , '][y]"' , ' value="'
299 , escape($gis_data[$a][$type][$i][$j]['y']) , '" />';
301 echo '<input type="submit" name="gis_data[' , $a , '][' , $type , ']['
302 , $i , '][add_point]"'
303 , ' class="add addPoint" value="' , __("Add a point") , '" />';
305 $caption = ($type == 'MULTILINESTRING')
306 ? __('Add a linestring')
307 : __('Add an inner ring');
308 echo '<br/>';
309 echo '<input type="submit"'
310 , ' name="gis_data[' , $a , '][' , $type , '][add_line]"'
311 , ' class="add addLine" value="' , $caption , '" />';
313 } elseif ($type == 'MULTIPOLYGON') {
314 $no_of_polygons = isset($gis_data[$a][$type]['no_of_polygons'])
315 ? $gis_data[$a][$type]['no_of_polygons'] : 1;
316 if ($no_of_polygons < 1) {
317 $no_of_polygons = 1;
319 if (isset($gis_data[$a][$type]['add_polygon'])) {
320 $no_of_polygons++;
322 echo '<input type="hidden"'
323 , ' name="gis_data[' , $a , '][' , $type , '][no_of_polygons]"'
324 , ' value="' , $no_of_polygons , '" />';
326 for ($k = 0; $k < $no_of_polygons; $k++) {
327 echo '<br/>';
328 printf(__('Polygon %d:'), $k + 1);
329 $no_of_lines = isset($gis_data[$a][$type][$k]['no_of_lines'])
330 ? $gis_data[$a][$type][$k]['no_of_lines'] : 1;
331 if ($no_of_lines < 1) {
332 $no_of_lines = 1;
334 if (isset($gis_data[$a][$type][$k]['add_line'])) {
335 $no_of_lines++;
337 echo '<input type="hidden"'
338 , ' name="gis_data[' , $a , '][' , $type , '][' , $k
339 , '][no_of_lines]"' , ' value="' , $no_of_lines , '" />';
341 for ($i = 0; $i < $no_of_lines; $i++) {
342 echo '<br/><br/>';
343 if ($i == 0) {
344 echo __('Outer ring:');
345 } else {
346 printf(__('Inner ring %d:'), $i);
349 $no_of_points = isset($gis_data[$a][$type][$k][$i]['no_of_points'])
350 ? $gis_data[$a][$type][$k][$i]['no_of_points'] : 4;
351 if ($no_of_points < 4) {
352 $no_of_points = 4;
354 if (isset($gis_data[$a][$type][$k][$i]['add_point'])) {
355 $no_of_points++;
357 echo '<input type="hidden"'
358 , ' name="gis_data[' , $a , '][' , $type , '][' , $k , '][' , $i
359 , '][no_of_points]"' , ' value="' , $no_of_points , '" />';
361 for ($j = 0; $j < $no_of_points; $j++) {
362 echo '<br/>';
363 printf(__('Point %d'), $j + 1);
364 echo ': ';
365 echo '<label for="x">' , __("X") , '</label>';
366 echo '<input type="text"'
367 , ' name="gis_data[' , $a , '][' , $type , '][' , $k , ']['
368 , $i , '][' , $j , '][x]"'
369 , ' value="' , escape($gis_data[$a][$type][$k][$i][$j]['x'])
370 , '" />';
371 echo '<label for="y">' , __("Y") , '</label>';
372 echo '<input type="text"'
373 , ' name="gis_data[' , $a , '][' , $type , '][' , $k , ']['
374 , $i , '][' , $j , '][y]"'
375 , ' value="' , escape($gis_data[$a][$type][$k][$i][$j]['y'])
376 , '" />';
378 echo '<input type="submit"'
379 , ' name="gis_data[' , $a , '][' , $type , '][' , $k , '][' , $i
380 , '][add_point]"'
381 , ' class="add addPoint" value="' , __("Add a point") , '" />';
383 echo '<br/>';
384 echo '<input type="submit"'
385 , ' name="gis_data[' , $a , '][' , $type , '][' , $k , '][add_line]"'
386 , ' class="add addLine" value="' , __('Add an inner ring') , '" />';
387 echo '<br/>';
389 echo '<br/>';
390 echo '<input type="submit"'
391 , ' name="gis_data[' , $a , '][' , $type , '][add_polygon]"'
392 , ' class="add addPolygon" value="' , __('Add a polygon') , '" />';
395 if ($geom_type == 'GEOMETRYCOLLECTION') {
396 echo '<br/><br/>';
397 echo '<input type="submit" name="gis_data[GEOMETRYCOLLECTION][add_geom]"'
398 , 'class="add addGeom" value="' , __("Add geometry") , '" />';
400 echo '</div>';
401 echo '<!-- End of data section -->';
403 echo '<br/>';
404 echo '<input type="submit" name="gis_data[save]" value="' , __('Go') , '" />';
406 echo '<div id="gis_data_output">';
407 echo '<h3>' , __('Output') , '</h3>';
408 echo '<p>';
409 echo __(
410 'Choose "GeomFromText" from the "Function" column and paste the'
411 . ' string below into the "Value" field.'
413 echo '</p>';
414 echo '<textarea id="gis_data_textarea" cols="95" rows="5">';
415 echo $result;
416 echo '</textarea>';
417 echo '</div>';
419 echo '</div>';
420 echo '</form>';
422 PMA\libraries\Response::getInstance()->addJSON('gis_editor', ob_get_contents());
423 ob_end_clean();