source: osm/applications/editors/josm/plugins/waypoints/src/WaypointReader.java@ 11239

Last change on this file since 11239 was 9949, checked in by stoecker, 17 years ago

fixed build of two more modules

File size: 3.3 KB
Line 
1package waypoints;
2
3import static org.openstreetmap.josm.tools.I18n.tr;
4
5import java.io.IOException;
6import java.io.InputStream;
7import java.io.InputStreamReader;
8
9import javax.xml.parsers.SAXParserFactory;
10import javax.xml.parsers.ParserConfigurationException;
11
12import org.openstreetmap.josm.data.coor.LatLon;
13import org.openstreetmap.josm.data.osm.DataSet; // NW
14import org.openstreetmap.josm.data.osm.Node; // NW
15import org.xml.sax.Attributes;
16import org.xml.sax.SAXException;
17import org.xml.sax.InputSource;
18import org.xml.sax.helpers.DefaultHandler;
19
20/**
21 * Read waypoints from a GPX file and convert to nodes.
22 */
23public class WaypointReader {
24
25 private static class Parser extends DefaultHandler {
26 /**
27 * Current track to be read. The last entry is the current trkpt.
28 * If in wpt-mode, it contain only one GpsPoint.
29 */
30 private DataSet dataSet;
31 private LatLon currentLatLon;
32 private String curWptName;
33 private boolean inName = false;
34
35 // NW start
36 // data now has two components: the GPS points and an OsmDataLayer.
37 // This is to allow us to convert waypoints straight to nodes.
38 // The way this works is that waypoints with a name not beginning
39 // with 0 - i.e. waypoints specially named - will be loaded in as
40 // nodes, in addition to going into the gpx layer. Other waypoints will
41 // only go into the gpx layer.
42 public Parser()
43 {
44 dataSet = new DataSet();
45 }
46 // NW end
47
48 @Override public void startElement(String namespaceURI,
49 String localName, String qName, Attributes atts)
50 throws SAXException {
51 if (qName.equals("wpt")) {
52 try {
53 double lat = Double.parseDouble(atts.getValue("lat"));
54 double lon = Double.parseDouble(atts.getValue("lon"));
55 if (Math.abs(lat) > 90)
56 throw new SAXException
57 (tr("Data error: lat value \"{0}\" is out of bound.",
58 lat));
59 if (Math.abs(lon) > 180)
60 throw new SAXException
61 (tr("Data error: lon value \"{0}\" is out of bound.",
62 lon));
63 currentLatLon = new LatLon(lat, lon);
64 } catch (NumberFormatException e) {
65 e.printStackTrace();
66 throw new SAXException(e);
67 }
68 }
69 else if (qName.equals("name")) {
70 inName = true;
71 curWptName = "";
72 }
73 }
74
75 @Override public void characters(char[] ch, int start, int length) {
76 // NW start
77 if (inName) {
78 curWptName = new String (ch,start,length);
79 }
80 // NW end
81 }
82
83 @Override public void endElement(String namespaceURI, String localName,
84 String qName) {
85 if (qName.equals("wpt") && curWptName!="") {
86 // create a new node from the latitude and longitude
87 System.out.println("Found a waypoint to convert to a node: "
88 + curWptName);
89 Node node = new Node(currentLatLon);
90 node.put("name",curWptName);
91 dataSet.nodes.add(node);
92 }
93 else if (qName.equals("name")) {
94 inName = false;
95 }
96 }
97 }
98
99 /**
100 * Parse and return the read data
101 */
102 public static DataSet parse(InputStream source)
103 throws SAXException, IOException, ParserConfigurationException {
104 Parser parser = new Parser();
105 SAXParserFactory.newInstance().newSAXParser().parse(new InputSource(new InputStreamReader(source, "UTF-8")), parser);
106 return parser.dataSet;
107 }
108}
Note: See TracBrowser for help on using the repository browser.