GPX Parser for Java? [closed]
Asked Answered
E

5

9

Are there any Java libraries for parsing GPX files? I need to parse many GPX files into our own data structure (our own database).

Excitation answered 16/8, 2010 at 16:10 Comment(0)
E
7

After some research, there is really no Java API/Lib for parsing GPX files, but I found a nice approach for parsing it using JAXB

Using this Tutorial: http://www.oracle.com/technetwork/articles/javase/index-140168.html

Steps:
1. Download GPX 1.0 and 1.1 Schema file (xsd)
2. Generate Java File from it using Eclipse Plugin
3. Init JAXBContext with package name of generated GPX java files (mine was "topografix.gpx.schema10")
4. Parse GPX File

JAXBContext jc = JAXBContext.newInstance("topografix.gpx.schema10");
Unmarshaller unmarshaller = jc.createUnmarshaller();
Gpx root = (Gpx) unmarshaller.unmarshal(new File("sample.gpx"));
List<Trk> tracks = root.getTrk();
....
Excitation answered 19/8, 2010 at 8:55 Comment(2)
Eclipse plugin: broken link :( sourceforge.net/projects/jaxb-builder perhaps?Goodill
This is a good place to start off faster: youtube.com/watch?v=Ip0T91q70PAGenie
G
10

This question is too old and so do the answers. Thanks to the open source world, we have now jgpx, on google code (forked multiple times on github) and GPXParser, on sourceforge.net. There are also a lot of results for a search on Github.

I'm not sure which one is more mature (one of them is marked as Alpha) but you can try them and let us know here.

Edit

Have a look at processing-gpx, it seems promising.

Here is a quick example

import tomc.gpx.*;

// outside setup()
GPX gpx;

  // inside setup()
  gpx = new GPX(this);

  // when you want to load data
  gpx.parse("test.gpx"); // or a URL

  // inside draw()
  for (int i = 0; i < gpx.getTrackCount(); i++) {
    GPXTrack trk = gpx.getTrack(i);
    // do something with trk.name
    for (int j = 0; j < trk.size(); j++) {
      GPXTrackSeg trkseg = trk.getTrackSeg(j);
      for (int k = 0; k < trkseg.size(); k++) {
        GPXPoint pt = trkseg.getPoint(k);
        // do something with pt.lat or pt.lon
      }
    }
  }

  for (int i = 0; i < gpx.getWayPointCount(); i++) {
    GPXWayPoint wpt = gpx.getWayPoint(i);
    // do something with wpt.lat or wpt.lon or wpt.name or wpt.type
  }
Genie answered 17/1, 2013 at 9:24 Comment(1)
It is tied to the Android OS.Apulia
E
7

After some research, there is really no Java API/Lib for parsing GPX files, but I found a nice approach for parsing it using JAXB

Using this Tutorial: http://www.oracle.com/technetwork/articles/javase/index-140168.html

Steps:
1. Download GPX 1.0 and 1.1 Schema file (xsd)
2. Generate Java File from it using Eclipse Plugin
3. Init JAXBContext with package name of generated GPX java files (mine was "topografix.gpx.schema10")
4. Parse GPX File

JAXBContext jc = JAXBContext.newInstance("topografix.gpx.schema10");
Unmarshaller unmarshaller = jc.createUnmarshaller();
Gpx root = (Gpx) unmarshaller.unmarshal(new File("sample.gpx"));
List<Trk> tracks = root.getTrk();
....
Excitation answered 19/8, 2010 at 8:55 Comment(2)
Eclipse plugin: broken link :( sourceforge.net/projects/jaxb-builder perhaps?Goodill
This is a good place to start off faster: youtube.com/watch?v=Ip0T91q70PAGenie
S
3

Very nice. However, I needed to do:

    GpxType gpx = null;
    try {
        JAXBContext jc = JAXBContext.newInstance(PACKAGE_NAME);
        Unmarshaller unmarshaller = jc.createUnmarshaller();
        JAXBElement<GpxType> root = (JAXBElement<GpxType>)unmarshaller
            .unmarshal(new File(TEST_FILE));
        gpx = root.getValue();
    } catch(JAXBException ex) {
       // TODO
    }

    List<TrkType> tracks = gpx.getTrk();
    for(TrkType track : tracks) {
        System.out.println(track.getName());
    }

BTW I used http://www.topografix.com/GPX/1/1.

-Ken

Staid answered 19/8, 2010 at 20:13 Comment(1)
yes I know, for simplicity I left out try-catch block and for Production code you have to support both 1.0 and 1.1 (so your and mine code combined). What I did was first try to unmarshall using schema10, if it throws an exception, i tried using schema11 (for 1.1). If that fails again that file is not a supported GPX file.Excitation
I
3

Ready to use, open source, and fully functional java GpxParser (and much more) here https://sourceforge.net/projects/geokarambola/

Details here https://plus.google.com/u/0/communities/110606810455751902142

With the above library parsing a GPX file is a one liner:

Gpx gpx = GpxFileIo.parseIn( "SomeGeoCollection.gpx" ) ;

Getting its points, routes or tracks trivial too:

ArrayList<Route> routes = gpx.getRoutes( ) ;
Imputation answered 27/3, 2016 at 21:51 Comment(1)
I'll give it a try. Would be nice to have it on github and/or as maven repositoryCompo
R
0

I'm not aware of any library specialized in parsing GPX files, but since GPX is XML you can use your preferred Java XML parser for reading it.
The GPX format is documented here (includes XSD schema): http://www.topografix.com/gpx.asp

Rayraya answered 16/8, 2010 at 16:21 Comment(1)
since GPX is very wide spread, I thought there should be some lib/api for that. Because I didn't want to start from scratch especially if someone should have already done that.Excitation

© 2022 - 2024 — McMap. All rights reserved.