2 /* vim: set expandtab sw=4 ts=4 sts=4: */
4 * Editor for Geometry data types.
8 use PMA\libraries\Response
;
9 use PMA\libraries\gis\GISFactory
;
10 use PMA\libraries\gis\GISVisualization
;
11 use PMA\libraries\URL
;
14 * Escapes special characters if the variable is set.
15 * Returns an empty string otherwise.
17 * @param string $variable variable to be escaped
19 * @return string escaped variable
21 function escape($variable)
23 return isset($variable) ?
htmlspecialchars($variable) : '';
26 require_once 'libraries/common.inc.php';
28 if (! isset($_REQUEST['field'])) {
29 PMA\libraries\Util
::checkParameters(array('field'));
32 // Get data if any posted
34 if (PMA_isValid($_REQUEST['gis_data'], 'array')) {
35 $gis_data = $_REQUEST['gis_data'];
48 // Extract type from the initial call and make sure that it's a valid one.
49 // Extract from field's values if available, if not use the column type passed.
50 if (! isset($gis_data['gis_type'])) {
51 if (isset($_REQUEST['type']) && $_REQUEST['type'] != '') {
52 $gis_data['gis_type'] = mb_strtoupper($_REQUEST['type']);
54 if (isset($_REQUEST['value']) && trim($_REQUEST['value']) != '') {
55 $start = (substr($_REQUEST['value'], 0, 1) == "'") ?
1 : 0;
56 $gis_data['gis_type'] = mb_substr(
59 mb_strpos($_REQUEST['value'], "(") - $start
62 if ((! isset($gis_data['gis_type']))
63 ||
(! in_array($gis_data['gis_type'], $gis_types))
65 $gis_data['gis_type'] = $gis_types[0];
68 $geom_type = htmlspecialchars($gis_data['gis_type']);
70 // Generate parameters from value passed.
71 $gis_obj = GISFactory
::factory($geom_type);
72 if (isset($_REQUEST['value'])) {
73 $gis_data = array_merge(
74 $gis_data, $gis_obj->generateParams($_REQUEST['value'])
78 // Generate Well Known Text
79 $srid = (isset($gis_data['srid']) && $gis_data['srid'] != '')
80 ?
htmlspecialchars($gis_data['srid']) : 0;
81 $wkt = $gis_obj->generateWkt($gis_data, 0);
82 $wkt_with_zero = $gis_obj->generateWkt($gis_data, 0, '0');
83 $result = "'" . $wkt . "'," . $srid;
85 // Generate SVG based visualization
86 $visualizationSettings = array(
89 'spatialColumn' => 'wkt'
91 $data = array(array('wkt' => $wkt_with_zero, 'srid' => $srid));
92 $visualization = GISVisualization
::getByData($data, $visualizationSettings)
95 $open_layers = GISVisualization
::getByData($data, $visualizationSettings)
98 // If the call is to update the WKT and visualization make an AJAX response
99 if (isset($_REQUEST['generate']) && $_REQUEST['generate'] == true) {
102 'visualization' => $visualization,
103 'openLayers' => $open_layers,
105 $response = Response
::getInstance();
106 $response->addJSON($extra_data);
112 echo '<form id="gis_data_editor_form" action="gis_data_editor.php" method="post">';
113 echo '<input type="hidden" id="pmaThemeImage"'
114 , ' value="' , $GLOBALS['pmaThemeImage'] , '" />';
115 echo '<div id="gis_data_editor">';
119 __('Value for the column "%s"'),
120 htmlspecialchars($_REQUEST['field'])
124 echo '<input type="hidden" name="field" value="'
125 , htmlspecialchars($_REQUEST['field']) , '" />';
126 // The input field to which the final result should be added
127 // and corresponding null checkbox
128 if (isset($_REQUEST['input_name'])) {
129 echo '<input type="hidden" name="input_name" value="'
130 , htmlspecialchars($_REQUEST['input_name']) , '" />';
132 echo URL
::getHiddenInputs();
134 echo '<!-- Visualization section -->';
135 echo '<div id="placeholder" style="width:450px;height:300px;'
136 , ($srid != 0 ?
'display:none;' : '') , '">';
140 echo '<div id="openlayersmap" style="width:450px;height:300px;'
141 , ($srid == 0 ?
'display:none;' : '') , '">';
144 echo '<div class="choice" style="float:right;clear:right;">';
145 echo '<input type="checkbox" id="choice" value="useBaseLayer"'
146 , ($srid != 0 ?
' checked="checked"' : '') , '/>';
147 echo '<label for="choice">' , __("Use OpenStreetMaps as Base Layer") , '</label>';
150 echo '<script language="javascript" type="text/javascript">';
153 echo '<!-- End of visualization section -->';
156 echo '<!-- Header section - Inclueds GIS type selector and input field for SRID -->';
157 echo '<div id="gis_data_header">';
158 echo '<select name="gis_data[gis_type]" class="gis_type">';
159 foreach ($gis_types as $gis_type) {
160 echo '<option value="' , $gis_type , '"';
161 if ($geom_type == $gis_type) {
162 echo ' selected="selected"';
164 echo '>' , $gis_type , '</option>';
167 echo ' ';
168 /* l10n: Spatial Reference System Identifier */
169 echo '<label for="srid">' , __('SRID:') , '</label>';
170 echo '<input name="gis_data[srid]" type="text" value="' , $srid , '" />';
172 echo '<!-- End of header section -->';
174 echo '<!-- Data section -->';
175 echo '<div id="gis_data">';
178 if ($geom_type == 'GEOMETRYCOLLECTION') {
179 $geom_count = (isset($gis_data[$geom_type]['geom_count']))
180 ?
intval($gis_data[$geom_type]['geom_count']) : 1;
181 if (isset($gis_data[$geom_type]['add_geom'])) {
184 echo '<input type="hidden" name="gis_data[GEOMETRYCOLLECTION][geom_count]"'
185 , ' value="' , $geom_count , '" />';
188 for ($a = 0; $a < $geom_count; $a++
) {
189 if (! isset($gis_data[$a])) {
193 if ($geom_type == 'GEOMETRYCOLLECTION') {
195 printf(__('Geometry %d:'), $a +
1);
197 if (isset($gis_data[$a]['gis_type'])) {
198 $type = htmlspecialchars($gis_data[$a]['gis_type']);
200 $type = $gis_types[0];
202 echo '<select name="gis_data[' , $a , '][gis_type]" class="gis_type">';
203 foreach (array_slice($gis_types, 0, 6) as $gis_type) {
204 echo '<option value="' , $gis_type , '"';
205 if ($type == $gis_type) {
206 echo ' selected="selected"';
208 echo '>' , $gis_type , '</option>';
215 if ($type == 'POINT') {
218 echo '<label for="x">' , __("X") , '</label>';
219 echo '<input name="gis_data[' , $a , '][POINT][x]" type="text"'
220 , ' value="' , escape($gis_data[$a]['POINT']['x']) , '" />';
221 echo '<label for="y">' , __("Y") , '</label>';
222 echo '<input name="gis_data[' , $a , '][POINT][y]" type="text"'
223 , ' value="' , escape($gis_data[$a]['POINT']['y']) , '" />';
225 } elseif ($type == 'MULTIPOINT' ||
$type == 'LINESTRING') {
226 $no_of_points = isset($gis_data[$a][$type]['no_of_points'])
227 ?
intval($gis_data[$a][$type]['no_of_points']) : 1;
228 if ($type == 'LINESTRING' && $no_of_points < 2) {
231 if ($type == 'MULTIPOINT' && $no_of_points < 1) {
235 if (isset($gis_data[$a][$type]['add_point'])) {
238 echo '<input type="hidden" value="' , $no_of_points , '"'
239 , ' name="gis_data[' , $a , '][' , $type , '][no_of_points]" />';
241 for ($i = 0; $i < $no_of_points; $i++
) {
243 printf(__('Point %d'), $i +
1);
245 echo '<label for="x">' , __("X") , '</label>';
246 echo '<input type="text"'
247 , ' name="gis_data[' , $a , '][' , $type , '][' , $i , '][x]"'
248 , ' value="' , escape($gis_data[$a][$type][$i]['x']) , '" />';
249 echo '<label for="y">' , __("Y") , '</label>';
250 echo '<input type="text"'
251 , ' name="gis_data[' , $a , '][' , $type , '][' , $i , '][y]"'
252 , ' value="' , escape($gis_data[$a][$type][$i]['y']) , '" />';
254 echo '<input type="submit"'
255 , ' name="gis_data[' , $a , '][' , $type , '][add_point]"'
256 , ' class="add addPoint" value="' , __("Add a point") , '" />';
258 } elseif ($type == 'MULTILINESTRING' ||
$type == 'POLYGON') {
259 $no_of_lines = isset($gis_data[$a][$type]['no_of_lines'])
260 ?
intval($gis_data[$a][$type]['no_of_lines']) : 1;
261 if ($no_of_lines < 1) {
264 if (isset($gis_data[$a][$type]['add_line'])) {
267 echo '<input type="hidden" value="' , $no_of_lines , '"'
268 , ' name="gis_data[' , $a , '][' , $type , '][no_of_lines]" />';
270 for ($i = 0; $i < $no_of_lines; $i++
) {
272 if ($type == 'MULTILINESTRING') {
273 printf(__('Linestring %d:'), $i +
1);
276 echo __('Outer ring:');
278 printf(__('Inner ring %d:'), $i);
282 $no_of_points = isset($gis_data[$a][$type][$i]['no_of_points'])
283 ?
intval($gis_data[$a][$type][$i]['no_of_points']) : 2;
284 if ($type == 'MULTILINESTRING' && $no_of_points < 2) {
287 if ($type == 'POLYGON' && $no_of_points < 4) {
290 if (isset($gis_data[$a][$type][$i]['add_point'])) {
293 echo '<input type="hidden" value="' , $no_of_points , '"'
294 , ' name="gis_data[' , $a , '][' , $type , '][' , $i
295 , '][no_of_points]" />';
297 for ($j = 0; $j < $no_of_points; $j++
) {
299 printf(__('Point %d'), $j +
1);
301 echo '<label for="x">' , __("X") , '</label>';
302 echo '<input type="text" name="gis_data[' , $a , '][' , $type . ']['
303 , $i , '][' , $j , '][x]" value="'
304 , escape($gis_data[$a][$type][$i][$j]['x']) , '" />';
305 echo '<label for="y">' , __("Y") , '</label>';
306 echo '<input type="text" name="gis_data[' , $a , '][' , $type , ']['
307 , $i , '][' , $j , '][y]"' , ' value="'
308 , escape($gis_data[$a][$type][$i][$j]['y']) , '" />';
310 echo '<input type="submit" name="gis_data[' , $a , '][' , $type , ']['
311 , $i , '][add_point]"'
312 , ' class="add addPoint" value="' , __("Add a point") , '" />';
314 $caption = ($type == 'MULTILINESTRING')
315 ?
__('Add a linestring')
316 : __('Add an inner ring');
318 echo '<input type="submit"'
319 , ' name="gis_data[' , $a , '][' , $type , '][add_line]"'
320 , ' class="add addLine" value="' , $caption , '" />';
322 } elseif ($type == 'MULTIPOLYGON') {
323 $no_of_polygons = isset($gis_data[$a][$type]['no_of_polygons'])
324 ?
intval($gis_data[$a][$type]['no_of_polygons']) : 1;
325 if ($no_of_polygons < 1) {
328 if (isset($gis_data[$a][$type]['add_polygon'])) {
331 echo '<input type="hidden"'
332 , ' name="gis_data[' , $a , '][' , $type , '][no_of_polygons]"'
333 , ' value="' , $no_of_polygons , '" />';
335 for ($k = 0; $k < $no_of_polygons; $k++
) {
337 printf(__('Polygon %d:'), $k +
1);
338 $no_of_lines = isset($gis_data[$a][$type][$k]['no_of_lines'])
339 ?
intval($gis_data[$a][$type][$k]['no_of_lines']) : 1;
340 if ($no_of_lines < 1) {
343 if (isset($gis_data[$a][$type][$k]['add_line'])) {
346 echo '<input type="hidden"'
347 , ' name="gis_data[' , $a , '][' , $type , '][' , $k
348 , '][no_of_lines]"' , ' value="' , $no_of_lines , '" />';
350 for ($i = 0; $i < $no_of_lines; $i++
) {
353 echo __('Outer ring:');
355 printf(__('Inner ring %d:'), $i);
358 $no_of_points = isset($gis_data[$a][$type][$k][$i]['no_of_points'])
359 ?
intval($gis_data[$a][$type][$k][$i]['no_of_points']) : 4;
360 if ($no_of_points < 4) {
363 if (isset($gis_data[$a][$type][$k][$i]['add_point'])) {
366 echo '<input type="hidden"'
367 , ' name="gis_data[' , $a , '][' , $type , '][' , $k , '][' , $i
368 , '][no_of_points]"' , ' value="' , $no_of_points , '" />';
370 for ($j = 0; $j < $no_of_points; $j++
) {
372 printf(__('Point %d'), $j +
1);
374 echo '<label for="x">' , __("X") , '</label>';
375 echo '<input type="text"'
376 , ' name="gis_data[' , $a , '][' , $type , '][' , $k , ']['
377 , $i , '][' , $j , '][x]"'
378 , ' value="' , escape($gis_data[$a][$type][$k][$i][$j]['x'])
380 echo '<label for="y">' , __("Y") , '</label>';
381 echo '<input type="text"'
382 , ' name="gis_data[' , $a , '][' , $type , '][' , $k , ']['
383 , $i , '][' , $j , '][y]"'
384 , ' value="' , escape($gis_data[$a][$type][$k][$i][$j]['y'])
387 echo '<input type="submit"'
388 , ' name="gis_data[' , $a , '][' , $type , '][' , $k , '][' , $i
390 , ' class="add addPoint" value="' , __("Add a point") , '" />';
393 echo '<input type="submit"'
394 , ' name="gis_data[' , $a , '][' , $type , '][' , $k , '][add_line]"'
395 , ' class="add addLine" value="' , __('Add an inner ring') , '" />';
399 echo '<input type="submit"'
400 , ' name="gis_data[' , $a , '][' , $type , '][add_polygon]"'
401 , ' class="add addPolygon" value="' , __('Add a polygon') , '" />';
404 if ($geom_type == 'GEOMETRYCOLLECTION') {
406 echo '<input type="submit" name="gis_data[GEOMETRYCOLLECTION][add_geom]"'
407 , 'class="add addGeom" value="' , __("Add geometry") , '" />';
410 echo '<!-- End of data section -->';
413 echo '<input type="submit" name="gis_data[save]" value="' , __('Go') , '" />';
415 echo '<div id="gis_data_output">';
416 echo '<h3>' , __('Output') , '</h3>';
419 'Choose "GeomFromText" from the "Function" column and paste the'
420 . ' string below into the "Value" field.'
423 echo '<textarea id="gis_data_textarea" cols="95" rows="5">';
424 echo htmlspecialchars($result);
431 Response
::getInstance()->addJSON('gis_editor', ob_get_contents());