tbl_gis_visualization.lib.php 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279
  1. <?php
  2. /* vim: set expandtab sw=4 ts=4 sts=4: */
  3. /**
  4. * Functions used to generate GIS visualizations.
  5. *
  6. * @package PhpMyAdmin
  7. */
  8. if (!defined('PHPMYADMIN')) {
  9. exit;
  10. }
  11. require_once 'libraries/sql.lib.php';
  12. /**
  13. * Returns a modified sql query with only the label column
  14. * and spatial column(wrapped with 'ASTEXT()' function).
  15. *
  16. * @param string $sql_query original sql query
  17. * @param array $visualizationSettings settings for the visualization
  18. *
  19. * @return string the modified sql query.
  20. */
  21. function PMA_GIS_modifyQuery($sql_query, $visualizationSettings)
  22. {
  23. $modified_query = 'SELECT ';
  24. // If label column is chosen add it to the query
  25. if (! empty($visualizationSettings['labelColumn'])) {
  26. $modified_query .= PMA_Util::backquote($visualizationSettings['labelColumn'])
  27. . ', ';
  28. }
  29. // Wrap the spatial column with 'ASTEXT()' function and add it
  30. $modified_query .= 'ASTEXT('
  31. . PMA_Util::backquote($visualizationSettings['spatialColumn'])
  32. . ') AS ' . PMA_Util::backquote($visualizationSettings['spatialColumn'])
  33. . ', ';
  34. // Get the SRID
  35. $modified_query .= 'SRID('
  36. . PMA_Util::backquote($visualizationSettings['spatialColumn'])
  37. . ') AS ' . PMA_Util::backquote('srid') . ' ';
  38. // Append the original query as the inner query
  39. $modified_query .= 'FROM (' . $sql_query . ') AS '
  40. . PMA_Util::backquote('temp_gis');
  41. return $modified_query;
  42. }
  43. /**
  44. * Formats a visualization for the GIS query results.
  45. *
  46. * @param array $data Data for the status chart
  47. * @param array &$visualizationSettings Settings used to generate the chart
  48. * @param string $format Format of the visulaization
  49. *
  50. * @return string|void HTML and JS code for the GIS visualization
  51. */
  52. function PMA_GIS_visualizationResults($data, &$visualizationSettings, $format)
  53. {
  54. include_once './libraries/gis/GIS_Visualization.class.php';
  55. include_once './libraries/gis/GIS_Factory.class.php';
  56. if (! isset($data[0])) {
  57. // empty data
  58. return __('No data found for GIS visualization.');
  59. } else {
  60. $visualization = new PMA_GIS_Visualization($data, $visualizationSettings);
  61. if ($visualizationSettings != null) {
  62. foreach ($visualization->getSettings() as $setting => $val) {
  63. if (! isset($visualizationSettings[$setting])) {
  64. $visualizationSettings[$setting] = $val;
  65. }
  66. }
  67. }
  68. if ($format == 'svg') {
  69. return $visualization->asSvg();
  70. } elseif ($format == 'png') {
  71. return $visualization->asPng();
  72. } elseif ($format == 'ol') {
  73. return $visualization->asOl();
  74. }
  75. }
  76. }
  77. /**
  78. * Generate visualization for the GIS query results and save it to a file.
  79. *
  80. * @param array $data data for the status chart
  81. * @param array $visualizationSettings settings used to generate the chart
  82. * @param string $format format of the visulaization
  83. * @param string $fileName file name
  84. *
  85. * @return file File containing the visualization
  86. */
  87. function PMA_GIS_saveToFile($data, $visualizationSettings, $format, $fileName)
  88. {
  89. include_once './libraries/gis/GIS_Visualization.class.php';
  90. include_once './libraries/gis/GIS_Factory.class.php';
  91. if (isset($data[0])) {
  92. $visualization = new PMA_GIS_Visualization($data, $visualizationSettings);
  93. if ($format == 'svg') {
  94. $visualization->toFileAsSvg($fileName);
  95. } elseif ($format == 'png') {
  96. $visualization->toFileAsPng($fileName);
  97. } elseif ($format == 'pdf') {
  98. $visualization->toFileAsPdf($fileName);
  99. }
  100. }
  101. }
  102. /**
  103. * Function to get html for the lebel column and spatial column
  104. *
  105. * @param string $column the column type. i.e either "labelColumn"
  106. * or "spatialColumn"
  107. * @param array $columnCandidates the list of select options
  108. * @param array $visualizationSettings visualization settings
  109. *
  110. * @return string $html
  111. */
  112. function PMA_getHtmlForColumn($column, $columnCandidates, $visualizationSettings)
  113. {
  114. $html = '<tr><td><label for="labelColumn">';
  115. $html .= ($column=="labelColumn") ? __("Label column") : __("Spatial column");
  116. $html .= '</label></td>';
  117. $html .= '<td><select name="visualizationSettings[' . $column . ']" id="'
  118. . $column . '">';
  119. if ($column == "labelColumn") {
  120. $html .= '<option value="">' . __("-- None --") . '</option>';
  121. }
  122. $html .= PMA_getHtmlForOptionsList(
  123. $columnCandidates, array($visualizationSettings[$column])
  124. );
  125. $html .= '</select></td>';
  126. $html .= '</tr>';
  127. return $html;
  128. }
  129. /**
  130. * Function to get HTML for the option of using open street maps
  131. *
  132. * @param boolean $isSelected the default value
  133. *
  134. * @return string HTML string
  135. */
  136. function PMA_getHtmlForUseOpenStreetMaps($isSelected)
  137. {
  138. $html = '<tr><td class="choice" colspan="2">';
  139. $html .= '<input type="checkbox" name="visualizationSettings[choice]"'
  140. . 'id="choice" value="useBaseLayer"';
  141. if ($isSelected) {
  142. $html .= ' checked="checked"';
  143. }
  144. $html .= '/>';
  145. $html .= '<label for="choice">';
  146. $html .= __("Use OpenStreetMaps as Base Layer");
  147. $html .= '</label>';
  148. $html .= '</td></tr>';
  149. return $html;
  150. }
  151. /**
  152. * Function to generate HTML for the GIS visualization page
  153. *
  154. * @param array $url_params url parameters
  155. * @param array $labelCandidates list of candidates for the label
  156. * @param array $spatialCandidates list of candidates for the spatial column
  157. * @param array $visualizationSettings visualization settings
  158. * @param String $sql_query the sql query
  159. * @param String $visualization HTML and js code for the visualization
  160. * @param boolean $svg_support whether svg download format is supported
  161. * @param array $data array of visualizing data
  162. *
  163. * @return string HTML code for the GIS visualization
  164. */
  165. function PMA_getHtmlForGisVisualization(
  166. $url_params, $labelCandidates, $spatialCandidates, $visualizationSettings,
  167. $sql_query, $visualization, $svg_support, $data
  168. ) {
  169. $html = '<div id="div_view_options">';
  170. $html .= '<fieldset>';
  171. $html .= '<legend>' . __('Display GIS Visualization') . '</legend>';
  172. $html .= '<div style="width: 400px; float: left;">';
  173. $html .= '<form method="post" action="tbl_gis_visualization.php">';
  174. $html .= PMA_URL_getHiddenInputs($url_params);
  175. $html .= '<table class="gis_table">';
  176. $html .= PMA_getHtmlForColumn(
  177. "labelColumn", $labelCandidates, $visualizationSettings
  178. );
  179. $html .= PMA_getHtmlForColumn(
  180. "spatialColumn", $spatialCandidates, $visualizationSettings
  181. );
  182. $html .= '<tr><td></td>';
  183. $html .= '<td class="button"><input type="submit"';
  184. $html .= ' name="displayVisualizationBtn" value="';
  185. $html .= __('Redraw');
  186. $html .= '" /></td></tr>';
  187. if (! $GLOBALS['PMA_Config']->isHttps()) {
  188. $isSelected = isset($visualizationSettings['choice']) ? true : false;
  189. $html .= PMA_getHtmlForUseOpenStreetMaps($isSelected);
  190. }
  191. $html .= '</table>';
  192. $html .= '<input type="hidden" name="displayVisualization" value="redraw">';
  193. $html .= '<input type="hidden" name="sql_query" value="';
  194. $html .= htmlspecialchars($sql_query) . '" />';
  195. $html .= '</form>';
  196. $html .= '</div>';
  197. $html .= '<div style="float:left;">';
  198. $html .= '<form method="post" class="disableAjax"';
  199. $html .= ' action="tbl_gis_visualization.php">';
  200. $html .= PMA_URL_getHiddenInputs($url_params);
  201. $html .= '<table class="gis_table">';
  202. $html .= '<tr><td><label for="fileName">';
  203. $html .= __("File name") . '</label></td>';
  204. $html .= '<td><input type="text" name="fileName" id="fileName" /></td></tr>';
  205. $html .= '<tr><td><label for="fileFormat">';
  206. $html .= __("Format") . '</label></td>';
  207. $html .= '<td><select name="fileFormat" id="fileFormat">';
  208. $html .= '<option value="png">PNG</option>';
  209. $html .= '<option value="pdf">PDF</option>';
  210. if ($svg_support) {
  211. $html .= '<option value="svg" selected="selected">SVG</option>';
  212. }
  213. $html .= '</select></td></tr>';
  214. $html .= '<tr><td></td>';
  215. $html .= '<td class="button"><input type="submit" name="saveToFileBtn" value="';
  216. $html .= __('Download') . '" /></td></tr>';
  217. $html .= '</table>';
  218. $html .= '<input type="hidden" name="saveToFile" value="download">';
  219. $html .= '<input type="hidden" name="sql_query" value="';
  220. $html .= htmlspecialchars($sql_query) . '" />';
  221. $html .= '</form>';
  222. $html .= '</div>';
  223. $html .= '<div style="clear:both;">&nbsp;</div>';
  224. $html .= '<div id="placeholder" style="width:';
  225. $html .= htmlspecialchars($visualizationSettings['width']) . 'px;height:';
  226. $html .= htmlspecialchars($visualizationSettings['height']) . 'px;">';
  227. $html .= $visualization;
  228. $html .= '</div>';
  229. $html .= '<div id="openlayersmap"></div>';
  230. $html .= '<input type="hidden" id="pmaThemeImage" value="';
  231. $html .= $GLOBALS['pmaThemeImage'] . '" />';
  232. $html .= '<script language="javascript" type="text/javascript">';
  233. $html .= 'function drawOpenLayers()';
  234. $html .= '{';
  235. if (! $GLOBALS['PMA_Config']->isHttps()) {
  236. $html .= PMA_GIS_visualizationResults($data, $visualizationSettings, 'ol');
  237. }
  238. $html .= '}';
  239. $html .= '</script>';
  240. $html .= '</fieldset>';
  241. $html .= '</div>';
  242. return $html;
  243. }
  244. ?>