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