gis_data_editor.php 15 KB

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