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 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_visualization.lib.php';
26 // Get data if any posted
28 if (PMA_isValid($_REQUEST['gis_data'], 'array')) {
29 $gis_data = $_REQUEST['gis_data'];
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))
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)
80 $visualizationSettings = array(
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) {
95 'visualization' => $visualization,
96 'openLayers' => $open_layers,
98 $response = PMA_Response
::getInstance();
99 $response->addJSON($extra_data);
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">';
112 __('Value for the column "%s"'),
113 htmlspecialchars($_REQUEST['field'])
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_generate_common_hidden_inputs();
127 echo '<!-- Visualization section -->';
128 echo '<div id="placeholder" style="width:450px;height:300px;'
129 . ($srid != 0 ?
'display:none;' : '') . '">';
133 echo '<div id="openlayersmap" style="width:450px;height:300px;'
134 . ($srid == 0 ?
'display:none;' : '') . '">';
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>';
143 echo '<script language="javascript" type="text/javascript">';
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>';
160 echo ' ';
161 echo '<label for="srid">' . __("SRID") . ':</label>';
162 echo '<input name="gis_data[srid]" type="text" value="' . $srid . '" />';
164 echo '<!-- End of header section -->';
166 echo '<!-- Data section -->';
167 echo '<div id="gis_data">';
170 if ($geom_type == 'GEOMETRYCOLLECTION') {
171 $geom_count = (isset($gis_data[$geom_type]['geom_count']))
172 ?
$gis_data[$geom_type]['geom_count'] : 1;
173 if (isset($gis_data[$geom_type]['add_geom'])) {
176 echo '<input type="hidden" name="gis_data[GEOMETRYCOLLECTION][geom_count]"'
177 . ' value="' . $geom_count . '" />';
180 for ($a = 0; $a < $geom_count; $a++
) {
182 if ($geom_type == 'GEOMETRYCOLLECTION') {
184 echo __("Geometry") . ' ' . ($a +
1) . ': ';
186 if (isset($gis_data[$a]['gis_type'])) {
187 $type = $gis_data[$a]['gis_type'];
189 $type = $gis_types[0];
191 echo '<select name="gis_data[' . $a . '][gis_type]" class="gis_type">';
192 foreach (array_slice($gis_types, 0, 6) as $gis_type) {
193 echo '<option value="' . $gis_type . '"';
194 if ($type == $gis_type) {
195 echo ' selected="selected"';
197 echo '>' . $gis_type . '</option>';
204 if ($type == 'POINT') {
206 echo __("Point") . ': ';
207 echo '<label for="x">' . __("X") . '</label>';
208 echo '<input name="gis_data[' . $a . '][POINT][x]" type="text"'
209 . ' value="' . escape($gis_data[$a]['POINT']['x']) . '" />';
210 echo '<label for="y">' . __("Y") . '</label>';
211 echo '<input name="gis_data[' . $a . '][POINT][y]" type="text"'
212 . ' value="' . escape($gis_data[$a]['POINT']['y']) . '" />';
214 } elseif ($type == 'MULTIPOINT' ||
$type == 'LINESTRING') {
215 $no_of_points = isset($gis_data[$a][$type]['no_of_points'])
216 ?
$gis_data[$a][$type]['no_of_points'] : 1;
217 if ($type == 'LINESTRING' && $no_of_points < 2) {
220 if ($type == 'MULTIPOINT' && $no_of_points < 1) {
224 if (isset($gis_data[$a][$type]['add_point'])) {
227 echo '<input type="hidden" value="' . $no_of_points . '"'
228 . ' name="gis_data[' . $a . '][' . $type . '][no_of_points]" />';
230 for ($i = 0; $i < $no_of_points; $i++
) {
232 printf(__('Point %d'), $i +
1);
234 echo '<label for="x">' . __("X") . '</label>';
235 echo '<input type="text"'
236 . ' name="gis_data[' . $a . '][' . $type . '][' . $i . '][x]"'
237 . ' value="' . escape($gis_data[$a][$type][$i]['x']) . '" />';
238 echo '<label for="y">' . __("Y") . '</label>';
239 echo '<input type="text"'
240 . ' name="gis_data[' . $a . '][' . $type . '][' . $i . '][y]"'
241 . ' value="' . escape($gis_data[$a][$type][$i]['y']). '" />';
243 echo '<input type="submit"'
244 . ' name="gis_data[' . $a . '][' . $type . '][add_point]"'
245 . ' class="add addPoint" value="' . __("Add a point") . '" />';
247 } elseif ($type == 'MULTILINESTRING' ||
$type == 'POLYGON') {
248 $no_of_lines = isset($gis_data[$a][$type]['no_of_lines'])
249 ?
$gis_data[$a][$type]['no_of_lines'] : 1;
250 if ($no_of_lines < 1) {
253 if (isset($gis_data[$a][$type]['add_line'])) {
256 echo '<input type="hidden" value="' . $no_of_lines . '"'
257 . ' name="gis_data[' . $a . '][' . $type . '][no_of_lines]" />';
259 for ($i = 0; $i < $no_of_lines; $i++
) {
261 if ($type == 'MULTILINESTRING') {
262 echo __("Linestring") . ' ' . ($i +
1) . ':';
265 echo __("Outer Ring") . ':';
267 echo __("Inner Ring") . ' ' . $i . ':';
271 $no_of_points = isset($gis_data[$a][$type][$i]['no_of_points'])
272 ?
$gis_data[$a][$type][$i]['no_of_points'] : 2;
273 if ($type == 'MULTILINESTRING' && $no_of_points < 2) {
276 if ($type == 'POLYGON' && $no_of_points < 4) {
279 if (isset($gis_data[$a][$type][$i]['add_point'])) {
282 echo '<input type="hidden" value="' . $no_of_points . '"'
283 . ' name="gis_data[' . $a . '][' . $type . '][' . $i . '][no_of_points]" />';
285 for ($j = 0; $j < $no_of_points; $j++
) {
287 printf(__('Point %d'), $j +
1);
289 echo '<label for="x">' . __("X") . '</label>';
290 echo '<input type="text"'
291 . ' name="gis_data[' . $a . '][' . $type . '][' . $i . '][' . $j . '][x]"'
292 . ' value="' . escape($gis_data[$a][$type][$i][$j]['x']) . '" />';
293 echo '<label for="y">' . __("Y") . '</label>';
294 echo '<input type="text"'
295 . ' name="gis_data[' . $a . '][' . $type . '][' . $i . '][' . $j . '][y]"'
296 . ' value="' . escape($gis_data[$a][$type][$i][$j]['x']) . '" />';
298 echo '<input type="submit"'
299 . ' name="gis_data[' . $a . '][' . $type . '][' . $i . '][add_point]"'
300 . ' class="add addPoint" value="' . __("Add a point") . '" />';
302 $caption = ($type == 'MULTILINESTRING')
303 ?
__('Add a linestring')
304 : __('Add an inner ring');
306 echo '<input type="submit"'
307 . ' name="gis_data[' . $a . '][' . $type . '][add_line]"'
308 . ' class="add addLine" value="' . $caption . '" />';
310 } elseif ($type == 'MULTIPOLYGON') {
311 $no_of_polygons = isset($gis_data[$a][$type]['no_of_polygons'])
312 ?
$gis_data[$a][$type]['no_of_polygons'] : 1;
313 if ($no_of_polygons < 1) {
316 if (isset($gis_data[$a][$type]['add_polygon'])) {
319 echo '<input type="hidden"'
320 . ' name="gis_data[' . $a . '][' . $type . '][no_of_polygons]"'
321 . ' value="' . $no_of_polygons . '" />';
323 for ($k = 0; $k < $no_of_polygons; $k++
) {
325 echo __("Polygon") . ' ' . ($k +
1) . ':';
326 $no_of_lines = isset($gis_data[$a][$type][$k]['no_of_lines'])
327 ?
$gis_data[$a][$type][$k]['no_of_lines'] : 1;
328 if ($no_of_lines < 1) {
331 if (isset($gis_data[$a][$type][$k]['add_line'])) {
334 echo '<input type="hidden"'
335 . ' name="gis_data[' . $a . '][' . $type . '][' . $k . '][no_of_lines]"'
336 . ' value="' . $no_of_lines . '" />';
338 for ($i = 0; $i < $no_of_lines; $i++
) {
341 echo __("Outer Ring") . ':';
343 echo __("Inner Ring") . ' ' . $i . ':';
346 $no_of_points = isset($gis_data[$a][$type][$k][$i]['no_of_points'])
347 ?
$gis_data[$a][$type][$k][$i]['no_of_points'] : 4;
348 if ($no_of_points < 4) {
351 if (isset($gis_data[$a][$type][$k][$i]['add_point'])) {
354 echo '<input type="hidden"'
355 . ' name="gis_data[' . $a . '][' . $type . '][' . $k . '][' . $i . '][no_of_points]"'
356 . ' value="' . $no_of_points . '" />';
358 for ($j = 0; $j < $no_of_points; $j++
) {
360 printf(__('Point %d'), $j +
1);
362 echo '<label for="x">' . __("X") . '</label>';
363 echo '<input type="text"'
364 . ' name="gis_data[' . $a . '][' . $type . '][' . $k . '][' . $i . '][' . $j . '][x]"'
365 . ' value="' . escape($gis_data[$a][$type][$k][$i][$j]['x']). '" />';
366 echo '<label for="y">' . __("Y") . '</label>';
367 echo '<input type="text"'
368 . ' name="gis_data[' . $a . '][' . $type . '][' . $k . '][' . $i . '][' . $j . '][y]"'
369 . ' value="' . escape($gis_data[$a][$type][$k][$i][$j]['y']) . '" />';
371 echo '<input type="submit"'
372 . ' name="gis_data[' . $a . '][' . $type . '][' . $k . '][' . $i . '][add_point]"'
373 . ' class="add addPoint" value="' . __("Add a point") . '" />';
376 echo '<input type="submit"'
377 . ' name="gis_data[' . $a . '][' . $type . '][' . $k . '][add_line]"'
378 . ' class="add addLine" value="' . __('Add an inner ring'). '" />';
382 echo '<input type="submit"'
383 . ' name="gis_data[' . $a . '][' . $type . '][add_polygon]"'
384 . ' class="add addPolygon" value="' . __('Add a polygon') . '" />';
387 if ($geom_type == 'GEOMETRYCOLLECTION') {
389 echo '<input type="submit" name="gis_data[GEOMETRYCOLLECTION][add_geom]"'
390 . 'class="add addGeom" value="' . __("Add geometry") . '" />';
393 echo '<!-- End of data section -->';
396 echo '<input type="submit" name="gis_data[save]" value="' . __('Go') . '" />';
398 echo '<div id="gis_data_output">';
399 echo '<h3>' . __('Output') . '</h3>';
402 'Choose "GeomFromText" from the "Function" column and paste the'
403 . ' string below into the "Value" field'
406 echo '<textarea id="gis_data_textarea" cols="95" rows="5">';
414 PMA_Response
::getInstance()->addJSON('gis_editor', ob_get_contents());