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

Last change on this file since 18338 was 18338, checked in by milovanderlinden, 10 years ago

fixed kml errors and UTF-8 problems

File size: 7.3 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$output = pg_escape_string(utf8_encode($_GET["output"]));
18$k = pg_escape_string(utf8_encode($_GET["k"]));
19$v = pg_escape_string(utf8_encode($_GET["v"]));
20$filename = pg_escape_string(utf8_encode($_GET["filename"]));
21
22// Connect to the postgresql database server
23$dbconn = pg_connect("host=" . $config['Database']['servername'] . " port=" . $config['Database']['port'] . " dbname=" . $config['Database']['dbname'] . " user=".$config['Database']['username'] . " password=" . $config['Database']['password']) or die('Could not connect: ' . pg_last_error());
24pg_set_client_encoding("UTF-8");
25// Initialize query
26$query = "SELECT name, lat, lon
27FROM (
28  SELECT name,
29         x(transform(way, 4326)) AS lon,
30         y(transform(way, 4326)) AS lat
31  FROM planet_osm_point
32  WHERE $k='$v'
33UNION
34  SELECT name,
35         x(centroid(transform(way, 4326))) AS lon,
36         y(centroid(transform(way, 4326))) AS lat
37  FROM planet_osm_polygon
38  WHERE $k='$v'
39) AS u1";
40
41// Run query
42$result = pg_query($query) or die('Query failed: ' . pg_last_error());
43
44switch($output) {
45    case "csv":
46        asGarmin($result, $filename . ".csv");
47        break;
48    case "ov2":
49        asOv2($result, $filename . ".ov2");
50        break;
51    case "gpx":
52        asGpx($result, $filename . ".gpx");
53        break;
54    case "kml":
55        asKml($result, $filename . ".kml");
56        break;
57    case "osm":
58        asOSM($result, $filename . ".osm");
59        break;
60    default:
61        die("No valid output specified");
62}
63
64pg_free_result($result);
65pg_close($dbconn);
66
67/**
68 * Create a Garmin specific csv file
69 *
70 * @param postgresl resultset $data
71 * @param string $filename
72 */
73function asGarmin($data, $filename) {
74// Set headers
75    header("Pragma: public");
76    header("Expires: 0");
77    header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
78
79    header("Cache-Control: public");
80    header("Content-Description: File Transfer");
81    header("Content-Disposition: attachment; filename=$filename");
82    header("Content-Type: text/plain"); // Content-Type: text/html
83    //header("Content-Transfer-Encoding: binary");
84
85    while ($line = pg_fetch_array($data, null, PGSQL_ASSOC)) {
86        echo $line["lon"] . ", " .
87            $line["lat"] . ", \"" .
88            $line["name"] . "\"\n";
89    }
90}
91
92/**
93 * Create a tomtom specific ov2 file
94 *
95 * @param postgresql resultset $data
96 * @param string $filename
97 */
98function asOv2($data, $filename) {
99// Set headers
100    header("Pragma: public");
101    header("Expires: 0");
102    header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
103    header("Cache-Control: public");
104
105    header("Content-Description: File Transfer");
106    header("Content-Disposition: attachment; filename=$filename");
107    header("Content-Type: application/octet-stream"); // Content-Type: text/html
108    header("Content-Transfer-Encoding: binary");
109
110    while ($line = pg_fetch_array($data, null, PGSQL_ASSOC)) {
111        echo chr(0x02) .
112            pack("V",strlen($line["name"]) + 14) .
113            pack("V",round($line["lon"] * 100000)) .
114            pack("V",round($line["lat"] * 100000)) .
115            utf8_decode($line["name"]) .
116            chr(0x00);
117    }
118}
119
120/**
121 * Create a generic GPX file
122 *
123 * @param postgresql resultset $data
124 * @param string $filename
125 */
126function asGpx($data, $filename) {
127// Set headers
128    header("Pragma: public");
129    header("Expires: 0");
130    header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
131    header("Cache-Control: public");
132
133    header("Content-Description: File Transfer");
134    header("Content-Disposition: attachment; filename=$filename");
135    header("Content-Type: application/force-download"); // Content-Type: text/html
136    //header("Content-Transfer-Encoding: binary");
137
138    echo "<?xml version='1.0' encoding='UTF-8'?>\n" .
139        "<gpx version=\"1.1\" creator=\"OSM-NL POI export\" xmlns=\"http://www.topografix.com/GPX/1/1\"\n" .
140        "  xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\"\n" .
141        "  xsi:schemaLocation=\"http://www.topografix.com/GPX/1/1 http://www.topografix.com/GPX/1/1/gpx.xsd\">\n" .
142        "<metadata></metadata>\n";
143
144    while ($line = pg_fetch_array($data, null, PGSQL_ASSOC)) {
145        $name = htmlspecialchars($line["name"]);
146        echo "<wpt lat=\"" . $line["lat"] . "\" lon=\"" . $line["lon"] . "\">\n" .
147            "  <name>" . $name . "</name>\n" .
148            "</wpt>\n";
149    }
150    echo "</gpx>";
151}
152
153/**
154 * Create a Google *MAPS* file (2D)
155 *
156 * TODO: Add 3D option for Google Earth
157 *
158 * @param postgresql resultset $data
159 * @param string $filename
160 */
161function asKml($data, $filename) {
162// Set headers
163    header("Pragma: public");
164    header("Expires: 0");
165    header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
166    header("Cache-Control: public");
167
168    header("Content-Description: File Transfer");
169    header("Content-Disposition: attachment; filename=$filename");
170    header("Content-Type: application/vnd.google-earth.kml+xml"); // Content-Type: text/html
171
172    echo "<?xml version='1.0' encoding='UTF-8'?>\n<kml xmlns='http://earth.google.com/kml/2.2'>\n";
173    echo "<Document>\n";
174    while ($line = pg_fetch_array($data, null, PGSQL_ASSOC)) {
175       $name = htmlspecialchars($line["name"]);
176        echo "<Placemark>\n\t<name>" . $name . "</name>\n" .
177            "\t<Point>\n\t\t<coordinates>" . $line["lon"] . ", " . $line["lat"] . "</coordinates>\n\t</Point>\n</Placemark>\n";
178    }
179    echo "</Document>\n";
180    echo "</kml>";
181}
182
183/**
184 * Create a Google *MAPS* file (2D)
185 *
186 * TODO: Add 3D option for Google Earth
187 *
188 * @param postgresql resultset $data
189 * @param string $filename
190 */
191function asOSM($data, $filename) {
192// Set headers
193    header("Pragma: public");
194    header("Expires: 0");
195    header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
196    header("Cache-Control: public");
197
198    header("Content-Description: File Transfer");
199    header("Content-Disposition: attachment; filename=$filename");
200    header("Content-Type: text/xml"); // Content-Type: text/html
201
202    echo '<?xml version="1.0" encoding="UTF-8"?>';
203    echo "\n";
204    echo '<osm version="0.6" generator="POI export">';
205    echo "\n";
206    echo '<bounds minlat="-180" minlon="-90" maxlat="180" maxlon="90"/>';
207    echo "\n";
208
209    while ($line = pg_fetch_array($data, null, PGSQL_ASSOC)) {
210        //$name = htmlentities($line["name"], ENT_QUOTES);
211        $name = htmlspecialchars($line["name"]);
212        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">';
213        echo "\n";
214        echo '<tag k="name" v="' . $name . '"/>';
215        echo "\n";
216        echo '</node>';
217        echo "\n";
218    }
219    echo "</osm>";
220}
221?>
Note: See TracBrowser for help on using the repository browser.