source: subversion/applications/utils/export/poiexport/download.php @ 19407

Last change on this file since 19407 was 18412, checked in by rubke, 10 years ago

release 1.3
added geojson export (rullzer)

File size: 10.6 KB
Line 
1<?php
2/**
3 * Process a query on a postgis/postgresql OpenStreetMap database
4 * and return a file in the requested format
5 */
6
7
8// Don't forget to rename config.php.example!
9require ("config.php"); 
10
11// Check to see if all the required parameters are given
12if (!isset($_GET["output"]) || !isset($_GET["k"]) || !isset($_GET["v"]) || !isset($_GET["filename"])) {
13    die("Error, Not all parameters supplied");
14}
15
16// Parse GET parameters
17$pos = strpos(strtolower($_SERVER['HTTP_ACCEPT_CHARSET']),'utf-8');
18if ( $pos == false ) {
19  $output = pg_escape_string(utf8_encode($_GET["output"]));
20  $k = pg_escape_string(utf8_encode($_GET["k"]));
21  $v = pg_escape_string(utf8_encode($_GET["v"]));
22  $filename = pg_escape_string(utf8_encode($_GET["filename"]));
23} else {
24  $output = pg_escape_string($_GET["output"]);
25  $k = pg_escape_string($_GET["k"]);
26  $v = pg_escape_string($_GET["v"]);
27  $filename = pg_escape_string($_GET["filename"]);
28}
29
30// Connect to the postgresql database server
31$dbconn = pg_connect("host=" . $config['Database']['servername'] . 
32                     " port=" . $config['Database']['port'] . 
33                     " dbname=" . $config['Database']['dbname'] . 
34                     " user=".$config['Database']['username'] . 
35                     " password=" . $config['Database']['password']) 
36          or die('Could not connect: ' . pg_last_error());
37pg_set_client_encoding("UTF-8");
38
39// Initialize query
40$query = "SELECT name, lat, lon
41FROM (
42  SELECT name,
43         x(transform(way, 4326)) AS lon,
44         y(transform(way, 4326)) AS lat
45  FROM planet_osm_point
46  WHERE $k='$v'
47UNION
48  SELECT name,
49         x(centroid(transform(way, 4326))) AS lon,
50         y(centroid(transform(way, 4326))) AS lat
51  FROM planet_osm_polygon
52  WHERE $k='$v'
53) AS u1";
54
55// Run query
56$result = pg_query($query) or die('Query failed: ' . pg_last_error());
57
58switch($output) {
59  case "csv":
60    asGarmin($result, GenerateSafeFileName($filename . ".csv"));
61    break;
62  case "ov2":
63    asOv2($result, GenerateSafeFileName($filename . ".ov2"));
64    break;
65  case "gpx":
66    asGpx($result, GenerateSafeFileName($filename . ".gpx"));
67    break;
68  case "kml":
69    asKml($result, GenerateSafeFileName($filename . ".kml"));
70    break;
71  case "osm":
72    asOSM($result, GenerateSafeFileName(filename . ".osm"));
73    break;
74  case "wpt":
75    asOziExplorer($result, GenerateSafeFileName($filename . ".wpt"));
76    break;
77  case "geojson":
78    asGeoJSON($result, GenerateSafeFileName($filename . ".geojson"));
79    break;
80  default:
81    die("No valid output specified: $output");
82}
83
84pg_free_result($result);
85pg_close($dbconn);
86
87
88/**
89 * Create a Garmin specific csv file
90 *
91 * Reference: http://www.poi-factory.com/garmin-csv-file-format (no official file description unfortunatelly)
92 *
93 * @param postgresl resultset $data
94 * @param string $filename
95 */
96function asGarmin($data, $filename) {
97// Set headers
98  header("Pragma: public");
99  header("Expires: 0");
100  header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
101
102  header("Cache-Control: public");
103  header("Content-Description: File Transfer");
104  header("Content-Disposition: attachment; filename=$filename");
105  header("Content-Type: application/force-download; charset=utf-8");
106
107  while ($line = pg_fetch_array($data, null, PGSQL_ASSOC)) {
108    echo $line["lon"] . ", " .
109         $line["lat"] . ", \"" .
110         $line["name"] . "\"\n";
111  }
112}
113
114/**
115 * Create a tomtom specific ov2 file
116 *
117 * Reference: http://www.tomtom.com/lib/doc/ttnavsdk3_manual.pdf
118 *
119 * @param postgresql resultset $data
120 * @param string $filename
121 */
122function asOv2($data, $filename) {
123// Set headers
124  header("Pragma: public");
125  header("Expires: 0");
126  header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
127  header("Cache-Control: public");
128
129  header("Content-Description: File Transfer");
130  header("Content-Disposition: attachment; filename=$filename");
131  header("Content-Type: application/octet-stream");
132  header("Content-Transfer-Encoding: binary; charset=utf-8");
133
134  while ($line = pg_fetch_array($data, null, PGSQL_ASSOC)) {
135    echo chr(0x02) .
136         pack("V",strlen($line["name"]) + 14) .
137         pack("V",round($line["lon"] * 100000)) .
138         pack("V",round($line["lat"] * 100000)) .
139         utf8_decode($line["name"]) .
140         chr(0x00);
141  }
142}
143
144/**
145 * Create a generic GPX file
146 *
147 * Reference: http://www.topografix.com/gpx/1/1/
148 * @param postgresql resultset $data
149 * @param string $filename
150 */
151function asGpx($data, $filename) {
152// Set headers
153  header("Pragma: public");
154  header("Expires: 0");
155  header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
156  header("Cache-Control: public");
157
158  header("Content-Description: File Transfer");
159  header("Content-Disposition: attachment; filename=$filename");
160  header("Content-Type: application/force-download; charset=utf-8");
161
162  echo "<?xml version='1.0' encoding='UTF-8'?>\n" .
163       "<gpx version=\"1.1\" creator=\"OSM-NL POI export\" xmlns=\"http://www.topografix.com/GPX/1/1\"\n" .
164       "  xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\"\n" .
165       "  xsi:schemaLocation=\"http://www.topografix.com/GPX/1/1 http://www.topografix.com/GPX/1/1/gpx.xsd\">\n" .
166       "<metadata></metadata>\n";
167
168  while ($line = pg_fetch_array($data, null, PGSQL_ASSOC)) {
169    $name = htmlspecialchars($line["name"]);
170    echo "<wpt lat=\"" . $line["lat"] . "\" lon=\"" . $line["lon"] . "\">\n" .
171         "  <name>" . $name . "</name>\n" .
172         "</wpt>\n";
173  }
174  echo "</gpx>";
175}
176
177/**
178 * Create a Google Earth/Maps .kml file (2D)
179 *
180 * TODO: Add 3D option for Google Earth
181 * Reference: http://code.google.com/intl/nl/apis/kml/documentation/kmlreference.html
182 *
183 * @param postgresql resultset $data
184 * @param string $filename
185 */
186function asKml($data, $filename) {
187// Set headers
188  header("Pragma: public");
189  header("Expires: 0");
190  header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
191  header("Cache-Control: public");
192
193  header("Content-Description: File Transfer");
194  header("Content-Disposition: attachment; filename=$filename");
195  header("Content-Type: application/vnd.google-earth.kml+xml; charset=utf-8");
196
197  echo "<?xml version='1.0' encoding='UTF-8'?>\n<kml xmlns='http://earth.google.com/kml/2.2'>\n";
198  echo "<Document>\n";
199  while ($line = pg_fetch_array($data, null, PGSQL_ASSOC)) {
200    $name = htmlspecialchars($line["name"]);
201    echo "<Placemark>\n\t<name>" . $name . "</name>\n" .
202         "\t<Point>\n\t\t<coordinates>" . $line["lon"] . ", " . $line["lat"] . "</coordinates>\n\t</Point>\n</Placemark>\n";
203  }
204  echo "</Document>\n";
205  echo "</kml>";
206}
207
208/**
209 * Create a .osm file
210 *
211 * TODO: Needs fixing
212 * Reference: http://wiki.openstreetmap.org/wiki/.osm
213 *
214 * @param postgresql resultset $data
215 * @param string $filename
216 */
217function asOSM($data, $filename) {
218// Set headers
219  header("Pragma: public");
220  header("Expires: 0");
221  header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
222  header("Cache-Control: public");
223
224  header("Content-Description: File Transfer");
225  header("Content-Disposition: attachment; filename=$filename");
226  header("Content-Type: application/force-download; charset=utf-8");
227
228  echo '<?xml version="1.0" encoding="UTF-8"?>';
229  echo "\n";
230  echo '<osm version="0.6" generator="POI export">';
231  echo "\n";
232  echo '<bounds minlat="-180" minlon="-90" maxlat="180" maxlon="90"/>';
233  echo "\n";
234
235  while ($line = pg_fetch_array($data, null, PGSQL_ASSOC)) {
236    //$name = htmlentities($line["name"], ENT_QUOTES);
237    $name = htmlspecialchars($line["name"]);
238    echo '<node id="-1" lat="' . $line["lat"] . '" lon="' .  $line["lon"] . '" version="1" changeset="-1" user="poiexport" uid="-1" visible="true" timestamp="2008-12-17T01:18:42Z">';
239    echo "\n";
240    echo '<tag k="name" v="' . $name . '"/>';
241    echo "\n";
242    echo '</node>';
243    echo "\n";
244  }
245  echo "</osm>";
246}
247
248/**
249 * Create an OziExplorer specific wpt file
250 *
251 * Reference: oziexplorer help
252 *
253 * @param postgresl resultset $data
254 * @param string $filename
255 */
256function asOziExplorer($data, $filename) {
257// Set headers
258  header("Pragma: public");
259  header("Expires: 0");
260  header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
261  header("Cache-Control: public");
262
263  header("Content-Description: File Transfer");
264  header("Content-Disposition: attachment; filename=$filename");
265  header("Content-Type: application/force-download; charset=utf-8");
266
267  $counter = 1;
268  echo "OziExplorer Waypoint File Version 1.1\n";
269  echo "WGS 84\n";
270  echo "Reserved 2\n";
271  echo "Reserved 3\n";
272  while ($line = pg_fetch_array($data, null, PGSQL_ASSOC)) {
273  $name = str_replace(",", chr(209), $line["name"]); // substitude comma's, see docs
274  echo $counter++ . ", " . 
275       $name . ", " .
276       $line["lat"] . ", " .
277       $line["lon"] . ", " .
278       ",0,1,3,0,65535,,0,0,0,-777\n";
279  }
280}
281
282/**
283 * Create an GeoJSON file
284 *
285 * Reference: http://geojson.org/
286 *
287 * @param postgresl resultset $data
288 * @param string $filename
289 */
290function asGeoJSON($data, $filename) {
291  //Set Headers
292  header("Pragma: public");
293  header("Expires: 0");
294  header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
295  header("Cache-Control: public");
296
297  header("Content-Description: File Transfer");
298  header("Content-Disposition: attachment; filename=$filename");
299  header("Content-Type: application/force-download; charset=utf-8");
300
301  $result = array();
302  $result["type"] = "FeatureCollection";
303
304  $features = array();
305
306  while ($line = pg_fetch_array($data, null, PGSQL_ASSOC)) {
307    $point = array();
308    $point["type"] = "Feature";
309    $point["geometry"] = array();
310    $point["geometry"]["type"] = "Point";
311    $point["geometry"]["coordinates"] = array((double)($line["lon"]), (double)($line["lat"]));
312    $point["properties"] = array();
313    $point["properties"]["name"] = $line["name"];
314    $features[] = $point;
315  }
316
317  $result["features"] = $features;
318  echo json_encode($result);
319}
320
321function GenerateSafeFileName($filename) {
322  $filename = strtolower($filename);
323  $filename = str_replace("#","_",$filename);
324  $filename = str_replace(" ","_",$filename);
325  $filename = str_replace("'","",$filename);
326  $filename = str_replace('"',"",$filename);
327  $filename = str_replace("__","_",$filename);
328  $filename = str_replace("&","and",$filename);
329  $filename = str_replace("/","_",$filename);
330  $filename = str_replace("\\","_",$filename);
331  $filename = str_replace("?","",$filename);
332  return $filename;
333}
334?>
Note: See TracBrowser for help on using the repository browser.