-
Notifications
You must be signed in to change notification settings - Fork 0
/
CacheturBulkWaypointsFromLabsGPX.html
260 lines (237 loc) · 6.47 KB
/
CacheturBulkWaypointsFromLabsGPX.html
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
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>Generate Cachetur bulk waypoints for labs from GPX</title>
<link media="screen" href="style.css" type="text/css" rel="stylesheet">
</head>
<body>
<h2>Generate Cachetur bulk waypoints for labs from GPX</h2>
<h3>Choose a GPX file or zipped GPX file. Handles Cachly, GeeohGo and labs2gpx files.</h3>
<input type="file" id="file" name="file" /> Just show link:<input id="justLink" type="checkbox"> <span id="linecount"></span>
<br/>
<textarea id="list" rows="25" cols="80"></textarea>
<br><b>Reload page to run again</b>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.1.1/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jszip/3.9.1/jszip.min.js"></script>
<script type="text/javascript">
var textArea = document.createElement('area');
var output = [];
var coords = null;
var numLines = 0;
var geeohGo = false;
var shortName = "";
$("#file").on("change", function(evt) {
var files = evt.target.files;
if (files[0].name.endsWith(".zip")) {
handleZipFile(files[0]);
} else {
var reader = new FileReader();
reader.onload = function(evt) {
processXML(this.result);
};
reader.readAsText(files[0]);
}
// Closure to capture the file information.
function handleZipFile(f) {
getZipFilesContent(f).then(dd => {
processXML(dd);
}).catch(e => {
console.log(e);
});
}
});
async function getZipFilesContent(data) {
const zipContent = [];
const promises = [];
const zip = (await JSZip.loadAsync(data));
zip.forEach(async (relativePath, file) => {
const promise = file.async('string');
promises.push(promise);
zipContent.push({
file: relativePath,
content: await promise
});
});
await Promise.all(promises);
return zipContent[0].content;
}
function processXML(dd) {
var lines = dd.replace(/\n/g," ");
var wpts = lines.split('<wpt ');
var line = '';
var url = "";
if (lines.indexOf("<author>Geooh</author>") >= 0) {
processGeeohGoXML(lines);
} else {
for (var j=0; j < wpts.length; j++) {
var theWpts = wpts[j];
processWpt(theWpts);
}
updateView(output);
}
}
function processWpt(wpt) {
var re1 = /<name>(.*)<\/name>/gm;
var re2 = /lat=\"(-?[0-9\.]+)\"\s+lon=\"(-?[0-9\.]+)\"/gm;
var re3 = /<groundspeak:name>(.*)<\/groundspeak:name>/gm;
var re5 = /<url>(.*)<\/url>/gm;
var re4 = /<groundspeak:long_description html="(.*)">(.*)<\/groundspeak:long_description>/gm;
var m2 = re2.exec(wpt);
if (m2 !== null) {
var m4 = null;
var name = null;
var url = "";
line = wpt.replace(/\r/g," ");
var m1 = re1.exec(line);
var m3 = re3.exec(line);
var m4 = re4.exec(line);
var m5 = re5.exec(line);
if (m1 != null) {
shortName = m1[1];
}
if (m2 !== null) {
var lat = m2[1];
var lon = m2[2];
coords = lat + "," + lon;
}
if (m3 !== null) {
name = m3[1];
}
if (m5 !== null) {
// Temporary. Omit url as Cachly has incorrect one.
if (m5[1].indexOf("coord.info") === -1 ) {
url = m5[1];
}
}
m4[2] = m4[2].replace(/\n/,"");
if (m4[1] === "True") {
if ((name !== null) && (coords!== null) && (m4 !== null) && (url !== null)) {
m4 = stripHtml(m4[2]);
m4 = decodeHTMLEntities(m4);
}
var m4rest = m4.indexOf("Waypoint Description");
m4= m4.substring(m4rest+"Waypoint Description".length);
m4rest = m4.indexOf("Adventure Lab Descriptions");
m4 = m4.substring(0,m4rest);
} else {
m4 = m4[2];
}
var linkDesc;
if (document.getElementById('justLink').checked) {
linkDesc = url;
} else {
linkDesc = url + " " + m4;
}
if (linkDesc.length > 1024) {
linkDesc = linkDesc.substr(0, 1021) + "...";
}
var bulkLine = decodeHTMLEntities(name) + ';lab;' + coords + ';' + linkDesc + '\n';
if (shortName && shortName.indexOf("-") === -1) {
bulkLine = shortName + " " + bulkLine;
}
output.push(bulkLine);
numLines++;
textArea.innerHTML = "";
coords = null;
name = null;
m4 = null;
m5 = null;
}
}
function processGeeohGoXML(dd) {
var m1;
var m2;
var m3;
var m4 = null;
var m5 = null;
var alName = "";
var alUrl = "";
var line;
var lines = dd;
lines = lines.replace(/\r/gm,"");
lines = lines.split(/<wpt /);
for (var i = 1; i < lines.length; i++) {
var re1 = /<urlname>(.*)<\/urlname>/g;
var re2 = /lat=\"(-?[0-9\.]+)\"\s+lon=\"(-?[0-9\.]+)\"/g;
var re3 = /<name>(.*)<\/name>/g;
var re4 = /<desc>(.*)<\/desc>/g;
var re5 = /<url>(.*)<\/url>/g;
line = lines[i];
m1 = re1.exec(line);
// Get Overarching AL name
if(m1 && line.indexOf("<type>Geocache|Adventure Lab" >= 0)) {
alName = m1[1];
}
m5 = re5.exec(line);
if(m5 && line.indexOf("<type>Geocache|Adventure Lab" >= 0)) {
alUrl = m5[1];
}
if (line.indexOf("<groundspeak:owner id") >= 0 ||(line.indexOf("<type>Waypoint|") >= 0 && line.indexOf("<type>Waypoint|Stage") < 0 )) {
continue;
}
coords= null;
m2 = re2.exec(line);
m3 = re3.exec(line);
m4 = re4.exec(line);
if (m2 !== null) {
var lat = m2[1];
var lon = m2[2];
var coords = lat + "," + lon;
}
if (m3 !== null) {
name = m3[1];
}
if (m4 !== null) {
m4 = m4[1];
m4 = stripHtml(m4);
m4 = decodeHTMLEntities(m4);
var linkDesc = alUrl + " " + m4;
if (linkDesc.length > 1024) {
linkDesc = linkDesc.substr(0, 1021) + "...";
}
if (document.getElementById('justLink').checked) {
var linkDesc = alUrl;
} else {
var linkDesc = alUrl + " " + m4;
}
line = alName + ": " + decodeHTMLEntities(name) + ';lab;' + coords + ';' + linkDesc + '\n';
output.push(line);
numLines++;
textArea.innerHTML = "";
coords = null;
name = null;
m4 = null;
m5 = null;
}
}
updateView(output);
}
function updateView(output) {
var lineMsg = numLines + " " + "waypoints generated. ";
if (numLines > 300) {
lineMsg += "cachetur only handles up to 300 lines.";
}
var lineDiv = document.getElementById('linecount');
lineDiv.innerHTML = lineMsg;
var outarea = document.getElementById('list');
// Remove last newline
if (output) {
output[output.length -1] = output[output.length -1].trim();
outarea.value = output.join(' '),
outarea.setSelectionRange(0, 99999);
}
}
function decodeHTMLEntities(text) {
text=text.replace(/;/g, "");
text=text.replace(/"/g,"'");
textArea.innerHTML = text;
return textArea.innerText;
}
function stripHtml(text) {
textArea.innerHTML = text;
return textArea.textContent;
}
</script>
</body>
</html>