gis_data_editor.php 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424
  1. <?php
  2. /* vim: set expandtab sw=4 ts=4 sts=4: */
  3. /**
  4. * Editor for Geometry data types.
  5. *
  6. * @package PhpMyAdmin
  7. */
  8. /**
  9. * Escapes special characters if the variable is set.
  10. * Returns an empty string otherwise.
  11. *
  12. * @param string $variable variable to be escaped
  13. *
  14. * @return string escaped variable
  15. */
  16. function escape($variable)
  17. {
  18. return isset($variable) ? htmlspecialchars($variable) : '';
  19. }
  20. require_once 'libraries/common.inc.php';
  21. require_once 'libraries/gis/GIS_Factory.class.php';
  22. require_once 'libraries/gis/GIS_Visualization.class.php';
  23. require_once 'libraries/tbl_gis_visualization.lib.php';
  24. // Get data if any posted
  25. $gis_data = array();
  26. if (PMA_isValid($_REQUEST['gis_data'], 'array')) {
  27. $gis_data = $_REQUEST['gis_data'];
  28. }
  29. $gis_types = array(
  30. 'POINT',
  31. 'MULTIPOINT',
  32. 'LINESTRING',
  33. 'MULTILINESTRING',
  34. 'POLYGON',
  35. 'MULTIPOLYGON',
  36. 'GEOMETRYCOLLECTION'
  37. );
  38. // Extract type from the initial call and make sure that it's a valid one.
  39. // Extract from field's values if availbale, if not use the column type passed.
  40. if (! isset($gis_data['gis_type'])) {
  41. if (isset($_REQUEST['type']) && $_REQUEST['type'] != '') {
  42. $gis_data['gis_type'] = strtoupper($_REQUEST['type']);
  43. }
  44. if (isset($_REQUEST['value']) && trim($_REQUEST['value']) != '') {
  45. $start = (substr($_REQUEST['value'], 0, 1) == "'") ? 1 : 0;
  46. $gis_data['gis_type'] = substr(
  47. $_REQUEST['value'], $start, strpos($_REQUEST['value'], "(") - $start
  48. );
  49. }
  50. if ((! isset($gis_data['gis_type']))
  51. || (! in_array($gis_data['gis_type'], $gis_types))
  52. ) {
  53. $gis_data['gis_type'] = $gis_types[0];
  54. }
  55. }
  56. $geom_type = $gis_data['gis_type'];
  57. // Generate parameters from value passed.
  58. $gis_obj = PMA_GIS_Factory::factory($geom_type);
  59. if (isset($_REQUEST['value'])) {
  60. $gis_data = array_merge(
  61. $gis_data, $gis_obj->generateParams($_REQUEST['value'])
  62. );
  63. }
  64. // Generate Well Known Text
  65. $srid = (isset($gis_data['srid']) && $gis_data['srid'] != '')
  66. ? htmlspecialchars($gis_data['srid']) : 0;
  67. $wkt = $gis_obj->generateWkt($gis_data, 0);
  68. $wkt_with_zero = $gis_obj->generateWkt($gis_data, 0, '0');
  69. $result = "'" . $wkt . "'," . $srid;
  70. // Generate PNG or SVG based visualization
  71. $format = (PMA_USR_BROWSER_AGENT == 'IE' && PMA_USR_BROWSER_VER <= 8)
  72. ? 'png' : 'svg';
  73. $visualizationSettings = array(
  74. 'width' => 450,
  75. 'height' => 300,
  76. 'spatialColumn' => 'wkt'
  77. );
  78. $data = array(array('wkt' => $wkt_with_zero, 'srid' => $srid));
  79. $visualization = PMA_GIS_visualizationResults(
  80. $data, $visualizationSettings, $format
  81. );
  82. $open_layers = PMA_GIS_visualizationResults($data, $visualizationSettings, 'ol');
  83. // If the call is to update the WKT and visualization make an AJAX response
  84. if (isset($_REQUEST['generate']) && $_REQUEST['generate'] == true) {
  85. $extra_data = array(
  86. 'result' => $result,
  87. 'visualization' => $visualization,
  88. 'openLayers' => $open_layers,
  89. );
  90. $response = PMA_Response::getInstance();
  91. $response->addJSON($extra_data);
  92. exit;
  93. }
  94. ob_start();
  95. echo '<form id="gis_data_editor_form" action="gis_data_editor.php" method="post">';
  96. echo '<input type="hidden" id="pmaThemeImage"'
  97. . ' value="' . $GLOBALS['pmaThemeImage'] . '" />';
  98. echo '<div id="gis_data_editor">';
  99. echo '<h3>';
  100. printf(
  101. __('Value for the column "%s"'),
  102. htmlspecialchars($_REQUEST['field'])
  103. );
  104. echo '</h3>';
  105. echo '<input type="hidden" name="field" value="'
  106. . htmlspecialchars($_REQUEST['field']) . '" />';
  107. // The input field to which the final result should be added
  108. // and corresponding null checkbox
  109. if (isset($_REQUEST['input_name'])) {
  110. echo '<input type="hidden" name="input_name" value="'
  111. . htmlspecialchars($_REQUEST['input_name']) . '" />';
  112. }
  113. echo PMA_URL_getHiddenInputs();
  114. echo '<!-- Visualization section -->';
  115. echo '<div id="placeholder" style="width:450px;height:300px;'
  116. . ($srid != 0 ? 'display:none;' : '') . '">';
  117. echo $visualization;
  118. echo '</div>';
  119. echo '<div id="openlayersmap" style="width:450px;height:300px;'
  120. . ($srid == 0 ? 'display:none;' : '') . '">';
  121. echo '</div>';
  122. echo '<div class="choice" style="float:right;clear:right;">';
  123. echo '<input type="checkbox" id="choice" value="useBaseLayer"'
  124. . ($srid != 0 ? ' checked="checked"' : '') . '/>';
  125. echo '<label for="choice">' . __("Use OpenStreetMaps as Base Layer") . '</label>';
  126. echo '</div>';
  127. echo '<script language="javascript" type="text/javascript">';
  128. echo $open_layers;
  129. echo '</script>';
  130. echo '<!-- End of visualization section -->';
  131. echo '<!-- Header section - Inclueds GIS type selector and input field for SRID -->';
  132. echo '<div id="gis_data_header">';
  133. echo '<select name="gis_data[gis_type]" class="gis_type">';
  134. foreach ($gis_types as $gis_type) {
  135. echo '<option value="' . $gis_type . '"';
  136. if ($geom_type == $gis_type) {
  137. echo ' selected="selected"';
  138. }
  139. echo '>' . $gis_type . '</option>';
  140. }
  141. echo '</select>';
  142. echo '&nbsp;&nbsp;&nbsp;&nbsp;';
  143. /* l10n: Spatial Reference System Identifier */
  144. echo '<label for="srid">' . __('SRID:') . '</label>';
  145. echo '<input name="gis_data[srid]" type="text" value="' . $srid . '" />';
  146. echo '</div>';
  147. echo '<!-- End of header section -->';
  148. echo '<!-- Data section -->';
  149. echo '<div id="gis_data">';
  150. $geom_count = 1;
  151. if ($geom_type == 'GEOMETRYCOLLECTION') {
  152. $geom_count = (isset($gis_data[$geom_type]['geom_count']))
  153. ? $gis_data[$geom_type]['geom_count'] : 1;
  154. if (isset($gis_data[$geom_type]['add_geom'])) {
  155. $geom_count++;
  156. }
  157. echo '<input type="hidden" name="gis_data[GEOMETRYCOLLECTION][geom_count]"'
  158. . ' value="' . $geom_count . '" />';
  159. }
  160. for ($a = 0; $a < $geom_count; $a++) {
  161. if ($geom_type == 'GEOMETRYCOLLECTION') {
  162. echo '<br/><br/>';
  163. printf(__('Geometry %d:'), $a + 1);
  164. echo '<br/>';
  165. if (isset($gis_data[$a]['gis_type'])) {
  166. $type = $gis_data[$a]['gis_type'];
  167. } else {
  168. $type = $gis_types[0];
  169. }
  170. echo '<select name="gis_data[' . $a . '][gis_type]" class="gis_type">';
  171. foreach (array_slice($gis_types, 0, 6) as $gis_type) {
  172. echo '<option value="' . $gis_type . '"';
  173. if ($type == $gis_type) {
  174. echo ' selected="selected"';
  175. }
  176. echo '>' . $gis_type . '</option>';
  177. }
  178. echo '</select>';
  179. } else {
  180. $type = $geom_type;
  181. }
  182. if ($type == 'POINT') {
  183. echo '<br/>';
  184. echo __('Point:');
  185. echo '<label for="x">' . __("X") . '</label>';
  186. echo '<input name="gis_data[' . $a . '][POINT][x]" type="text"'
  187. . ' value="' . escape($gis_data[$a]['POINT']['x']) . '" />';
  188. echo '<label for="y">' . __("Y") . '</label>';
  189. echo '<input name="gis_data[' . $a . '][POINT][y]" type="text"'
  190. . ' value="' . escape($gis_data[$a]['POINT']['y']) . '" />';
  191. } elseif ($type == 'MULTIPOINT' || $type == 'LINESTRING') {
  192. $no_of_points = isset($gis_data[$a][$type]['no_of_points'])
  193. ? $gis_data[$a][$type]['no_of_points'] : 1;
  194. if ($type == 'LINESTRING' && $no_of_points < 2) {
  195. $no_of_points = 2;
  196. }
  197. if ($type == 'MULTIPOINT' && $no_of_points < 1) {
  198. $no_of_points = 1;
  199. }
  200. if (isset($gis_data[$a][$type]['add_point'])) {
  201. $no_of_points++;
  202. }
  203. echo '<input type="hidden" value="' . $no_of_points . '"'
  204. . ' name="gis_data[' . $a . '][' . $type . '][no_of_points]" />';
  205. for ($i = 0; $i < $no_of_points; $i++) {
  206. echo '<br/>';
  207. printf(__('Point %d'), $i + 1);
  208. echo ': ';
  209. echo '<label for="x">' . __("X") . '</label>';
  210. echo '<input type="text"'
  211. . ' name="gis_data[' . $a . '][' . $type . '][' . $i . '][x]"'
  212. . ' value="' . escape($gis_data[$a][$type][$i]['x']) . '" />';
  213. echo '<label for="y">' . __("Y") . '</label>';
  214. echo '<input type="text"'
  215. . ' name="gis_data[' . $a . '][' . $type . '][' . $i . '][y]"'
  216. . ' value="' . escape($gis_data[$a][$type][$i]['y']) . '" />';
  217. }
  218. echo '<input type="submit"'
  219. . ' name="gis_data[' . $a . '][' . $type . '][add_point]"'
  220. . ' class="add addPoint" value="' . __("Add a point") . '" />';
  221. } elseif ($type == 'MULTILINESTRING' || $type == 'POLYGON') {
  222. $no_of_lines = isset($gis_data[$a][$type]['no_of_lines'])
  223. ? $gis_data[$a][$type]['no_of_lines'] : 1;
  224. if ($no_of_lines < 1) {
  225. $no_of_lines = 1;
  226. }
  227. if (isset($gis_data[$a][$type]['add_line'])) {
  228. $no_of_lines++;
  229. }
  230. echo '<input type="hidden" value="' . $no_of_lines . '"'
  231. . ' name="gis_data[' . $a . '][' . $type . '][no_of_lines]" />';
  232. for ($i = 0; $i < $no_of_lines; $i++) {
  233. echo '<br/>';
  234. if ($type == 'MULTILINESTRING') {
  235. printf(__('Linestring %d:'), $i + 1);
  236. } else {
  237. if ($i == 0) {
  238. echo __('Outer ring:');
  239. } else {
  240. printf(__('Inner ring %d:'), $i);
  241. }
  242. }
  243. $no_of_points = isset($gis_data[$a][$type][$i]['no_of_points'])
  244. ? $gis_data[$a][$type][$i]['no_of_points'] : 2;
  245. if ($type == 'MULTILINESTRING' && $no_of_points < 2) {
  246. $no_of_points = 2;
  247. }
  248. if ($type == 'POLYGON' && $no_of_points < 4) {
  249. $no_of_points = 4;
  250. }
  251. if (isset($gis_data[$a][$type][$i]['add_point'])) {
  252. $no_of_points++;
  253. }
  254. echo '<input type="hidden" value="' . $no_of_points . '"'
  255. . ' name="gis_data[' . $a . '][' . $type . '][' . $i
  256. . '][no_of_points]" />';
  257. for ($j = 0; $j < $no_of_points; $j++) {
  258. echo('<br/>');
  259. printf(__('Point %d'), $j + 1);
  260. echo ': ';
  261. echo '<label for="x">' . __("X") . '</label>';
  262. echo '<input type="text" name="gis_data[' . $a . '][' . $type . ']['
  263. . $i . '][' . $j . '][x]" value="'
  264. . escape($gis_data[$a][$type][$i][$j]['x']) . '" />';
  265. echo '<label for="y">' . __("Y") . '</label>';
  266. echo '<input type="text" name="gis_data[' . $a . '][' . $type . ']['
  267. . $i . '][' . $j . '][y]"' . ' value="'
  268. . escape($gis_data[$a][$type][$i][$j]['x']) . '" />';
  269. }
  270. echo '<input type="submit" name="gis_data[' . $a . '][' . $type . ']['
  271. . $i . '][add_point]"'
  272. . ' class="add addPoint" value="' . __("Add a point") . '" />';
  273. }
  274. $caption = ($type == 'MULTILINESTRING')
  275. ? __('Add a linestring')
  276. : __('Add an inner ring');
  277. echo '<br/>';
  278. echo '<input type="submit"'
  279. . ' name="gis_data[' . $a . '][' . $type . '][add_line]"'
  280. . ' class="add addLine" value="' . $caption . '" />';
  281. } elseif ($type == 'MULTIPOLYGON') {
  282. $no_of_polygons = isset($gis_data[$a][$type]['no_of_polygons'])
  283. ? $gis_data[$a][$type]['no_of_polygons'] : 1;
  284. if ($no_of_polygons < 1) {
  285. $no_of_polygons = 1;
  286. }
  287. if (isset($gis_data[$a][$type]['add_polygon'])) {
  288. $no_of_polygons++;
  289. }
  290. echo '<input type="hidden"'
  291. . ' name="gis_data[' . $a . '][' . $type . '][no_of_polygons]"'
  292. . ' value="' . $no_of_polygons . '" />';
  293. for ($k = 0; $k < $no_of_polygons; $k++) {
  294. echo '<br/>';
  295. printf(__('Polygon %d:'), $k + 1);
  296. $no_of_lines = isset($gis_data[$a][$type][$k]['no_of_lines'])
  297. ? $gis_data[$a][$type][$k]['no_of_lines'] : 1;
  298. if ($no_of_lines < 1) {
  299. $no_of_lines = 1;
  300. }
  301. if (isset($gis_data[$a][$type][$k]['add_line'])) {
  302. $no_of_lines++;
  303. }
  304. echo '<input type="hidden"'
  305. . ' name="gis_data[' . $a . '][' . $type . '][' . $k
  306. . '][no_of_lines]"' . ' value="' . $no_of_lines . '" />';
  307. for ($i = 0; $i < $no_of_lines; $i++) {
  308. echo '<br/><br/>';
  309. if ($i == 0) {
  310. echo __('Outer ring:');
  311. } else {
  312. printf(__('Inner ring %d:'), $i);
  313. }
  314. $no_of_points = isset($gis_data[$a][$type][$k][$i]['no_of_points'])
  315. ? $gis_data[$a][$type][$k][$i]['no_of_points'] : 4;
  316. if ($no_of_points < 4) {
  317. $no_of_points = 4;
  318. }
  319. if (isset($gis_data[$a][$type][$k][$i]['add_point'])) {
  320. $no_of_points++;
  321. }
  322. echo '<input type="hidden"'
  323. . ' name="gis_data[' . $a . '][' . $type . '][' . $k . '][' . $i
  324. . '][no_of_points]"' . ' value="' . $no_of_points . '" />';
  325. for ($j = 0; $j < $no_of_points; $j++) {
  326. echo '<br/>';
  327. printf(__('Point %d'), $j + 1);
  328. echo ': ';
  329. echo '<label for="x">' . __("X") . '</label>';
  330. echo '<input type="text"'
  331. . ' name="gis_data[' . $a . '][' . $type . '][' . $k . ']['
  332. . $i . '][' . $j . '][x]"'
  333. . ' value="' . escape($gis_data[$a][$type][$k][$i][$j]['x'])
  334. . '" />';
  335. echo '<label for="y">' . __("Y") . '</label>';
  336. echo '<input type="text"'
  337. . ' name="gis_data[' . $a . '][' . $type . '][' . $k . ']['
  338. . $i . '][' . $j . '][y]"'
  339. . ' value="' . escape($gis_data[$a][$type][$k][$i][$j]['y'])
  340. . '" />';
  341. }
  342. echo '<input type="submit"'
  343. . ' name="gis_data[' . $a . '][' . $type . '][' . $k . '][' . $i
  344. . '][add_point]"'
  345. . ' class="add addPoint" value="' . __("Add a point") . '" />';
  346. }
  347. echo '<br/>';
  348. echo '<input type="submit"'
  349. . ' name="gis_data[' . $a . '][' . $type . '][' . $k . '][add_line]"'
  350. . ' class="add addLine" value="' . __('Add an inner ring') . '" />';
  351. echo '<br/>';
  352. }
  353. echo '<br/>';
  354. echo '<input type="submit"'
  355. . ' name="gis_data[' . $a . '][' . $type . '][add_polygon]"'
  356. . ' class="add addPolygon" value="' . __('Add a polygon') . '" />';
  357. }
  358. }
  359. if ($geom_type == 'GEOMETRYCOLLECTION') {
  360. echo '<br/><br/>';
  361. echo '<input type="submit" name="gis_data[GEOMETRYCOLLECTION][add_geom]"'
  362. . 'class="add addGeom" value="' . __("Add geometry") . '" />';
  363. }
  364. echo '</div>';
  365. echo '<!-- End of data section -->';
  366. echo '<br/>';
  367. echo '<input type="submit" name="gis_data[save]" value="' . __('Go') . '" />';
  368. echo '<div id="gis_data_output">';
  369. echo '<h3>' . __('Output') . '</h3>';
  370. echo '<p>';
  371. echo __(
  372. 'Choose "GeomFromText" from the "Function" column and paste the'
  373. . ' string below into the "Value" field.'
  374. );
  375. echo '</p>';
  376. echo '<textarea id="gis_data_textarea" cols="95" rows="5">';
  377. echo $result;
  378. echo '</textarea>';
  379. echo '</div>';
  380. echo '</div>';
  381. echo '</form>';
  382. PMA_Response::getInstance()->addJSON('gis_editor', ob_get_contents());
  383. ob_end_clean();
  384. ?>