-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathconvert.php
238 lines (195 loc) · 9.12 KB
/
convert.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
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
<?php
//Copyright (C) 2019 Yunus Kahveci
//Have a look at the README.md and LICENSE files!
error_reporting(0);
try {
$files = glob('tmp/*'); // get all file names
foreach($files as $file){ // iterate files
if(is_file($file))
unlink($file); // delete file
}
if ($_FILES['file']['name'] <> "") {
$id = md5(base64_encode($_FILES['file']['tmp_name'] . time()));
$fileName = "$id.gpx";
if (file_exists("tmp/$fileName")) {
unlink("tmp/$fileName");
}
move_uploaded_file($_FILES['file']['tmp_name'], 'tmp/' . $fileName);
$data = file_get_contents('tmp/' . $fileName);
$p = xml_parser_create();
xml_parse_into_struct($p, $data, $values, $index);
xml_parser_free($p);
$content = '<?xml version="1.0" encoding="UTF-8"?>
<kml xmlns="http://www.opengis.net/kml/2.2" xmlns:gx="http://www.google.com/kml/ext/2.2" xmlns:kml="http://www.opengis.net/kml/2.2"
xmlns:atom="http://www.w3.org/2005/Atom">
<Document>
<name><![CDATA[' . str_replace(".gpx", "", $_FILES['file']['name']) . ']]></name>
<atom:link href="http://iOverlander.com"/>
<TimeStamp>
<when>2019-06-20T14:25:28-04:00</when>
</TimeStamp>
<description><![CDATA[<br/><p> </p><p><br/>Converted using <b><a href=\'http://gpx2kml.com\' title=\'Go to gpx2kml.com\'>gpx2kml.com</a></b></p><br/>]]></description>
<visibility>1</visibility>
<open>1</open>
<Style id="placemark-red">
<IconStyle>
<Icon>
<href>http://maps.me/placemarks/placemark-red.png</href>
</Icon>
</IconStyle>
</Style>
<Style id="placemark-blue">
<IconStyle>
<Icon>
<href>http://maps.me/placemarks/placemark-blue.png</href>
</Icon>
</IconStyle>
</Style>
<Style id="placemark-purple">
<IconStyle>
<Icon>
<href>http://maps.me/placemarks/placemark-purple.png</href>
</Icon>
</IconStyle>
</Style>
<Style id="placemark-yellow">
<IconStyle>
<Icon>
<href>http://maps.me/placemarks/placemark-yellow.png</href>
</Icon>
</IconStyle>
</Style>
<Style id="placemark-pink">
<IconStyle>
<Icon>
<href>http://maps.me/placemarks/placemark-pink.png</href>
</Icon>
</IconStyle>
</Style>
<Style id="placemark-brown">
<IconStyle>
<Icon>
<href>http://maps.me/placemarks/placemark-brown.png</href>
</Icon>
</IconStyle>
</Style>
<Style id="placemark-green">
<IconStyle>
<Icon>
<href>http://maps.me/placemarks/placemark-green.png</href>
</Icon>
</IconStyle>
</Style>
<Style id="placemark-orange">
<IconStyle>
<Icon>
<href>http://maps.me/placemarks/placemark-orange.png</href>
</Icon>
</IconStyle>
</Style>
<Folder>
<name>Waypoints</name>
<description>A list of waypoints</description>
<visibility>1</visibility>
<open>0</open>
';
foreach ($index['WPT'] as $WPTId) {
unset($ele);
if ($values[$WPTId]['type'] === "open" && $values[$WPTId]['level'] === 2) {
$convert = true;
$forSearchIndex = $WPTId + 1;
for ($forSearchInterrupt = false; !$forSearchInterrupt; $forSearchIndex++) {
if ($values[$forSearchIndex]['tag'] === "NAME" && $values[$forSearchIndex]['type'] === "complete") {
$name = str_replace("&", "&", $values[$forSearchIndex]['value']);
$name = str_replace("\"", """, $name);
} elseif ($values[$forSearchIndex]['tag'] === "ELE" && $values[$forSearchIndex]['type'] === "complete") {
$ele = $values[$forSearchIndex]['value'];
} elseif ($values[$forSearchIndex]['tag'] === "CMT" && $values[$forSearchIndex]['type'] === "complete") {
$desc = str_replace("&", "&", $values[$forSearchIndex]['value']);
$desc = str_replace("\"", """, $desc);
$desc = str_replace("<", "<", $desc);
$desc = str_replace(">", ">", $desc);
} elseif ($values[$forSearchIndex]['tag'] === "LINK" && $values[$forSearchIndex]['type'] === "open") {
$link = $values[$forSearchIndex]['attributes']['HREF'];
} elseif ($values[$forSearchIndex]['tag'] === "TYPE" && $values[$forSearchIndex]['type'] === "complete") {
$type = $values[$forSearchIndex]['value'];
} elseif ($values[$forSearchIndex]['type'] === "close" && $values[$forSearchIndex]['tag'] === "WPT") {
$forSearchInterrupt = true;
}
}
//debugging: auskommentiert, fehler sollte nicht hier sein
if ($type == "iOverlander Established Campground" || $type == "iOverlander Informal Campsite" || $type == "iOverlander Wild Camping") {
$color = "<styleUrl>#placemark-green</styleUrl>";
} elseif ($type == "iOverlander Hotel" || $type == "iOverlander Hostel") {
$color = "<styleUrl>#placemark-blue</styleUrl>";
} elseif ($type == "iOverlander Fuel Station" || $type == "iOverlander Propane" || $type == "iOverlander Mechanic and Parts" || $type == "iOverlander Water") {
$color = "<styleUrl>#placemark-purple</styleUrl>";
} elseif ($type == "iOverlander Sanitation Dump Station") {
$color = "<styleUrl>#placemark-brown</styleUrl>";
} elseif ($type == "iOverlander Restaurant" || $type == "iOverlander Tourist Attraction" || $type == "iOverlander Shopping" || $type == "iOverlander Medical" || $type == "iOverlander Pet Services" || $type == "iOverlander Laudromat") {
$color = "<styleUrl>#placemark-yellow</styleUrl>";
} elseif ($type == "iOverlander Customs and Immigration" || $type == "iOverlander Checkpoint" || $type == "iOverlander Consulate / Embassy") {
$color = "<styleUrl>#placemark-red</styleUrl>";
} elseif ($type == "iOverlander Vehicle Insurance" || $type == "iOverlander Vehicle Storage" || $type == "iOverlander Vehicle Shipping") {
$color = "<styleUrl>#placemark-orange</styleUrl>";
} elseif ($type == "iOverlander Warning" || $type == "iOverlander Other") {
$color = "<styleUrl>#placemark-pink</styleUrl>";
} else {
$color = "";
}
$chosenCategory = false;
$type = str_replace("iOverlander ", "", $type);
foreach ($_POST['categories'] as $category) {
if($category == $type) {
$chosenCategory = true;
}
}
if($chosenCategory) {
$content = $content . '<Placemark>
<name>' . $name . '</name>
<visibility>1</visibility>
<open>0</open>
'. $color .'
<atom:link href="' . $link . '"/>
<description>
- ' . $desc . ' </description>
<LookAt>
<longitude>' . $values[$WPTId]['attributes']['LON'] . '</longitude>
<latitude>' . $values[$WPTId]['attributes']['LAT'] . '</latitude>
<range>500</range>
<tilt>45</tilt>
<heading>0</heading>
</LookAt>
<Point>
<coordinates>
' . $values[$WPTId]['attributes']['LON'] . "," . $values[$WPTId]['attributes']['LAT']; if(isset($ele)) {$content = $content . ',' . $ele;} $content = $content . '
</coordinates>
</Point>
</Placemark>
';
}
}
}
$content = $content . '
</Folder>
</Document>
</kml>';
$convertedFileName = "tmp/" . str_replace(".gpx", "", $_FILES['file']['name']) . " - " . $id . ".kml";
if ($convert) {
$handle = fopen($convertedFileName, "w");
fwrite($handle, $content);
fclose($handle);
header("Location: $convertedFileName");
} else {
echo("No waypoints were found. Try another file.");
$noWayPoints = true;
}
} else {
header("Location: index.php");
die();
}
} finally {
if (!$noWayPoints) {
echo("An error occurred. Please try again later.");
}
}