forked from phpmyadmin/phpmyadmin
-
Notifications
You must be signed in to change notification settings - Fork 0
/
tbl_gis_visualization.php
220 lines (195 loc) · 7.13 KB
/
tbl_gis_visualization.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
<?php
/* vim: set expandtab sw=4 ts=4 sts=4: */
/**
* handles creation of the GIS visualizations.
*
* @package PhpMyAdmin
*/
require_once 'libraries/common.inc.php';
// Runs common work
require_once 'libraries/db_common.inc.php';
$url_params['goto'] = $cfg['DefaultTabDatabase'];
$url_params['back'] = 'sql.php';
// Import visualization functions
require_once 'libraries/gis_visualization.lib.php';
$response = PMA_Response::getInstance();
// Throw error if no sql query is set
if (! isset($sql_query) || $sql_query == '') {
$response->isSuccess(false);
$response->addHTML(
PMA_Message::error(__('No SQL query was set to fetch data.'))
);
exit;
}
// Execute the query and return the result
$result = PMA_DBI_tryQuery($sql_query);
// Get the meta data of results
$meta = PMA_DBI_getFieldsMeta($result);
// Find the candidate fields for label column and spatial column
$labelCandidates = array(); $spatialCandidates = array();
foreach ($meta as $column_meta) {
if ($column_meta->type == 'geometry') {
$spatialCandidates[] = $column_meta->name;
} else {
$labelCandidates[] = $column_meta->name;
}
}
// Get settings if any posted
$visualizationSettings = array();
if (PMA_isValid($_REQUEST['visualizationSettings'], 'array')) {
$visualizationSettings = $_REQUEST['visualizationSettings'];
}
if (! isset($visualizationSettings['labelColumn']) && isset($labelCandidates[0])) {
$visualizationSettings['labelColumn'] = '';
}
// If spatial column is not set, use first geometric colum as spatial column
if (! isset($visualizationSettings['spatialColumn'])) {
$visualizationSettings['spatialColumn'] = $spatialCandidates[0];
}
// Convert geometric columns from bytes to text.
$modified_query = PMA_GIS_modifyQuery($sql_query, $visualizationSettings);
$modified_result = PMA_DBI_tryQuery($modified_query);
$data = array();
while ($row = PMA_DBI_fetchAssoc($modified_result)) {
$data[] = $row;
}
if (isset($_REQUEST['saveToFile'])) {
$response->disable();
$file_name = $_REQUEST['fileName'];
if ($file_name == '') {
$file_name = $visualizationSettings['spatialColumn'];
}
$save_format = $_REQUEST['fileFormat'];
PMA_GIS_saveToFile($data, $visualizationSettings, $save_format, $file_name);
exit();
}
$header = $response->getHeader();
$scripts = $header->getScripts();
$scripts->addFile('openlayers/OpenLayers.js');
$scripts->addFile('jquery/jquery.svg.js');
$scripts->addFile('tbl_gis_visualization.js');
$scripts->addFile('OpenStreetMap.js');
// If all the rows contain SRID, use OpenStreetMaps on the initial loading.
if (! isset($_REQUEST['displayVisualization'])) {
$visualizationSettings['choice'] = 'useBaseLayer';
foreach ($data as $row) {
if ($row['srid'] == 0) {
unset($visualizationSettings['choice']);
break;
}
}
}
$svg_support = (PMA_USR_BROWSER_AGENT == 'IE' && PMA_USR_BROWSER_VER <= 8)
? false : true;
$format = $svg_support ? 'svg' : 'png';
// get the chart and settings after chart generation
$visualization = PMA_GIS_visualizationResults(
$data, $visualizationSettings, $format
);
/**
* Displays the page
*/
?>
<!-- Display visualization options -->
<div id="div_view_options">
<fieldset>
<legend><?php echo __('Display GIS Visualization'); ?></legend>
<div style="width: 400px; float: left;">
<form method="post" action="tbl_gis_visualization.php">
<?php echo PMA_generate_common_hidden_inputs($url_params); ?>
<table class="gis_table">
<tr><td><label for="labelColumn"><?php echo __("Label column"); ?></label></td>
<td><select name="visualizationSettings[labelColumn]" id="labelColumn">
<option value=""><?php echo __("-- None --"); ?></option>
<?php
foreach ($labelCandidates as $labelCandidate) {
echo('<option value="' . htmlspecialchars($labelCandidate) . '"');
if ($labelCandidate == $visualizationSettings['labelColumn']) {
echo(' selected="selected"');
}
echo('>' . htmlspecialchars($labelCandidate) . '</option>');
}
?>
</select></td>
</tr>
<tr><td><label for="spatial Column"><?php echo __("Spatial column"); ?></label></td>
<td><select name="visualizationSettings[spatialColumn]" id="spatialColumn">
<?php
foreach ($spatialCandidates as $spatialCandidate) {
echo('<option value="' . htmlspecialchars($spatialCandidate) . '"');
if ($spatialCandidate == $visualizationSettings['spatialColumn']) {
echo(' selected="selected"');
}
echo('>' . htmlspecialchars($spatialCandidate) . '</option>');
}
?>
</select></td>
</tr>
<tr><td></td>
<td class="button"><input type="submit" name="displayVisualizationBtn" value="<?php echo __('Redraw'); ?>" /></td>
</tr>
<?php
if (! $GLOBALS['PMA_Config']->isHttps()) {
?>
<tr><td class="choice" colspan="2">
<input type="checkbox" name="visualizationSettings[choice]" id="choice" value="useBaseLayer"
<?php
if (isset($visualizationSettings['choice'])) {
echo(' checked="checked"');
}
?>
/>
<label for="choice"><?php echo __("Use OpenStreetMaps as Base Layer"); ?></label>
</td></tr>
<?php
}
?>
</table>
<input type="hidden" name="displayVisualization" value="redraw">
<input type="hidden" name="sql_query" value="<?php echo htmlspecialchars($sql_query); ?>" />
</form>
</div>
<div style="float:left;">
<form method="post" class="disableAjax" action="tbl_gis_visualization.php">
<?php echo PMA_generate_common_hidden_inputs($url_params); ?>
<table class="gis_table">
<tr><td><label for="fileName"><?php echo __("File name"); ?></label></td>
<td><input type="text" name="fileName" id="fileName" /></td>
</tr>
<tr><td><label for="fileFormat"><?php echo __("Format"); ?></label></td>
<td><select name="fileFormat" id="fileFormat">
<option value="png">PNG</option>
<option value="pdf">PDF</option>
<?php
if ($svg_support) {
echo ('<option value="svg" selected="selected">SVG</option>');
}
?>
</select></td>
</tr>
<tr><td></td>
<td class="button"><input type="submit" name="saveToFileBtn" value="<?php echo __('Download'); ?>" /></td>
</tr>
</table>
<input type="hidden" name="saveToFile" value="download">
<input type="hidden" name="sql_query" value="<?php echo htmlspecialchars($sql_query); ?>" />
</form>
</div>
<div style="clear:both;"> </div>
<div id="placeholder" style="width:<?php echo htmlspecialchars($visualizationSettings['width']); ?>px;height:<?php echo htmlspecialchars($visualizationSettings['height']); ?>px;">
<?php echo $visualization; ?>
</div>
<div id="openlayersmap"></div>
<input type="hidden" id="pmaThemeImage" value="<?php echo($GLOBALS['pmaThemeImage']); ?>" />
<script language="javascript" type="text/javascript">
function drawOpenLayers()
{
<?php
if (! $GLOBALS['PMA_Config']->isHttps()) {
echo (PMA_GIS_visualizationResults($data, $visualizationSettings, 'ol'));
}
?>
}
</script>
</fieldset>
</div>