Android GPX Library

This is a small Android library that I created for a work project, it can be used to easily parse and create GPX files.

Have a look at the sample project using this library on my GitHub

The library contains a SAX parser for the GPX format, as described in the GPX Schema documentation at topografix.com .

The Gpx parser is asynchronous, this means that an event listener interface must be provided when the parser is started. The parsed GPX document or any parsing error that may occur will be returned through the event listener interface.

Basic Usage

This is the basic code required to parse a GPX file, assuming that we have a “file.gpx” in the assets folder:

    try {
      
      InputStream input = getAssets().open("file.gpx");
      
      // The GpxParser automatically closes the InputStream so we do not have to bother about it
      new GpxParser(input, this, this).parse();
      
    } catch (IOException e) {
      // Error opening/reading file
    }

The GpxParser constructor takes three parameters:

  • The InputStream for input data
  • A GpxParserListener implementation (see below)
  • A GpxParserProgressListener implementation, can be null (see below)

Once the GpxParser has been constructed just call the parse() method to start it.

Parser listeners

Two different event listeners can be provided to the GpxParser constructor:

public GpxParser(InputStream gpxIs, 
                 GpxParserListener listener, 
                 GpxParserProgressListener progressListener)

The GpxParserListener is mandatory, an IllegalArgumentException will be thrown if this argument is null.
GpxParserListener provides basic feedback on the parser activity, the methods are pretty self explanatory:

public static interface GpxParserListener {
		
	public void onGpxParseStarted();
	public void onGpxParseCompleted(GPXDocument document);
	public void onGpxParseError(String type, 
                                    String message, 
                                    int lineNumber, 
                                    int columnNumber);
		
}

The GpxParserProgressListener on the other hand is only required if you need a fine degree of feedback on what the parser is currently doing in the background.
This listener should be used only if necessary as it requires implementing a lot of methods.

public static interface GpxParserProgressListener {

  public void onGpxNewTrackParsed(int count, GPXTrack track);
  public void onGpxNewRouteParsed(int count, GPXRoute track);
  public void onGpxNewSegmentParsed(int count, GPXSegment segment);
  public void onGpxNewTrackPointParsed(int count, GPXTrackPoint trackPoint);
  public void onGpxNewRoutePointParsed(int count, GPXRoutePoint routePoint);
  public void onGpxNewWayPointParsed(int count, GPXWayPoint wayPoint);
		
}

As you can see the *GpxParserProgressListener* can be used to get a callback on every GPX entity that is being parsed.

Internal GPX representation

As you may have noticed this library provides a set of objects to represent different GPX entities

  • GPXDocument Top level container, represents the GPX file
  • GPXTrack GPX trk entity
  • GPXRoute GPX rte entity
  • GPXSegment GPX trkseg entity
  • GPXTrackPoint GPX trkpt entity
  • GPXRoutePoint GPX rtept entity
  • GPXWayPoint GPX wpt entity

Please read the code for more details on how these classes work, or leave a comment here.

Advertisements

2 thoughts on “Android GPX Library

Add yours

  1. Thanks man. I got this task at work and your library saved me a lot of time! I’d buy you a beer if I could. Cheers!

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Create a free website or blog at WordPress.com.

Up ↑

%d bloggers like this: