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/pma_gis_visualization.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_URL_getHiddenInputs();
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 /* l10n: Spatial Reference System Identifier */
162 echo '<label for="srid">' . __('SRID:') . '</label>';
163 echo '<input name="gis_data[srid]" type="text" value="' . $srid . '" />';
165 echo '<!-- End of header section -->';
167 echo '<!-- Data section -->';
168 echo '<div id="gis_data">';
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'])) {
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') {
185 printf(__('Geometry %d:'), $a +
1);
187 if (isset($gis_data[$a]['gis_type'])) {
188 $type = $gis_data[$a]['gis_type'];
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>';
205 if ($type == '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) {
221 if ($type == 'MULTIPOINT' && $no_of_points < 1) {
225 if (isset($gis_data[$a][$type]['add_point'])) {
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++
) {
233 printf(__('Point %d'), $i +
1);
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) {
254 if (isset($gis_data[$a][$type]['add_line'])) {
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++
) {
262 if ($type == 'MULTILINESTRING') {
263 printf(__('Linestring %d:'), $i +
1);
266 echo __('Outer ring:');
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) {
277 if ($type == 'POLYGON' && $no_of_points < 4) {
280 if (isset($gis_data[$a][$type][$i]['add_point'])) {
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++
) {
288 printf(__('Point %d'), $j +
1);
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');
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) {
317 if (isset($gis_data[$a][$type]['add_polygon'])) {
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++
) {
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) {
332 if (isset($gis_data[$a][$type][$k]['add_line'])) {
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++
) {
342 echo __('Outer ring:');
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) {
352 if (isset($gis_data[$a][$type][$k][$i]['add_point'])) {
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++
) {
361 printf(__('Point %d'), $j +
1);
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") . '" />';
377 echo '<input type="submit"'
378 . ' name="gis_data[' . $a . '][' . $type . '][' . $k . '][add_line]"'
379 . ' class="add addLine" value="' . __('Add an inner ring'). '" />';
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') {
390 echo '<input type="submit" name="gis_data[GEOMETRYCOLLECTION][add_geom]"'
391 . 'class="add addGeom" value="' . __("Add geometry") . '" />';
394 echo '<!-- End of data section -->';
397 echo '<input type="submit" name="gis_data[save]" value="' . __('Go') . '" />';
399 echo '<div id="gis_data_output">';
400 echo '<h3>' . __('Output') . '</h3>';
403 'Choose "GeomFromText" from the "Function" column and paste the'
404 . ' string below into the "Value" field'
407 echo '<textarea id="gis_data_textarea" cols="95" rows="5">';
415 PMA_Response
::getInstance()->addJSON('gis_editor', ob_get_contents());