2 /* vim: set expandtab sw=4 ts=4 sts=4: */
4 * Editor for Geometry data types.
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 string escaped variable
17 function escape($variable)
19 return isset($variable) ?
htmlspecialchars($variable) : '';
22 require_once 'libraries/common.inc.php';
23 require_once 'libraries/gis/GIS_Factory.class.php';
24 require_once 'libraries/gis/GIS_Visualization.class.php';
25 require_once 'libraries/tbl_gis_visualization.lib.php';
27 // Get data if any posted
29 if (PMA_isValid($_REQUEST['gis_data'], 'array')) {
30 $gis_data = $_REQUEST['gis_data'];
43 /** @var PMA_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 = PMA_GIS_Factory
::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 = PMA_GIS_visualizationResults(
93 $data, $visualizationSettings, $format
95 $open_layers = PMA_GIS_visualizationResults($data, $visualizationSettings, 'ol');
97 // If the call is to update the WKT and visualization make an AJAX response
98 if (isset($_REQUEST['generate']) && $_REQUEST['generate'] == true) {
101 'visualization' => $visualization,
102 'openLayers' => $open_layers,
104 $response = PMA_Response
::getInstance();
105 $response->addJSON($extra_data);
111 echo '<form id="gis_data_editor_form" action="gis_data_editor.php" method="post">';
112 echo '<input type="hidden" id="pmaThemeImage"'
113 . ' value="' . $GLOBALS['pmaThemeImage'] . '" />';
114 echo '<div id="gis_data_editor">';
118 __('Value for the column "%s"'),
119 htmlspecialchars($_REQUEST['field'])
123 echo '<input type="hidden" name="field" value="'
124 . htmlspecialchars($_REQUEST['field']) . '" />';
125 // The input field to which the final result should be added
126 // and corresponding null checkbox
127 if (isset($_REQUEST['input_name'])) {
128 echo '<input type="hidden" name="input_name" value="'
129 . htmlspecialchars($_REQUEST['input_name']) . '" />';
131 echo PMA_URL_getHiddenInputs();
133 echo '<!-- Visualization section -->';
134 echo '<div id="placeholder" style="width:450px;height:300px;'
135 . ($srid != 0 ?
'display:none;' : '') . '">';
139 echo '<div id="openlayersmap" style="width:450px;height:300px;'
140 . ($srid == 0 ?
'display:none;' : '') . '">';
143 echo '<div class="choice" style="float:right;clear:right;">';
144 echo '<input type="checkbox" id="choice" value="useBaseLayer"'
145 . ($srid != 0 ?
' checked="checked"' : '') . '/>';
146 echo '<label for="choice">' . __("Use OpenStreetMaps as Base Layer") . '</label>';
149 echo '<script language="javascript" type="text/javascript">';
152 echo '<!-- End of visualization section -->';
155 echo '<!-- Header section - Inclueds GIS type selector and input field for SRID -->';
156 echo '<div id="gis_data_header">';
157 echo '<select name="gis_data[gis_type]" class="gis_type">';
158 foreach ($gis_types as $gis_type) {
159 echo '<option value="' . $gis_type . '"';
160 if ($geom_type == $gis_type) {
161 echo ' selected="selected"';
163 echo '>' . $gis_type . '</option>';
166 echo ' ';
167 /* l10n: Spatial Reference System Identifier */
168 echo '<label for="srid">' . __('SRID:') . '</label>';
169 echo '<input name="gis_data[srid]" type="text" value="' . $srid . '" />';
171 echo '<!-- End of header section -->';
173 echo '<!-- Data section -->';
174 echo '<div id="gis_data">';
177 if ($geom_type == 'GEOMETRYCOLLECTION') {
178 $geom_count = (isset($gis_data[$geom_type]['geom_count']))
179 ?
$gis_data[$geom_type]['geom_count'] : 1;
180 if (isset($gis_data[$geom_type]['add_geom'])) {
183 echo '<input type="hidden" name="gis_data[GEOMETRYCOLLECTION][geom_count]"'
184 . ' value="' . $geom_count . '" />';
187 for ($a = 0; $a < $geom_count; $a++
) {
189 if ($geom_type == 'GEOMETRYCOLLECTION') {
191 printf(__('Geometry %d:'), $a +
1);
193 if (isset($gis_data[$a]['gis_type'])) {
194 $type = $gis_data[$a]['gis_type'];
196 $type = $gis_types[0];
198 echo '<select name="gis_data[' . $a . '][gis_type]" class="gis_type">';
199 foreach (array_slice($gis_types, 0, 6) as $gis_type) {
200 echo '<option value="' . $gis_type . '"';
201 if ($type == $gis_type) {
202 echo ' selected="selected"';
204 echo '>' . $gis_type . '</option>';
211 if ($type == 'POINT') {
214 echo '<label for="x">' . __("X") . '</label>';
215 echo '<input name="gis_data[' . $a . '][POINT][x]" type="text"'
216 . ' value="' . escape($gis_data[$a]['POINT']['x']) . '" />';
217 echo '<label for="y">' . __("Y") . '</label>';
218 echo '<input name="gis_data[' . $a . '][POINT][y]" type="text"'
219 . ' value="' . escape($gis_data[$a]['POINT']['y']) . '" />';
221 } elseif ($type == 'MULTIPOINT' ||
$type == 'LINESTRING') {
222 $no_of_points = isset($gis_data[$a][$type]['no_of_points'])
223 ?
$gis_data[$a][$type]['no_of_points'] : 1;
224 if ($type == 'LINESTRING' && $no_of_points < 2) {
227 if ($type == 'MULTIPOINT' && $no_of_points < 1) {
231 if (isset($gis_data[$a][$type]['add_point'])) {
234 echo '<input type="hidden" value="' . $no_of_points . '"'
235 . ' name="gis_data[' . $a . '][' . $type . '][no_of_points]" />';
237 for ($i = 0; $i < $no_of_points; $i++
) {
239 printf(__('Point %d'), $i +
1);
241 echo '<label for="x">' . __("X") . '</label>';
242 echo '<input type="text"'
243 . ' name="gis_data[' . $a . '][' . $type . '][' . $i . '][x]"'
244 . ' value="' . escape($gis_data[$a][$type][$i]['x']) . '" />';
245 echo '<label for="y">' . __("Y") . '</label>';
246 echo '<input type="text"'
247 . ' name="gis_data[' . $a . '][' . $type . '][' . $i . '][y]"'
248 . ' value="' . escape($gis_data[$a][$type][$i]['y']) . '" />';
250 echo '<input type="submit"'
251 . ' name="gis_data[' . $a . '][' . $type . '][add_point]"'
252 . ' class="add addPoint" value="' . __("Add a point") . '" />';
254 } elseif ($type == 'MULTILINESTRING' ||
$type == 'POLYGON') {
255 $no_of_lines = isset($gis_data[$a][$type]['no_of_lines'])
256 ?
$gis_data[$a][$type]['no_of_lines'] : 1;
257 if ($no_of_lines < 1) {
260 if (isset($gis_data[$a][$type]['add_line'])) {
263 echo '<input type="hidden" value="' . $no_of_lines . '"'
264 . ' name="gis_data[' . $a . '][' . $type . '][no_of_lines]" />';
266 for ($i = 0; $i < $no_of_lines; $i++
) {
268 if ($type == 'MULTILINESTRING') {
269 printf(__('Linestring %d:'), $i +
1);
272 echo __('Outer ring:');
274 printf(__('Inner ring %d:'), $i);
278 $no_of_points = isset($gis_data[$a][$type][$i]['no_of_points'])
279 ?
$gis_data[$a][$type][$i]['no_of_points'] : 2;
280 if ($type == 'MULTILINESTRING' && $no_of_points < 2) {
283 if ($type == 'POLYGON' && $no_of_points < 4) {
286 if (isset($gis_data[$a][$type][$i]['add_point'])) {
289 echo '<input type="hidden" value="' . $no_of_points . '"'
290 . ' name="gis_data[' . $a . '][' . $type . '][' . $i
291 . '][no_of_points]" />';
293 for ($j = 0; $j < $no_of_points; $j++
) {
295 printf(__('Point %d'), $j +
1);
297 echo '<label for="x">' . __("X") . '</label>';
298 echo '<input type="text" name="gis_data[' . $a . '][' . $type . ']['
299 . $i . '][' . $j . '][x]" value="'
300 . escape($gis_data[$a][$type][$i][$j]['x']) . '" />';
301 echo '<label for="y">' . __("Y") . '</label>';
302 echo '<input type="text" name="gis_data[' . $a . '][' . $type . ']['
303 . $i . '][' . $j . '][y]"' . ' value="'
304 . escape($gis_data[$a][$type][$i][$j]['x']) . '" />';
306 echo '<input type="submit" name="gis_data[' . $a . '][' . $type . ']['
307 . $i . '][add_point]"'
308 . ' class="add addPoint" value="' . __("Add a point") . '" />';
310 $caption = ($type == 'MULTILINESTRING')
311 ?
__('Add a linestring')
312 : __('Add an inner ring');
314 echo '<input type="submit"'
315 . ' name="gis_data[' . $a . '][' . $type . '][add_line]"'
316 . ' class="add addLine" value="' . $caption . '" />';
318 } elseif ($type == 'MULTIPOLYGON') {
319 $no_of_polygons = isset($gis_data[$a][$type]['no_of_polygons'])
320 ?
$gis_data[$a][$type]['no_of_polygons'] : 1;
321 if ($no_of_polygons < 1) {
324 if (isset($gis_data[$a][$type]['add_polygon'])) {
327 echo '<input type="hidden"'
328 . ' name="gis_data[' . $a . '][' . $type . '][no_of_polygons]"'
329 . ' value="' . $no_of_polygons . '" />';
331 for ($k = 0; $k < $no_of_polygons; $k++
) {
333 printf(__('Polygon %d:'), $k +
1);
334 $no_of_lines = isset($gis_data[$a][$type][$k]['no_of_lines'])
335 ?
$gis_data[$a][$type][$k]['no_of_lines'] : 1;
336 if ($no_of_lines < 1) {
339 if (isset($gis_data[$a][$type][$k]['add_line'])) {
342 echo '<input type="hidden"'
343 . ' name="gis_data[' . $a . '][' . $type . '][' . $k
344 . '][no_of_lines]"' . ' value="' . $no_of_lines . '" />';
346 for ($i = 0; $i < $no_of_lines; $i++
) {
349 echo __('Outer ring:');
351 printf(__('Inner ring %d:'), $i);
354 $no_of_points = isset($gis_data[$a][$type][$k][$i]['no_of_points'])
355 ?
$gis_data[$a][$type][$k][$i]['no_of_points'] : 4;
356 if ($no_of_points < 4) {
359 if (isset($gis_data[$a][$type][$k][$i]['add_point'])) {
362 echo '<input type="hidden"'
363 . ' name="gis_data[' . $a . '][' . $type . '][' . $k . '][' . $i
364 . '][no_of_points]"' . ' value="' . $no_of_points . '" />';
366 for ($j = 0; $j < $no_of_points; $j++
) {
368 printf(__('Point %d'), $j +
1);
370 echo '<label for="x">' . __("X") . '</label>';
371 echo '<input type="text"'
372 . ' name="gis_data[' . $a . '][' . $type . '][' . $k . ']['
373 . $i . '][' . $j . '][x]"'
374 . ' value="' . escape($gis_data[$a][$type][$k][$i][$j]['x'])
376 echo '<label for="y">' . __("Y") . '</label>';
377 echo '<input type="text"'
378 . ' name="gis_data[' . $a . '][' . $type . '][' . $k . ']['
379 . $i . '][' . $j . '][y]"'
380 . ' value="' . escape($gis_data[$a][$type][$k][$i][$j]['y'])
383 echo '<input type="submit"'
384 . ' name="gis_data[' . $a . '][' . $type . '][' . $k . '][' . $i
386 . ' class="add addPoint" value="' . __("Add a point") . '" />';
389 echo '<input type="submit"'
390 . ' name="gis_data[' . $a . '][' . $type . '][' . $k . '][add_line]"'
391 . ' class="add addLine" value="' . __('Add an inner ring') . '" />';
395 echo '<input type="submit"'
396 . ' name="gis_data[' . $a . '][' . $type . '][add_polygon]"'
397 . ' class="add addPolygon" value="' . __('Add a polygon') . '" />';
400 if ($geom_type == 'GEOMETRYCOLLECTION') {
402 echo '<input type="submit" name="gis_data[GEOMETRYCOLLECTION][add_geom]"'
403 . 'class="add addGeom" value="' . __("Add geometry") . '" />';
406 echo '<!-- End of data section -->';
409 echo '<input type="submit" name="gis_data[save]" value="' . __('Go') . '" />';
411 echo '<div id="gis_data_output">';
412 echo '<h3>' . __('Output') . '</h3>';
415 'Choose "GeomFromText" from the "Function" column and paste the'
416 . ' string below into the "Value" field.'
419 echo '<textarea id="gis_data_textarea" cols="95" rows="5">';
427 PMA_Response
::getInstance()->addJSON('gis_editor', ob_get_contents());