Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

GPX Parser for Java? [closed]

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

like image 519
Buju Avatar asked Aug 16 '10 16:08

Buju


2 Answers

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
  }
like image 106
Mohamed Taher Alrefaie Avatar answered Sep 28 '22 08:09

Mohamed Taher Alrefaie


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();
....
like image 38
Buju Avatar answered Sep 28 '22 08:09

Buju