A weirdly named site with lots of boring content!

Mon 26 Jan 2009

XML Parsing and Display of GPX data in Java

With all these GPX recordings of routes I'm collecting, I wanted to view and compare details of past routes in a more user-friendly manner than poring through reams of XML output. The Google Maps API would be great to incorporate somehow, and it is freely available for JavaScript use. However I decided to be awkward and write a desktop application in Java. I'm figuring that I'll find some way around the problem of getting graphical map imagery later.

So far I have a small Java Swing/AWT application which reads an XML file of GPS points from a recording of a cycle ride and displays the route in a JPanel within a JFrame:

There are three primary classes - quickChart which extends JFrame, chartPanel which extends JPanel, and gpxParser which extends org.xml.sax.helpers.DefaultHandler.

When the JFrame fires up, it sets the JPanel to visible. The JFrame also has a menu bar, with only two options at present: Open and Exit. Exit is obvious, and Open calls the gpxParser class to read a XML document via SAX. There are 600 to 4000 points in a file, and as it walks through the XML tree gpxParser reads each one into an ArrayList. This array list is then passed over to the JPanel, which iterates through them when rendering the route, and also when checking for 'mouseovers' (the point under the mouse is selected, as shown in orange on the route graphic).


6:17 pm, Sunday, 15 February 09

this sounds great, can you put an example code for me? thx!


10:28 pm, Tuesday, 17 February 09

Here is the gpxParser class which parses the source XML using an event-driven SAX model.

To implement a SAX parser like this, you create a class which extends org.xml.sax.helpers.DefaultHandler, and in this class you override functions of DefaultHandler in order to handle various events which are fired as the XML document is read element-by-element:

import java.io.*;<br/>import java.text.ParseException;<br/>import java.util.ArrayList;<br/><br/>import org.xml.sax.*;<br/><br/>public class gpxParser extends org.xml.sax.helpers.DefaultHandler {<br/> private stringStack elementNames;<br/> private float minLat, maxLat, minLon, maxLon;<br/> private int totalPoints;<br/> private long totalSeconds;<br/> private ArrayList&lt;journeyPoint> plots = new ArrayList&lt;journeyPoint>(50);<br/> private StringBuilder contentBuffer;<br/> private double currentDistance;<br/> <br/> public gpxParser() {<br/> clear();<br/> }<br/> <br/> public void clear() {<br/> totalPoints = 0;<br/> currentDistance = 0;<br/> totalSeconds = 0;<br/> elementNames = new stringStack();<br/> plots.clear();<br/> contentBuffer = new StringBuilder();<br/> }<br/> <br/> /*<br/> * READ GPX DATA FILE<br/> */<br/> public int read(String filename) {<br/> clear();<br/><br/> try {<br/> FileInputStream in = new FileInputStream(new File(filename));<br/> InputSource source = new InputSource(in);<br/> <br/> XMLReader parser = org.xml.sax.helpers.XMLReaderFactory.createXMLReader("org.apache.xerces.parsers.SAXParser");<br/> parser.setContentHandler(this);<br/> parser.parse(source);<br/> in.close();<br/> } catch (FileNotFoundException e) {<br/> // TODO Auto-generated catch block<br/> e.printStackTrace();<br/> } catch (UnsupportedEncodingException e) {<br/> // TODO Auto-generated catch block<br/> e.printStackTrace();<br/> } catch (SAXException e) {<br/> // TODO Auto-generated catch block<br/> e.printStackTrace();<br/> } catch (IOException e) {<br/> // TODO Auto-generated catch block<br/> e.printStackTrace();<br/> }<br/> return 0;<br/> }<br/><br/> public void load(ArrayList&lt;journeyPoint> arrayList, journeyPoint topLeft, journeyPoint bottomRight) {<br/> clear();<br/> plots.addAll(arrayList);<br/> totalPoints = plots.size();<br/> }<br/> <br/> /*<br/> * DefaultHandler::startElement() fires whenever an XML start tag is encountered <br/> * @see org.xml.sax.helpers.DefaultHandler#startElement(java.lang.String, java.lang.String, java.lang.String, org.xml.sax.Attributes)<br/> */<br/> public void startElement(String uri, String localName, String qName, Attributes attributes) throws SAXException {<br/> <br/> // the &lt;bounds> element has attributes which specify min & max latitude and longitude <br/> if (localName.compareToIgnoreCase("bounds") == 0) {<br/> <br/> minLat = new Float(attributes.getValue("minlat")).floatValue();<br/> maxLat = new Float(attributes.getValue("maxlat")).floatValue();<br/> minLon = new Float(attributes.getValue("minlon")).floatValue();<br/> maxLon = new Float(attributes.getValue("maxlon")).floatValue();<br/> <br/> } else {<br/> <br/> // the &lt;trkpt> element has attributes which specify latitude and longitude (it has child elements that specify the time and elevation) <br/> if (localName.compareToIgnoreCase("trkpt") == 0) {<br/> totalPoints++;<br/> plots.add(new journeyPoint(Double.parseDouble(attributes.getValue("lon")), Double.parseDouble(attributes.getValue("lat"))));<br/> }<br/> }<br/> <br/> // Clear content buffer<br/> contentBuffer.delete(0, contentBuffer.length());<br/> <br/> // Store name of current element in stack<br/> elementNames.push(qName);<br/> }<br/> <br/> /* <br/> * the DefaultHandler::characters() function fires 1 or more times for each text node encountered<br/> * <br/> */<br/> public void characters(char[] ch, int start, int length) throws SAXException {<br/> contentBuffer.append(String.copyValueOf(ch, start, length));<br/> }<br/> <br/> /* <br/> * the DefaultHandler::endElement() function fires for each end tag<br/> * <br/> */<br/> public void endElement(String uri, String localName, String qName) throws SAXException {<br/> String currentElement = elementNames.pop();<br/> <br/> if (totalPoints > 0 && currentElement != null) {<br/> if (currentElement.compareToIgnoreCase("ele") == 0) {<br/> plots.get(totalPoints-1).setElevation(Float.parseFloat(contentBuffer.toString()));<br/> } else {<br/> if (currentElement.compareToIgnoreCase("time") == 0) {<br/> try {<br/> plots.get(totalPoints-1).setTime(contentBuffer.toString());<br/> } catch (ParseException e) {<br/> System.out.println("Bad date: " + contentBuffer.toString());<br/> }<br/> } else if (currentElement.compareToIgnoreCase("trkpt") == 0) {<br/> if (totalPoints > 1) {<br/> currentDistance += plots.get(totalPoints-1).metresTo(plots.get(totalPoints-2));<br/> plots.get(totalPoints-1).setDistance(currentDistance);<br/> totalSeconds += plots.get(totalPoints-1).secondsTo(plots.get(totalPoints-2));<br/> plots.get(totalPoints-1).setDuration(totalSeconds);<br/> }<br/> }<br/> }<br/> }<br/> }<br/> <br/> public int getTotalPoints() {<br/> return totalPoints;<br/> }<br/><br/> public ArrayList&lt;journeyPoint> getPlots() {<br/> return plots;<br/> }<br/><br/> public journeyPoint getTopLeft() {<br/> return new journeyPoint(minLon, minLat);<br/> }<br/><br/> public journeyPoint getBottomRight() {<br/> return new journeyPoint(maxLon, maxLat);<br/> }<br/>}

The SAX model is effectively a stateless XML parser. The XML document is read one time, and is not stored in memory. If we want to know where we are in the document tree, we need to record our position ourselves. This is what makes it so lightweight and fast, however it also means it isn't always intuitive to work with. Take a look at the GPS files which we're parsing here. They record track data as a series of <trkpt> elements:

&lt;trkpt lat="52.446738677" lon="-1.880564885"><br/> &lt;ele>155.390259&lt;/ele><br/> &lt;time>2008-12-15T15:56:02Z&lt;/time><br/>&lt;/trkpt>

Each of these elements records a GPS position using attributes specifying the latitude and longitude, and two child elements, <ele> and <time>, which each contain a string (in XML parlance a "child text node") representing the data value recorded at that point. The <ele> element records the elevation in metres, and the other is obvious.

So in order for us to record all the data for a point, we first need to read the attributes of the <trkpt> element, and then the text data of its two child elements. Each of these requires a different approach in our Java code. Reading attributes is straightforward - we can do it in the startElement() function which SAX fires each time the start of an XML element is encountered.

Reading the two text nodes is not so simple. Text nodes can be handled by overriding the characters() method, however a single text node may call this multiple times, once for each chunk of text. This means that we won't know that we have read all the text until we hit the end of the XML element - which we deal with in the endElement() method. So our characters() method just has to concatenate all the chunks of text, and the endElement() method will then process that text.

I use a simple stack to record the element we're currently reading, and the points of the GPX route are saved as an ArrayList of journeyPoint instances.

This ArrayList forms the internal model that our Java application uses to represent a recorded route. Once the gpsParser class has built it, it is passed to the chartPanel class for display.


8:02 pm, Wednesday, 23 December 09

Hi, I wonder if you could show me the code of the journeyPoint class...that would be very useful...Thanxs a lot!


2:35 pm, Monday, 28 December 09

Hi Juan, I've uploaded all the code here. Give me a shout if you have any problems getting it to run OK.


6:19 pm, Tuesday, 6 April 10

Hi, I want to use your gpx parser (probably modified) in a personal project. Can I do that? What license or conditions will apply?


1:01 am, Thursday, 8 April 10

Hi Quique, please feel free to make use of the code here. I haven't done any work on it for a while, so I'm glad to hear it could be useful to someone.

No restrictions, but I'd be interested to hear how it goes!


4:58 pm, Wednesday, 21 September 11

To read GPX files easily in Java see: http://sourceforge.net/p/gpsanalysis/home/Home
Leave Comment

/xkcd/ Dying Gift