gis_data_editor.php 15 KB

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