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';
26 // Get data if any posted
28 if (PMA_isValid($_REQUEST['gis_data'], 'array')) {
29 $gis_data = $_REQUEST['gis_data'];
42 /** @var PMA_String $pmaString */
43 $pmaString = $GLOBALS['PMA_String'];
45 // Extract type from the initial call and make sure that it's a valid one.
46 // Extract from field's values if available, if not use the column type passed.
47 if (! isset($gis_data['gis_type'])) {
48 if (isset($_REQUEST['type']) && $_REQUEST['type'] != '') {
49 $gis_data['gis_type'] = /*overload*/mb_strtoupper($_REQUEST['type']);
51 if (isset($_REQUEST['value']) && trim($_REQUEST['value']) != '') {
52 $start = (substr($_REQUEST['value'], 0, 1) == "'") ?
1 : 0;
53 $gis_data['gis_type'] = /*overload*/mb_substr(
56 /*overload*/mb_strpos($_REQUEST['value'], "(") - $start
59 if ((! isset($gis_data['gis_type']))
60 ||
(! in_array($gis_data['gis_type'], $gis_types))
62 $gis_data['gis_type'] = $gis_types[0];
65 $geom_type = $gis_data['gis_type'];
67 // Generate parameters from value passed.
68 $gis_obj = PMA_GIS_Factory
::factory($geom_type);
69 if (isset($_REQUEST['value'])) {
70 $gis_data = array_merge(
71 $gis_data, $gis_obj->generateParams($_REQUEST['value'])
75 // Generate Well Known Text
76 $srid = (isset($gis_data['srid']) && $gis_data['srid'] != '')
77 ?
htmlspecialchars($gis_data['srid']) : 0;
78 $wkt = $gis_obj->generateWkt($gis_data, 0);
79 $wkt_with_zero = $gis_obj->generateWkt($gis_data, 0, '0');
80 $result = "'" . $wkt . "'," . $srid;
82 // Generate PNG or SVG based visualization
83 $format = (PMA_USR_BROWSER_AGENT
== 'IE' && PMA_USR_BROWSER_VER
<= 8)
85 $visualizationSettings = array(
88 'spatialColumn' => 'wkt'
90 $data = array(array('wkt' => $wkt_with_zero, 'srid' => $srid));
91 $visualization = PMA_GIS_Visualization
::getByData($data, $visualizationSettings)->toImage($format);
93 $open_layers = PMA_GIS_Visualization
::getByData($data, $visualizationSettings)->asOl();
95 // If the call is to update the WKT and visualization make an AJAX response
96 if (isset($_REQUEST['generate']) && $_REQUEST['generate'] == true) {
99 'visualization' => $visualization,
100 'openLayers' => $open_layers,
102 $response = PMA_Response
::getInstance();
103 $response->addJSON($extra_data);
109 echo '<form id="gis_data_editor_form" action="gis_data_editor.php" method="post">';
110 echo '<input type="hidden" id="pmaThemeImage"'
111 . ' value="' . $GLOBALS['pmaThemeImage'] . '" />';
112 echo '<div id="gis_data_editor">';
116 __('Value for the column "%s"'),
117 htmlspecialchars($_REQUEST['field'])
121 echo '<input type="hidden" name="field" value="'
122 . htmlspecialchars($_REQUEST['field']) . '" />';
123 // The input field to which the final result should be added
124 // and corresponding null checkbox
125 if (isset($_REQUEST['input_name'])) {
126 echo '<input type="hidden" name="input_name" value="'
127 . htmlspecialchars($_REQUEST['input_name']) . '" />';
129 echo PMA_URL_getHiddenInputs();
131 echo '<!-- Visualization section -->';
132 echo '<div id="placeholder" style="width:450px;height:300px;'
133 . ($srid != 0 ?
'display:none;' : '') . '">';
137 echo '<div id="openlayersmap" style="width:450px;height:300px;'
138 . ($srid == 0 ?
'display:none;' : '') . '">';
141 echo '<div class="choice" style="float:right;clear:right;">';
142 echo '<input type="checkbox" id="choice" value="useBaseLayer"'
143 . ($srid != 0 ?
' checked="checked"' : '') . '/>';
144 echo '<label for="choice">' . __("Use OpenStreetMaps as Base Layer") . '</label>';
147 echo '<script language="javascript" type="text/javascript">';
150 echo '<!-- End of visualization section -->';
153 echo '<!-- Header section - Inclueds GIS type selector and input field for SRID -->';
154 echo '<div id="gis_data_header">';
155 echo '<select name="gis_data[gis_type]" class="gis_type">';
156 foreach ($gis_types as $gis_type) {
157 echo '<option value="' . $gis_type . '"';
158 if ($geom_type == $gis_type) {
159 echo ' selected="selected"';
161 echo '>' . $gis_type . '</option>';
164 echo ' ';
165 /* l10n: Spatial Reference System Identifier */
166 echo '<label for="srid">' . __('SRID:') . '</label>';
167 echo '<input name="gis_data[srid]" type="text" value="' . $srid . '" />';
169 echo '<!-- End of header section -->';
171 echo '<!-- Data section -->';
172 echo '<div id="gis_data">';
175 if ($geom_type == 'GEOMETRYCOLLECTION') {
176 $geom_count = (isset($gis_data[$geom_type]['geom_count']))
177 ?
$gis_data[$geom_type]['geom_count'] : 1;
178 if (isset($gis_data[$geom_type]['add_geom'])) {
181 echo '<input type="hidden" name="gis_data[GEOMETRYCOLLECTION][geom_count]"'
182 . ' value="' . $geom_count . '" />';
185 for ($a = 0; $a < $geom_count; $a++
) {
187 if ($geom_type == 'GEOMETRYCOLLECTION') {
189 printf(__('Geometry %d:'), $a +
1);
191 if (isset($gis_data[$a]['gis_type'])) {
192 $type = $gis_data[$a]['gis_type'];
194 $type = $gis_types[0];
196 echo '<select name="gis_data[' . $a . '][gis_type]" class="gis_type">';
197 foreach (array_slice($gis_types, 0, 6) as $gis_type) {
198 echo '<option value="' . $gis_type . '"';
199 if ($type == $gis_type) {
200 echo ' selected="selected"';
202 echo '>' . $gis_type . '</option>';
209 if ($type == 'POINT') {
212 echo '<label for="x">' . __("X") . '</label>';
213 echo '<input name="gis_data[' . $a . '][POINT][x]" type="text"'
214 . ' value="' . escape($gis_data[$a]['POINT']['x']) . '" />';
215 echo '<label for="y">' . __("Y") . '</label>';
216 echo '<input name="gis_data[' . $a . '][POINT][y]" type="text"'
217 . ' value="' . escape($gis_data[$a]['POINT']['y']) . '" />';
219 } elseif ($type == 'MULTIPOINT' ||
$type == 'LINESTRING') {
220 $no_of_points = isset($gis_data[$a][$type]['no_of_points'])
221 ?
$gis_data[$a][$type]['no_of_points'] : 1;
222 if ($type == 'LINESTRING' && $no_of_points < 2) {
225 if ($type == 'MULTIPOINT' && $no_of_points < 1) {
229 if (isset($gis_data[$a][$type]['add_point'])) {
232 echo '<input type="hidden" value="' . $no_of_points . '"'
233 . ' name="gis_data[' . $a . '][' . $type . '][no_of_points]" />';
235 for ($i = 0; $i < $no_of_points; $i++
) {
237 printf(__('Point %d'), $i +
1);
239 echo '<label for="x">' . __("X") . '</label>';
240 echo '<input type="text"'
241 . ' name="gis_data[' . $a . '][' . $type . '][' . $i . '][x]"'
242 . ' value="' . escape($gis_data[$a][$type][$i]['x']) . '" />';
243 echo '<label for="y">' . __("Y") . '</label>';
244 echo '<input type="text"'
245 . ' name="gis_data[' . $a . '][' . $type . '][' . $i . '][y]"'
246 . ' value="' . escape($gis_data[$a][$type][$i]['y']) . '" />';
248 echo '<input type="submit"'
249 . ' name="gis_data[' . $a . '][' . $type . '][add_point]"'
250 . ' class="add addPoint" value="' . __("Add a point") . '" />';
252 } elseif ($type == 'MULTILINESTRING' ||
$type == 'POLYGON') {
253 $no_of_lines = isset($gis_data[$a][$type]['no_of_lines'])
254 ?
$gis_data[$a][$type]['no_of_lines'] : 1;
255 if ($no_of_lines < 1) {
258 if (isset($gis_data[$a][$type]['add_line'])) {
261 echo '<input type="hidden" value="' . $no_of_lines . '"'
262 . ' name="gis_data[' . $a . '][' . $type . '][no_of_lines]" />';
264 for ($i = 0; $i < $no_of_lines; $i++
) {
266 if ($type == 'MULTILINESTRING') {
267 printf(__('Linestring %d:'), $i +
1);
270 echo __('Outer ring:');
272 printf(__('Inner ring %d:'), $i);
276 $no_of_points = isset($gis_data[$a][$type][$i]['no_of_points'])
277 ?
$gis_data[$a][$type][$i]['no_of_points'] : 2;
278 if ($type == 'MULTILINESTRING' && $no_of_points < 2) {
281 if ($type == 'POLYGON' && $no_of_points < 4) {
284 if (isset($gis_data[$a][$type][$i]['add_point'])) {
287 echo '<input type="hidden" value="' . $no_of_points . '"'
288 . ' name="gis_data[' . $a . '][' . $type . '][' . $i
289 . '][no_of_points]" />';
291 for ($j = 0; $j < $no_of_points; $j++
) {
293 printf(__('Point %d'), $j +
1);
295 echo '<label for="x">' . __("X") . '</label>';
296 echo '<input type="text" name="gis_data[' . $a . '][' . $type . ']['
297 . $i . '][' . $j . '][x]" value="'
298 . escape($gis_data[$a][$type][$i][$j]['x']) . '" />';
299 echo '<label for="y">' . __("Y") . '</label>';
300 echo '<input type="text" name="gis_data[' . $a . '][' . $type . ']['
301 . $i . '][' . $j . '][y]"' . ' value="'
302 . escape($gis_data[$a][$type][$i][$j]['x']) . '" />';
304 echo '<input type="submit" name="gis_data[' . $a . '][' . $type . ']['
305 . $i . '][add_point]"'
306 . ' class="add addPoint" value="' . __("Add a point") . '" />';
308 $caption = ($type == 'MULTILINESTRING')
309 ?
__('Add a linestring')
310 : __('Add an inner ring');
312 echo '<input type="submit"'
313 . ' name="gis_data[' . $a . '][' . $type . '][add_line]"'
314 . ' class="add addLine" value="' . $caption . '" />';
316 } elseif ($type == 'MULTIPOLYGON') {
317 $no_of_polygons = isset($gis_data[$a][$type]['no_of_polygons'])
318 ?
$gis_data[$a][$type]['no_of_polygons'] : 1;
319 if ($no_of_polygons < 1) {
322 if (isset($gis_data[$a][$type]['add_polygon'])) {
325 echo '<input type="hidden"'
326 . ' name="gis_data[' . $a . '][' . $type . '][no_of_polygons]"'
327 . ' value="' . $no_of_polygons . '" />';
329 for ($k = 0; $k < $no_of_polygons; $k++
) {
331 printf(__('Polygon %d:'), $k +
1);
332 $no_of_lines = isset($gis_data[$a][$type][$k]['no_of_lines'])
333 ?
$gis_data[$a][$type][$k]['no_of_lines'] : 1;
334 if ($no_of_lines < 1) {
337 if (isset($gis_data[$a][$type][$k]['add_line'])) {
340 echo '<input type="hidden"'
341 . ' name="gis_data[' . $a . '][' . $type . '][' . $k
342 . '][no_of_lines]"' . ' value="' . $no_of_lines . '" />';
344 for ($i = 0; $i < $no_of_lines; $i++
) {
347 echo __('Outer ring:');
349 printf(__('Inner ring %d:'), $i);
352 $no_of_points = isset($gis_data[$a][$type][$k][$i]['no_of_points'])
353 ?
$gis_data[$a][$type][$k][$i]['no_of_points'] : 4;
354 if ($no_of_points < 4) {
357 if (isset($gis_data[$a][$type][$k][$i]['add_point'])) {
360 echo '<input type="hidden"'
361 . ' name="gis_data[' . $a . '][' . $type . '][' . $k . '][' . $i
362 . '][no_of_points]"' . ' value="' . $no_of_points . '" />';
364 for ($j = 0; $j < $no_of_points; $j++
) {
366 printf(__('Point %d'), $j +
1);
368 echo '<label for="x">' . __("X") . '</label>';
369 echo '<input type="text"'
370 . ' name="gis_data[' . $a . '][' . $type . '][' . $k . ']['
371 . $i . '][' . $j . '][x]"'
372 . ' value="' . escape($gis_data[$a][$type][$k][$i][$j]['x'])
374 echo '<label for="y">' . __("Y") . '</label>';
375 echo '<input type="text"'
376 . ' name="gis_data[' . $a . '][' . $type . '][' . $k . ']['
377 . $i . '][' . $j . '][y]"'
378 . ' value="' . escape($gis_data[$a][$type][$k][$i][$j]['y'])
381 echo '<input type="submit"'
382 . ' name="gis_data[' . $a . '][' . $type . '][' . $k . '][' . $i
384 . ' class="add addPoint" value="' . __("Add a point") . '" />';
387 echo '<input type="submit"'
388 . ' name="gis_data[' . $a . '][' . $type . '][' . $k . '][add_line]"'
389 . ' class="add addLine" value="' . __('Add an inner ring') . '" />';
393 echo '<input type="submit"'
394 . ' name="gis_data[' . $a . '][' . $type . '][add_polygon]"'
395 . ' class="add addPolygon" value="' . __('Add a polygon') . '" />';
398 if ($geom_type == 'GEOMETRYCOLLECTION') {
400 echo '<input type="submit" name="gis_data[GEOMETRYCOLLECTION][add_geom]"'
401 . 'class="add addGeom" value="' . __("Add geometry") . '" />';
404 echo '<!-- End of data section -->';
407 echo '<input type="submit" name="gis_data[save]" value="' . __('Go') . '" />';
409 echo '<div id="gis_data_output">';
410 echo '<h3>' . __('Output') . '</h3>';
413 'Choose "GeomFromText" from the "Function" column and paste the'
414 . ' string below into the "Value" field.'
417 echo '<textarea id="gis_data_textarea" cols="95" rows="5">';
425 PMA_Response
::getInstance()->addJSON('gis_editor', ob_get_contents());