source: josm/src/org/openstreetmap/josm/io/RawGpsReader.java@ 103

Last change on this file since 103 was 103, checked in by imi, 18 years ago
  • fixed unicode compatibility
  • added Layer menu which mirrors the layer list context menu
  • added "convert to data layer" for all gps layers
  • removed raw gpx import and 0.2 osm-file import
  • removed dependency to JDOM
File size: 2.6 KB
Line 
1package org.openstreetmap.josm.io;
2
3import java.io.IOException;
4import java.io.InputStream;
5import java.io.InputStreamReader;
6import java.util.Collection;
7import java.util.LinkedList;
8
9import org.openstreetmap.josm.data.coor.LatLon;
10import org.openstreetmap.josm.gui.layer.RawGpsLayer.GpsPoint;
11import org.xml.sax.Attributes;
12import org.xml.sax.SAXException;
13
14import uk.co.wilson.xml.MinML2;
15
16/**
17 * Read raw gps data from a gpx file. Only way points with their ways segments
18 * and waypoints are imported.
19 * @author imi
20 */
21public class RawGpsReader {
22
23 private static class Parser extends MinML2 {
24 /**
25 * Current track to be read. The last entry is the current trkpt.
26 * If in wpt-mode, it contain only one GpsPoint.
27 */
28 private Collection<GpsPoint> current = new LinkedList<GpsPoint>();
29 public Collection<Collection<GpsPoint>> data = new LinkedList<Collection<GpsPoint>>();
30 private LatLon currentLatLon;
31 private String currentTime;
32
33 @Override public void startElement(String namespaceURI, String localName, String qName, Attributes atts) throws SAXException {
34 if (qName.equals("wpt") || qName.equals("trkpt")) {
35 try {
36 double lat = Double.parseDouble(atts.getValue("lat"));
37 double lon = Double.parseDouble(atts.getValue("lon"));
38 if (Math.abs(lat) > 90)
39 throw new SAXException("Data error: lat value '"+lat+"' is out of bound.");
40 if (Math.abs(lon) > 180)
41 throw new SAXException("Data error: lon value '"+lon+"' is out of bound.");
42 currentLatLon = new LatLon(lat, lon);
43 } catch (NumberFormatException e) {
44 e.printStackTrace();
45 throw new SAXException(e);
46 }
47 } else if (qName.equals("time")) {
48 currentTime = "";
49 }
50 }
51
52 @Override public void characters(char[] ch, int start, int length) {
53 if (currentTime != null && currentTime.equals(""))
54 currentTime = new String(ch, start, length);
55 }
56
57 @Override public void endElement(String namespaceURI, String localName, String qName) {
58 if (qName.equals("wpt") || qName.equals("trkpt")) {
59 current.add(new GpsPoint(currentLatLon, currentTime));
60 } else if (qName.equals("trkseg") || qName.equals("trk") || qName.equals("gpx"))
61 newTrack();
62 }
63
64 private void newTrack() {
65 if (!current.isEmpty()) {
66 data.add(current);
67 current = new LinkedList<GpsPoint>();
68 }
69 }
70 }
71
72 /**
73 * Parse and return the read data
74 */
75 public static Collection<Collection<GpsPoint>> parse(InputStream source) throws SAXException, IOException {
76 Parser parser = new Parser();
77 parser.parse(new InputStreamReader(source, "UTF-8"));
78 return parser.data;
79 }
80}
Note: See TracBrowser for help on using the repository browser.