2 /* vim: set expandtab sw=4 ts=4 sts=4: */
4 * Editor for Geometry data types.
8 use PMA\libraries\gis\GISFactory
;
9 use PMA\libraries\gis\GISVisualization
;
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
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 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(
53 mb_strpos($_REQUEST['value'], "(") - $start
56 if ((! isset($gis_data['gis_type']))
57 ||
(! in_array($gis_data['gis_type'], $gis_types))
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(
83 'spatialColumn' => 'wkt'
85 $data = array(array('wkt' => $wkt_with_zero, 'srid' => $srid));
86 $visualization = GISVisualization
::getByData($data, $visualizationSettings)
89 $open_layers = GISVisualization
::getByData($data, $visualizationSettings)
92 // If the call is to update the WKT and visualization make an AJAX response
93 if (isset($_REQUEST['generate']) && $_REQUEST['generate'] == true) {
96 'visualization' => $visualization,
97 'openLayers' => $open_layers,
99 $response = PMA\libraries\Response
::getInstance();
100 $response->addJSON($extra_data);
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">';
113 __('Value for the column "%s"'),
114 htmlspecialchars($_REQUEST['field'])
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;' : '') , '">';
134 echo '<div id="openlayersmap" style="width:450px;height:300px;'
135 , ($srid == 0 ?
'display:none;' : '') , '">';
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>';
144 echo '<script language="javascript" type="text/javascript">';
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>';
161 echo ' ';
162 /* l10n: Spatial Reference System Identifier */
163 echo '<label for="srid">' , __('SRID:') , '</label>';
164 echo '<input name="gis_data[srid]" type="text" value="' , $srid , '" />';
166 echo '<!-- End of header section -->';
168 echo '<!-- Data section -->';
169 echo '<div id="gis_data">';
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'])) {
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') {
186 printf(__('Geometry %d:'), $a +
1);
188 if (isset($gis_data[$a]['gis_type'])) {
189 $type = $gis_data[$a]['gis_type'];
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>';
206 if ($type == '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) {
222 if ($type == 'MULTIPOINT' && $no_of_points < 1) {
226 if (isset($gis_data[$a][$type]['add_point'])) {
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++
) {
234 printf(__('Point %d'), $i +
1);
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) {
255 if (isset($gis_data[$a][$type]['add_line'])) {
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++
) {
263 if ($type == 'MULTILINESTRING') {
264 printf(__('Linestring %d:'), $i +
1);
267 echo __('Outer ring:');
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) {
278 if ($type == 'POLYGON' && $no_of_points < 4) {
281 if (isset($gis_data[$a][$type][$i]['add_point'])) {
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++
) {
290 printf(__('Point %d'), $j +
1);
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');
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) {
319 if (isset($gis_data[$a][$type]['add_polygon'])) {
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++
) {
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) {
334 if (isset($gis_data[$a][$type][$k]['add_line'])) {
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++
) {
344 echo __('Outer ring:');
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) {
354 if (isset($gis_data[$a][$type][$k][$i]['add_point'])) {
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++
) {
363 printf(__('Point %d'), $j +
1);
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'])
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'])
378 echo '<input type="submit"'
379 , ' name="gis_data[' , $a , '][' , $type , '][' , $k , '][' , $i
381 , ' class="add addPoint" value="' , __("Add a point") , '" />';
384 echo '<input type="submit"'
385 , ' name="gis_data[' , $a , '][' , $type , '][' , $k , '][add_line]"'
386 , ' class="add addLine" value="' , __('Add an inner ring') , '" />';
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') {
397 echo '<input type="submit" name="gis_data[GEOMETRYCOLLECTION][add_geom]"'
398 , 'class="add addGeom" value="' , __("Add geometry") , '" />';
401 echo '<!-- End of data section -->';
404 echo '<input type="submit" name="gis_data[save]" value="' , __('Go') , '" />';
406 echo '<div id="gis_data_output">';
407 echo '<h3>' , __('Output') , '</h3>';
410 'Choose "GeomFromText" from the "Function" column and paste the'
411 . ' string below into the "Value" field.'
414 echo '<textarea id="gis_data_textarea" cols="95" rows="5">';
422 PMA\libraries\Response
::getInstance()->addJSON('gis_editor', ob_get_contents());