gis_data_editor.php 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436
  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. 'mysqlVersion' => $GLOBALS['dbi']->getVersion()
  83. );
  84. $data = array(array('wkt' => $wkt_with_zero, 'srid' => $srid));
  85. $visualization = GisVisualization::getByData($data, $visualizationSettings)
  86. ->toImage('svg');
  87. $open_layers = GisVisualization::getByData($data, $visualizationSettings)
  88. ->asOl();
  89. // If the call is to update the WKT and visualization make an AJAX response
  90. if (isset($_POST['generate']) && $_POST['generate'] == true) {
  91. $extra_data = array(
  92. 'result' => $result,
  93. 'visualization' => $visualization,
  94. 'openLayers' => $open_layers,
  95. );
  96. $response = Response::getInstance();
  97. $response->addJSON($extra_data);
  98. exit;
  99. }
  100. ob_start();
  101. echo '<form id="gis_data_editor_form" action="gis_data_editor.php" method="post">';
  102. echo '<input type="hidden" id="pmaThemeImage"'
  103. , ' value="' , $GLOBALS['pmaThemeImage'] , '" />';
  104. echo '<div id="gis_data_editor">';
  105. echo '<h3>';
  106. printf(
  107. __('Value for the column "%s"'),
  108. htmlspecialchars($_POST['field'])
  109. );
  110. echo '</h3>';
  111. echo '<input type="hidden" name="field" value="'
  112. , htmlspecialchars($_POST['field']) , '" />';
  113. // The input field to which the final result should be added
  114. // and corresponding null checkbox
  115. if (isset($_POST['input_name'])) {
  116. echo '<input type="hidden" name="input_name" value="'
  117. , htmlspecialchars($_POST['input_name']) , '" />';
  118. }
  119. echo Url::getHiddenInputs();
  120. echo '<!-- Visualization section -->';
  121. echo '<div id="placeholder"'
  122. , ($srid != 0 ? 'class="hide"' : '') , '>';
  123. echo $visualization;
  124. echo '</div>';
  125. // No not remove inline style or it will cause "Cannot read property 'w' of null" on GIS editor map init
  126. echo '<div id="openlayersmap" style="width: ' . $visualizationSettings['width'] . 'px; height: ' . $visualizationSettings['height'] . 'px;" '
  127. , ($srid == 0 ? 'class="hide"' : '') , '>';
  128. echo '</div>';
  129. echo '<div class="choice floatright">';
  130. echo '<input type="checkbox" id="choice" value="useBaseLayer"'
  131. , ($srid != 0 ? ' checked="checked"' : '') , '/>';
  132. echo '<label for="choice">' , __("Use OpenStreetMaps as Base Layer") , '</label>';
  133. echo '</div>';
  134. echo '<script language="javascript" type="text/javascript">';
  135. echo $open_layers;
  136. echo '</script>';
  137. echo '<!-- End of visualization section -->';
  138. echo '<!-- Header section - Inclueds GIS type selector and input field for SRID -->';
  139. echo '<div id="gis_data_header">';
  140. echo '<select name="gis_data[gis_type]" class="gis_type">';
  141. foreach ($gis_types as $gis_type) {
  142. echo '<option value="' , $gis_type , '"';
  143. if ($geom_type == $gis_type) {
  144. echo ' selected="selected"';
  145. }
  146. echo '>' , $gis_type , '</option>';
  147. }
  148. echo '</select>';
  149. echo '&nbsp;&nbsp;&nbsp;&nbsp;';
  150. /* l10n: Spatial Reference System Identifier */
  151. echo '<label for="srid">' , __('SRID:') , '</label>';
  152. echo '<input name="gis_data[srid]" type="text" value="' , $srid , '" />';
  153. echo '</div>';
  154. echo '<!-- End of header section -->';
  155. echo '<!-- Data section -->';
  156. echo '<div id="gis_data">';
  157. $geom_count = 1;
  158. if ($geom_type == 'GEOMETRYCOLLECTION') {
  159. $geom_count = (isset($gis_data[$geom_type]['geom_count']))
  160. ? intval($gis_data[$geom_type]['geom_count']) : 1;
  161. if (isset($gis_data[$geom_type]['add_geom'])) {
  162. $geom_count++;
  163. }
  164. echo '<input type="hidden" name="gis_data[GEOMETRYCOLLECTION][geom_count]"'
  165. , ' value="' , $geom_count , '" />';
  166. }
  167. for ($a = 0; $a < $geom_count; $a++) {
  168. if (! isset($gis_data[$a])) {
  169. continue;
  170. }
  171. if ($geom_type == 'GEOMETRYCOLLECTION') {
  172. echo '<br/><br/>';
  173. printf(__('Geometry %d:'), $a + 1);
  174. echo '<br/>';
  175. if (isset($gis_data[$a]['gis_type'])) {
  176. $type = htmlspecialchars($gis_data[$a]['gis_type']);
  177. } else {
  178. $type = $gis_types[0];
  179. }
  180. echo '<select name="gis_data[' , $a , '][gis_type]" class="gis_type">';
  181. foreach (array_slice($gis_types, 0, 6) as $gis_type) {
  182. echo '<option value="' , $gis_type , '"';
  183. if ($type == $gis_type) {
  184. echo ' selected="selected"';
  185. }
  186. echo '>' , $gis_type , '</option>';
  187. }
  188. echo '</select>';
  189. } else {
  190. $type = $geom_type;
  191. }
  192. if ($type == 'POINT') {
  193. echo '<br/>';
  194. echo __('Point:');
  195. echo '<label for="x">' , __("X") , '</label>';
  196. echo '<input name="gis_data[' , $a , '][POINT][x]" type="text"'
  197. , ' value="' , escape($gis_data[$a]['POINT']['x']) , '" />';
  198. echo '<label for="y">' , __("Y") , '</label>';
  199. echo '<input name="gis_data[' , $a , '][POINT][y]" type="text"'
  200. , ' value="' , escape($gis_data[$a]['POINT']['y']) , '" />';
  201. } elseif ($type == 'MULTIPOINT' || $type == 'LINESTRING') {
  202. $no_of_points = isset($gis_data[$a][$type]['no_of_points'])
  203. ? intval($gis_data[$a][$type]['no_of_points']) : 1;
  204. if ($type == 'LINESTRING' && $no_of_points < 2) {
  205. $no_of_points = 2;
  206. }
  207. if ($type == 'MULTIPOINT' && $no_of_points < 1) {
  208. $no_of_points = 1;
  209. }
  210. if (isset($gis_data[$a][$type]['add_point'])) {
  211. $no_of_points++;
  212. }
  213. echo '<input type="hidden" value="' , $no_of_points , '"'
  214. , ' name="gis_data[' , $a , '][' , $type , '][no_of_points]" />';
  215. for ($i = 0; $i < $no_of_points; $i++) {
  216. echo '<br/>';
  217. printf(__('Point %d'), $i + 1);
  218. echo ': ';
  219. echo '<label for="x">' , __("X") , '</label>';
  220. echo '<input type="text"'
  221. , ' name="gis_data[' , $a , '][' , $type , '][' , $i , '][x]"'
  222. , ' value="' , escape($gis_data[$a][$type][$i]['x']) , '" />';
  223. echo '<label for="y">' , __("Y") , '</label>';
  224. echo '<input type="text"'
  225. , ' name="gis_data[' , $a , '][' , $type , '][' , $i , '][y]"'
  226. , ' value="' , escape($gis_data[$a][$type][$i]['y']) , '" />';
  227. }
  228. echo '<input type="submit"'
  229. , ' name="gis_data[' , $a , '][' , $type , '][add_point]"'
  230. , ' class="add addPoint" value="' , __("Add a point") , '" />';
  231. } elseif ($type == 'MULTILINESTRING' || $type == 'POLYGON') {
  232. $no_of_lines = isset($gis_data[$a][$type]['no_of_lines'])
  233. ? intval($gis_data[$a][$type]['no_of_lines']) : 1;
  234. if ($no_of_lines < 1) {
  235. $no_of_lines = 1;
  236. }
  237. if (isset($gis_data[$a][$type]['add_line'])) {
  238. $no_of_lines++;
  239. }
  240. echo '<input type="hidden" value="' , $no_of_lines , '"'
  241. , ' name="gis_data[' , $a , '][' , $type , '][no_of_lines]" />';
  242. for ($i = 0; $i < $no_of_lines; $i++) {
  243. echo '<br/>';
  244. if ($type == 'MULTILINESTRING') {
  245. printf(__('Linestring %d:'), $i + 1);
  246. } else {
  247. if ($i == 0) {
  248. echo __('Outer ring:');
  249. } else {
  250. printf(__('Inner ring %d:'), $i);
  251. }
  252. }
  253. $no_of_points = isset($gis_data[$a][$type][$i]['no_of_points'])
  254. ? intval($gis_data[$a][$type][$i]['no_of_points']) : 2;
  255. if ($type == 'MULTILINESTRING' && $no_of_points < 2) {
  256. $no_of_points = 2;
  257. }
  258. if ($type == 'POLYGON' && $no_of_points < 4) {
  259. $no_of_points = 4;
  260. }
  261. if (isset($gis_data[$a][$type][$i]['add_point'])) {
  262. $no_of_points++;
  263. }
  264. echo '<input type="hidden" value="' , $no_of_points , '"'
  265. , ' name="gis_data[' , $a , '][' , $type , '][' , $i
  266. , '][no_of_points]" />';
  267. for ($j = 0; $j < $no_of_points; $j++) {
  268. echo('<br/>');
  269. printf(__('Point %d'), $j + 1);
  270. echo ': ';
  271. echo '<label for="x">' , __("X") , '</label>';
  272. echo '<input type="text" name="gis_data[' , $a , '][' , $type . ']['
  273. , $i , '][' , $j , '][x]" value="'
  274. , escape($gis_data[$a][$type][$i][$j]['x']) , '" />';
  275. echo '<label for="y">' , __("Y") , '</label>';
  276. echo '<input type="text" name="gis_data[' , $a , '][' , $type , ']['
  277. , $i , '][' , $j , '][y]"' , ' value="'
  278. , escape($gis_data[$a][$type][$i][$j]['y']) , '" />';
  279. }
  280. echo '<input type="submit" name="gis_data[' , $a , '][' , $type , ']['
  281. , $i , '][add_point]"'
  282. , ' class="add addPoint" value="' , __("Add a point") , '" />';
  283. }
  284. $caption = ($type == 'MULTILINESTRING')
  285. ? __('Add a linestring')
  286. : __('Add an inner ring');
  287. echo '<br/>';
  288. echo '<input type="submit"'
  289. , ' name="gis_data[' , $a , '][' , $type , '][add_line]"'
  290. , ' class="add addLine" value="' , $caption , '" />';
  291. } elseif ($type == 'MULTIPOLYGON') {
  292. $no_of_polygons = isset($gis_data[$a][$type]['no_of_polygons'])
  293. ? intval($gis_data[$a][$type]['no_of_polygons']) : 1;
  294. if ($no_of_polygons < 1) {
  295. $no_of_polygons = 1;
  296. }
  297. if (isset($gis_data[$a][$type]['add_polygon'])) {
  298. $no_of_polygons++;
  299. }
  300. echo '<input type="hidden"'
  301. , ' name="gis_data[' , $a , '][' , $type , '][no_of_polygons]"'
  302. , ' value="' , $no_of_polygons , '" />';
  303. for ($k = 0; $k < $no_of_polygons; $k++) {
  304. echo '<br/>';
  305. printf(__('Polygon %d:'), $k + 1);
  306. $no_of_lines = isset($gis_data[$a][$type][$k]['no_of_lines'])
  307. ? intval($gis_data[$a][$type][$k]['no_of_lines']) : 1;
  308. if ($no_of_lines < 1) {
  309. $no_of_lines = 1;
  310. }
  311. if (isset($gis_data[$a][$type][$k]['add_line'])) {
  312. $no_of_lines++;
  313. }
  314. echo '<input type="hidden"'
  315. , ' name="gis_data[' , $a , '][' , $type , '][' , $k
  316. , '][no_of_lines]"' , ' value="' , $no_of_lines , '" />';
  317. for ($i = 0; $i < $no_of_lines; $i++) {
  318. echo '<br/><br/>';
  319. if ($i == 0) {
  320. echo __('Outer ring:');
  321. } else {
  322. printf(__('Inner ring %d:'), $i);
  323. }
  324. $no_of_points = isset($gis_data[$a][$type][$k][$i]['no_of_points'])
  325. ? intval($gis_data[$a][$type][$k][$i]['no_of_points']) : 4;
  326. if ($no_of_points < 4) {
  327. $no_of_points = 4;
  328. }
  329. if (isset($gis_data[$a][$type][$k][$i]['add_point'])) {
  330. $no_of_points++;
  331. }
  332. echo '<input type="hidden"'
  333. , ' name="gis_data[' , $a , '][' , $type , '][' , $k , '][' , $i
  334. , '][no_of_points]"' , ' value="' , $no_of_points , '" />';
  335. for ($j = 0; $j < $no_of_points; $j++) {
  336. echo '<br/>';
  337. printf(__('Point %d'), $j + 1);
  338. echo ': ';
  339. echo '<label for="x">' , __("X") , '</label>';
  340. echo '<input type="text"'
  341. , ' name="gis_data[' , $a , '][' , $type , '][' , $k , ']['
  342. , $i , '][' , $j , '][x]"'
  343. , ' value="' , escape($gis_data[$a][$type][$k][$i][$j]['x'])
  344. , '" />';
  345. echo '<label for="y">' , __("Y") , '</label>';
  346. echo '<input type="text"'
  347. , ' name="gis_data[' , $a , '][' , $type , '][' , $k , ']['
  348. , $i , '][' , $j , '][y]"'
  349. , ' value="' , escape($gis_data[$a][$type][$k][$i][$j]['y'])
  350. , '" />';
  351. }
  352. echo '<input type="submit"'
  353. , ' name="gis_data[' , $a , '][' , $type , '][' , $k , '][' , $i
  354. , '][add_point]"'
  355. , ' class="add addPoint" value="' , __("Add a point") , '" />';
  356. }
  357. echo '<br/>';
  358. echo '<input type="submit"'
  359. , ' name="gis_data[' , $a , '][' , $type , '][' , $k , '][add_line]"'
  360. , ' class="add addLine" value="' , __('Add an inner ring') , '" />';
  361. echo '<br/>';
  362. }
  363. echo '<br/>';
  364. echo '<input type="submit"'
  365. , ' name="gis_data[' , $a , '][' , $type , '][add_polygon]"'
  366. , ' class="add addPolygon" value="' , __('Add a polygon') , '" />';
  367. }
  368. }
  369. if ($geom_type == 'GEOMETRYCOLLECTION') {
  370. echo '<br/><br/>';
  371. echo '<input type="submit" name="gis_data[GEOMETRYCOLLECTION][add_geom]"'
  372. , 'class="add addGeom" value="' , __("Add geometry") , '" />';
  373. }
  374. echo '</div>';
  375. echo '<!-- End of data section -->';
  376. echo '<br/>';
  377. echo '<input type="submit" name="gis_data[save]" value="' , __('Go') , '" />';
  378. echo '<div id="gis_data_output">';
  379. echo '<h3>' , __('Output') , '</h3>';
  380. echo '<p>';
  381. echo __(
  382. 'Choose "ST_GeomFromText" from the "Function" column and paste the'
  383. . ' string below into the "Value" field.'
  384. );
  385. echo '</p>';
  386. echo '<textarea id="gis_data_textarea" cols="95" rows="5">';
  387. echo htmlspecialchars($result);
  388. echo '</textarea>';
  389. echo '</div>';
  390. echo '</div>';
  391. echo '</form>';
  392. Response::getInstance()->addJSON('gis_editor', ob_get_contents());
  393. ob_end_clean();