/OSJGL

Open-Sourced Java Geographical Library

Primary LanguageJavaMIT LicenseMIT

OSJGL

Open-Sourced Java Geographical Library. Useful for importing/exporting GPX Path files, as well as handling coordinates, calculating great-circle distance, and other utities related to geography.

OSJGL is licensed under the MIT License. If you would like to contribute to the project, please open a pull request. Please keep in mind that OSJGL distributions target Java 6, and any pull request must target Java 6, as well.

You may download a distributable JAR here. Requires Java 6 or higher.

What files can I import/export with it?

Currently, OSJGL supports the following file formats:

  • GPX Routes
  • GPX Tracks*

*exporting GPX Tracks will convert it to a GPX Route file.

How do I import a GPX file?

Here is an example on importing a GPX file:

import org.oslib.gis.gpx.*;
...

Gpx gpx = GpxParser.parse(new java.io.File("/path/to/file.gpx");

How do I get the coordinates inside the GPX object?

Here is an example of calculating the total distance of the GPX path:

import org.oslib.gis.gpx.*;
...

double km = 0; // The total distance in kilometers
GpxTrack track = gpx.get(0); // Grab the GPX Track

GpxWayPoint last = null; // The last iterated GpxWayPoint

for(GpxSegment segment : track) { // Iterate each segment in track
  for(GpxWayPoint point : segment) { // Iterate each point in segment
    if(last != null)
      km += last.distanceTo(point);
    last = point;
  }
}
System.out.println("Total distance in kilometers: "+km);

How do I get the meta data of the GPX object?

Here is an example of getting the Meta Data values from the GPX file.

String name   = gpx.getMetaData().getName();
String author = gpx.getMetaData().getAuthor();
String desc   = gpx.getMetaData().getDescription();
String copy   = gpx.getMetaData().getCopyright();
String link   = gpx.getMetaData().getLink();
String keys   = gpx.getMetaData().getKeyWords();

How do I set the meta data of the GPX object?

Here is an example of setting the name of the GPX file.

gpx.getMetaData().setName("Santa Monica Pier");

How do I export a GPX object?

Here is an example of exporting an already existing GPX file.

import org.oslib.gis.gpx.*;
...

new GpxWriter(gpx).write(new File("/path/to/file.gpx"));