1 |
/**
|
2 |
* ZANavi, Zoff Android Navigation system.
|
3 |
* Copyright (C) 2012 Zoff <zoff@zoff.cc>
|
4 |
*
|
5 |
* This program is free software; you can redistribute it and/or
|
6 |
* modify it under the terms of the GNU General Public License
|
7 |
* version 2 as published by the Free Software Foundation.
|
8 |
*
|
9 |
* This program is distributed in the hope that it will be useful,
|
10 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
11 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
12 |
* GNU General Public License for more details.
|
13 |
*
|
14 |
* You should have received a copy of the GNU General Public License
|
15 |
* along with this program; if not, write to the
|
16 |
* Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
|
17 |
* Boston, MA 02110-1301, USA.
|
18 |
*/
|
19 |
|
20 |
/**
|
21 |
* Copyright (C) 2011 Dipl.-Ing.(FH) Thomas Schindel, Bad Elster
|
22 |
*
|
23 |
* This program is free software; you can redistribute it and/or
|
24 |
* modify it under the terms of the GNU General Public License
|
25 |
* version 2 as published by the Free Software Foundation.
|
26 |
*
|
27 |
* This program is distributed in the hope that it will be useful,
|
28 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
29 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
30 |
* GNU General Public License for more details.
|
31 |
*
|
32 |
* You should have received a copy of the GNU General Public License
|
33 |
* along with this program; if not, write to the
|
34 |
* Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
|
35 |
* Boston, MA 02110-1301, USA.
|
36 |
*/
|
37 |
|
38 |
package de.oberoner.gpx2navit_txt;
|
39 |
|
40 |
import itcrowd.gps.util.DistanceUtil;
|
41 |
|
42 |
import java.io.BufferedWriter;
|
43 |
import java.io.File;
|
44 |
import java.io.IOException;
|
45 |
import java.text.DecimalFormat;
|
46 |
import java.util.ArrayList;
|
47 |
|
48 |
import javax.xml.parsers.DocumentBuilder;
|
49 |
import javax.xml.parsers.DocumentBuilderFactory;
|
50 |
import javax.xml.parsers.ParserConfigurationException;
|
51 |
|
52 |
import org.w3c.dom.Document;
|
53 |
import org.w3c.dom.Element;
|
54 |
import org.w3c.dom.Node;
|
55 |
import org.w3c.dom.NodeList;
|
56 |
import org.xml.sax.SAXException;
|
57 |
|
58 |
/**
|
59 |
* Created by IntelliJ IDEA.
|
60 |
* User: Dipl.-Ing.(FH) Thomas Schindel, Bad Elster
|
61 |
* Date: 06.11.11
|
62 |
* Time: 15:37
|
63 |
* To change this template use File | Settings | File Templates.
|
64 |
*/
|
65 |
public class GpxDOM
|
66 |
{
|
67 |
protected Document gpxDoc;
|
68 |
|
69 |
public class GPX_Track
|
70 |
{
|
71 |
ArrayList<NavitTrkptHeader> h;
|
72 |
ArrayList<NavitTrkptList> i;
|
73 |
}
|
74 |
|
75 |
public GpxDOM(String GpxFilePath)
|
76 |
{
|
77 |
gpxDoc = null;
|
78 |
|
79 |
try
|
80 |
{
|
81 |
File inFile = new File(GpxFilePath);
|
82 |
|
83 |
DocumentBuilderFactory docFactory = DocumentBuilderFactory.newInstance();
|
84 |
DocumentBuilder docBuilder = docFactory.newDocumentBuilder();
|
85 |
gpxDoc = docBuilder.parse(inFile);
|
86 |
|
87 |
// Check if file is really a GPX-File
|
88 |
Element gpxElement = gpxDoc.getDocumentElement();
|
89 |
if (!"gpx".equals(gpxElement.getNodeName()))
|
90 |
{
|
91 |
gpxDoc = null;
|
92 |
throw new SAXException("This is not a GPX file: " + gpxElement.getNodeName());
|
93 |
}
|
94 |
}
|
95 |
catch (ParserConfigurationException e)
|
96 |
{
|
97 |
e.printStackTrace();
|
98 |
}
|
99 |
catch (SAXException e)
|
100 |
{
|
101 |
e.printStackTrace();
|
102 |
}
|
103 |
catch (IOException e)
|
104 |
{
|
105 |
e.printStackTrace();
|
106 |
}
|
107 |
}
|
108 |
|
109 |
public GPX_Track getTrackLabel()
|
110 |
{
|
111 |
NodeList nameList = gpxDoc.getElementsByTagName("trk");
|
112 |
ArrayList<NavitTrkptHeader> header = new ArrayList<NavitTrkptHeader>();
|
113 |
ArrayList<NavitTrkptList> navitTrkptRecList = new ArrayList<NavitTrkptList>();
|
114 |
GPX_Track obj = new GPX_Track();
|
115 |
|
116 |
if (nameList.getLength() > 0)
|
117 |
{
|
118 |
for (int ii = 0; ii < nameList.getLength(); ii++)
|
119 |
{
|
120 |
org.w3c.dom.Node nn = nameList.item(ii);
|
121 |
if (nn.getNodeType() == Node.ELEMENT_NODE)
|
122 |
{
|
123 |
try
|
124 |
{
|
125 |
Element firstElement = (Element) nn;
|
126 |
NodeList nameElementList = firstElement.getElementsByTagName("name");
|
127 |
Element nameElement = (Element) nameElementList.item(0);
|
128 |
NodeList name = nameElement.getChildNodes();
|
129 |
// System.out.println("ii:" + ii + ":" + name.item(0).getNodeValue());
|
130 |
NavitTrkptHeader h = new NavitTrkptHeader();
|
131 |
h.setLabel("\"" + MainFrame.remove_special_chars(name.item(0).getNodeValue()) + "\"");
|
132 |
header.add(h);
|
133 |
}
|
134 |
catch (Exception e)
|
135 |
{
|
136 |
|
137 |
}
|
138 |
}
|
139 |
|
140 |
//else
|
141 |
{
|
142 |
try
|
143 |
{
|
144 |
Element firstElement = (Element) nn;
|
145 |
NodeList nodeList = firstElement.getElementsByTagName("trkpt");
|
146 |
if (nodeList.getLength() > 0)
|
147 |
{
|
148 |
NavitTrkptList tpl = new NavitTrkptList();
|
149 |
|
150 |
for (int jj = 0; jj < nodeList.getLength(); jj++)
|
151 |
{
|
152 |
NavitTrkptRec navitTrkptRec = new NavitTrkptRec();
|
153 |
Node node = nodeList.item(jj);
|
154 |
if (node.getNodeType() == Node.ELEMENT_NODE)
|
155 |
{
|
156 |
try
|
157 |
{
|
158 |
Element element = (Element) nodeList.item(jj);
|
159 |
//System.out.println("jj:" + jj + ":" + element.getAttribute("lat") + "," + element.getAttribute("lon"));
|
160 |
navitTrkptRec.setLat(element.getAttribute("lat"));
|
161 |
navitTrkptRec.setLon(element.getAttribute("lon"));
|
162 |
}
|
163 |
catch (Exception e)
|
164 |
{
|
165 |
|
166 |
}
|
167 |
}
|
168 |
tpl.add(navitTrkptRec);
|
169 |
}
|
170 |
navitTrkptRecList.add(tpl);
|
171 |
}
|
172 |
}
|
173 |
catch (Exception e)
|
174 |
{
|
175 |
|
176 |
}
|
177 |
}
|
178 |
}
|
179 |
}
|
180 |
|
181 |
obj.h = header;
|
182 |
obj.i = navitTrkptRecList;
|
183 |
|
184 |
return obj;
|
185 |
}
|
186 |
|
187 |
public NavitWptList getWptList()
|
188 |
{
|
189 |
|
190 |
/*
|
191 |
* <wpt lon="12.384331925" lat="50.310372273">
|
192 |
* <ele>560.329834</ele>
|
193 |
* <name>001</name>
|
194 |
* <desc>13-MRZ-11 12:47:52</desc>
|
195 |
* </wpt>
|
196 |
*/
|
197 |
|
198 |
Node node;
|
199 |
NavitWptList navitWptList = null;
|
200 |
|
201 |
// Liste der Wegpunkte lesen
|
202 |
NodeList wptList = gpxDoc.getElementsByTagName("wpt");
|
203 |
|
204 |
// Gibt es Wegpunkte?
|
205 |
if (wptList.getLength() > 0)
|
206 |
{
|
207 |
|
208 |
navitWptList = new NavitWptList();
|
209 |
|
210 |
for (int i = 0; i < wptList.getLength(); i++)
|
211 |
{
|
212 |
|
213 |
NavitWptRec navitWptRec = new NavitWptRec();
|
214 |
|
215 |
node = wptList.item(i);
|
216 |
|
217 |
if (node.getNodeType() == Node.ELEMENT_NODE)
|
218 |
{
|
219 |
|
220 |
Element element = (Element) node;
|
221 |
|
222 |
// Attribute des wpt-Tags lesen
|
223 |
navitWptRec.setLat(element.getAttribute("lat"));
|
224 |
navitWptRec.setLon(element.getAttribute("lon"));
|
225 |
|
226 |
// Name des Waypoints ermitteln - wenn vorhanden
|
227 |
String name_ = "";
|
228 |
NodeList nameElementList = element.getElementsByTagName("name");
|
229 |
Element nameElement = (Element) nameElementList.item(0);
|
230 |
if (nameElement != null)
|
231 |
{
|
232 |
NodeList name = nameElement.getChildNodes();
|
233 |
navitWptRec.setLabel(name.item(0).getNodeValue());
|
234 |
name_ = name.item(0).getNodeValue();
|
235 |
}
|
236 |
else
|
237 |
{
|
238 |
navitWptRec.setLabel("");
|
239 |
}
|
240 |
|
241 |
// Beschreibung des Waypoints holen - wenn vorhanden
|
242 |
NodeList descElementList = element.getElementsByTagName("desc");
|
243 |
Element descElement = (Element) descElementList.item(0);
|
244 |
if (descElement != null)
|
245 |
{
|
246 |
NodeList desc = descElement.getChildNodes();
|
247 |
navitWptRec.setDescription(desc.item(0).getNodeValue());
|
248 |
|
249 |
if (name_.equals(""))
|
250 |
{
|
251 |
// we dont have a name, so set "desc" as name
|
252 |
navitWptRec.setLabel(desc.item(0).getNodeValue());
|
253 |
}
|
254 |
}
|
255 |
else
|
256 |
{
|
257 |
navitWptRec.setDescription("");
|
258 |
}
|
259 |
}
|
260 |
|
261 |
// Den Datensatz des neuen Wegpunkts in die Wegpunktliste schreiben
|
262 |
navitWptList.add(navitWptRec);
|
263 |
}
|
264 |
}
|
265 |
return navitWptList;
|
266 |
}
|
267 |
|
268 |
public GPX_Track getRouteLabel()
|
269 |
{
|
270 |
NodeList nameList = gpxDoc.getElementsByTagName("rte");
|
271 |
ArrayList<NavitTrkptHeader> header = new ArrayList<NavitTrkptHeader>();
|
272 |
ArrayList<NavitTrkptList> navitTrkptRecList = new ArrayList<NavitTrkptList>();
|
273 |
GPX_Track obj = new GPX_Track();
|
274 |
|
275 |
if (nameList.getLength() > 0)
|
276 |
{
|
277 |
for (int ii = 0; ii < nameList.getLength(); ii++)
|
278 |
{
|
279 |
org.w3c.dom.Node nn = nameList.item(ii);
|
280 |
if (nn.getNodeType() == Node.ELEMENT_NODE)
|
281 |
{
|
282 |
try
|
283 |
{
|
284 |
Element firstElement = (Element) nn;
|
285 |
NodeList nameElementList = firstElement.getElementsByTagName("name");
|
286 |
Element nameElement = (Element) nameElementList.item(0);
|
287 |
NodeList name = nameElement.getChildNodes();
|
288 |
// System.out.println("ii:" + ii + ":" + name.item(0).getNodeValue());
|
289 |
NavitTrkptHeader h = new NavitTrkptHeader();
|
290 |
h.setLabel("\"" + MainFrame.remove_special_chars(name.item(0).getNodeValue()) + "\"");
|
291 |
header.add(h);
|
292 |
}
|
293 |
catch (Exception e)
|
294 |
{
|
295 |
|
296 |
}
|
297 |
}
|
298 |
|
299 |
//else
|
300 |
{
|
301 |
try
|
302 |
{
|
303 |
Element firstElement = (Element) nn;
|
304 |
NodeList nodeList = firstElement.getElementsByTagName("rtept");
|
305 |
if (nodeList.getLength() > 0)
|
306 |
{
|
307 |
NavitTrkptList tpl = new NavitTrkptList();
|
308 |
|
309 |
for (int jj = 0; jj < nodeList.getLength(); jj++)
|
310 |
{
|
311 |
NavitTrkptRec navitTrkptRec = new NavitTrkptRec();
|
312 |
Node node = nodeList.item(jj);
|
313 |
if (node.getNodeType() == Node.ELEMENT_NODE)
|
314 |
{
|
315 |
Element element = (Element) nodeList.item(jj);
|
316 |
// System.out.println("jj:" + jj + ":" + element.getAttribute("lat") + "," + element.getAttribute("lon"));
|
317 |
navitTrkptRec.setLat(element.getAttribute("lat"));
|
318 |
navitTrkptRec.setLon(element.getAttribute("lon"));
|
319 |
}
|
320 |
tpl.add(navitTrkptRec);
|
321 |
}
|
322 |
navitTrkptRecList.add(tpl);
|
323 |
}
|
324 |
}
|
325 |
catch (Exception e)
|
326 |
{
|
327 |
|
328 |
}
|
329 |
}
|
330 |
}
|
331 |
}
|
332 |
|
333 |
obj.h = header;
|
334 |
obj.i = navitTrkptRecList;
|
335 |
|
336 |
return obj;
|
337 |
}
|
338 |
|
339 |
/**************************************************************************
|
340 |
* calculateDistanc Berechnet die Gesamtlänge des Tracks auf der
|
341 |
* Grundlage der Kreisbogenmethode.
|
342 |
* Siehe: http://www.kompf.de/gps/distcalc.html
|
343 |
*
|
344 |
* @param trkptList
|
345 |
* Die Liste der Trackpunkte
|
346 |
* @return Gesamtlänge in Meter
|
347 |
*/
|
348 |
public double calculateDistance(NavitTrkptList trkptList)
|
349 |
{
|
350 |
|
351 |
double dist = 0;
|
352 |
double lat1;
|
353 |
double lat2 = 0;
|
354 |
double lon1;
|
355 |
double lon2 = 0;
|
356 |
|
357 |
for (int i = 0; i < trkptList.size() - 1; i++)
|
358 |
{
|
359 |
|
360 |
if (i == 0)
|
361 |
{
|
362 |
|
363 |
lat1 = Double.valueOf(trkptList.get(i).getLat());
|
364 |
lon1 = Double.valueOf(trkptList.get(i).getLon());
|
365 |
}
|
366 |
else
|
367 |
{
|
368 |
lat1 = lat2;
|
369 |
lon1 = lon2;
|
370 |
}
|
371 |
|
372 |
lat2 = Double.valueOf(trkptList.get(i + 1).getLat());
|
373 |
lon2 = Double.valueOf(trkptList.get(i + 1).getLon());
|
374 |
|
375 |
if (i == 930)
|
376 |
{
|
377 |
i = 930;
|
378 |
}
|
379 |
dist = dist + DistanceUtil.distance(lat1, lon1, lat2, lon2);
|
380 |
}
|
381 |
|
382 |
return dist * 1000; //Entfernung von km in m umrechnen
|
383 |
}
|
384 |
|
385 |
public void writeWptList(NavitWptList wptList, BufferedWriter outFile)
|
386 |
{
|
387 |
|
388 |
String s;
|
389 |
|
390 |
try
|
391 |
{
|
392 |
|
393 |
DecimalFormat trkptFormat = new DecimalFormat("#.000000");
|
394 |
for (int i = 0; i < wptList.size(); i++)
|
395 |
{
|
396 |
outFile.write("type=" + wptList.get(i).getType() + " " + "label=" + "\"" + MainFrame.remove_special_chars(wptList.get(i).getLabel()) + "\" " + "description=" + "\"" + MainFrame.remove_special_chars(wptList.get(i).getDescription()) + "\" " + "gc_type=" + "\"" + MainFrame.remove_special_chars(wptList.get(i).getGc_type()) + "\"\n");
|
397 |
|
398 |
s = trkptFormat.format(new Double(wptList.get(i).getLon())) + " ";
|
399 |
s = s.replace(",", ".");
|
400 |
outFile.write(s);
|
401 |
s = trkptFormat.format(new Double(wptList.get(i).getLat())) + " " + "\n";
|
402 |
s = s.replace(",", ".");
|
403 |
outFile.write(s);
|
404 |
}
|
405 |
|
406 |
}
|
407 |
catch (IOException e)
|
408 |
{
|
409 |
e.printStackTrace();
|
410 |
}
|
411 |
|
412 |
}
|
413 |
|
414 |
public void writeTrkptList(NavitTrkptHeader trkptHeader, NavitTrkptList trkptList, BufferedWriter outFile)
|
415 |
{
|
416 |
|
417 |
String s;
|
418 |
|
419 |
try
|
420 |
{
|
421 |
String name_ = MainFrame.remove_special_chars(trkptHeader.getLabel());
|
422 |
if (name_.equals(""))
|
423 |
{
|
424 |
name_ = MainFrame.remove_special_chars(trkptHeader.getDesc());
|
425 |
}
|
426 |
|
427 |
outFile.write("type=" + trkptHeader.getNavitType() + " " + "label=" + name_ + " " + "desc=" + MainFrame.remove_special_chars(trkptHeader.getDesc()) + " " + "type=" + MainFrame.remove_special_chars(trkptHeader.getType()) + " " + "length=" + trkptHeader.getLength() + " " + "count=" + trkptHeader.getCount() + "\n");
|
428 |
|
429 |
DecimalFormat trkptFormat = new DecimalFormat("#.000000");
|
430 |
for (int i = 0; i < trkptList.size(); i++)
|
431 |
{
|
432 |
s = trkptFormat.format(new Double(trkptList.get(i).getLon())) + " ";
|
433 |
s = s.replace(",", ".");
|
434 |
outFile.write(s);
|
435 |
s = trkptFormat.format(new Double(trkptList.get(i).getLat())) + "\n";
|
436 |
s = s.replace(",", ".");
|
437 |
outFile.write(s);
|
438 |
}
|
439 |
|
440 |
}
|
441 |
catch (IOException e)
|
442 |
{
|
443 |
e.printStackTrace();
|
444 |
}
|
445 |
}
|
446 |
|
447 |
}
|