This article is linked to the GPX parsing library that I published - fork it here on Github.
For a recent Android, project I needed to parse GPX files. ‘GPX’ is an acronym for GPS Exchange Format and it is an XML format to describe routes, tracks and waypoints, together with information about altitude, timing and so forth. This is an example:
<?xml version="1.0" encoding="UTF-8" standalone="no" ?>
<gpx xmlns="http://www.topografix.com/GPX/1/1" xmlns:gpxx="http://www.garmin.com/xmlschemas/GpxExtensions/v3" xmlns:gpxtpx="http://www.garmin.com/xmlschemas/TrackPointExtension/v1" creator="Oregon 400t" version="1.1" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.topografix.com/GPX/1/1 http://www.topografix.com/GPX/1/1/gpx.xsd http://www.garmin.com/xmlschemas/GpxExtensions/v3 http://www.garmin.com/xmlschemas/GpxExtensionsv3.xsd http://www.garmin.com/xmlschemas/TrackPointExtension/v1 http://www.garmin.com/xmlschemas/TrackPointExtensionv1.xsd">
<metadata>
<link href="http://www.garmin.com">
<text>Garmin International</text>
</link>
<time>2009-10-17T22:58:43Z</time>
</metadata>
<trk>
<name>Example GPX Document</name>
<trkseg>
<trkpt lat="47.644548" lon="-122.326897">
<ele>4.46</ele>
<time>2009-10-17T18:37:26Z</time>
</trkpt>
</trkseg>
</trk>
</gpx>
When faced with the need to parse such files in Android, and map them to convenient structures, I found no existing library to do it. Therefore I set out to make one myself. The first step was to map all these GPX object in a convenient way, so for instance:
public class TrackPoint {
private final Double mLatitude;
private final Double mLongitude;
private final Double mElevation;
private final DateTime mTime;
...
}
What took me the most time was the actual XML parsing. My last encounter with XML was a while ago, so I had to wrap my head around it again. I decided to use the default XmlPullParser shipped with the Android SDK. You start by defining the XML tags in Java:
static final String TAG_GPX = "gpx";
static final String TAG_TRACK = "trk";
static final String TAG_SEGMENT = "trkseg";
static final String TAG_POINT = "trkpt";
static final String TAG_LAT = "lat";
static final String TAG_LON = "lon";
...
And then it becomes a matter of recursively scanning the GPX file, “nesting” one level deeper when you encounter specific tags, and parsing each tag attribute. This is for instance how you can parse a TrackPoint:
// Processes summary tags in the feed.
TrackPoint readPoint(XmlPullParser parser) throws IOException, XmlPullParserException {
parser.require(XmlPullParser.START_TAG, ns, TAG_POINT);
Double lat = Double.valueOf(parser.getAttributeValue(null, TAG_LAT));
Double lng = Double.valueOf(parser.getAttributeValue(null, TAG_LON));
Double ele = null;
DateTime time = null;
while (parser.next() != XmlPullParser.END_TAG) {
if (parser.getEventType() != XmlPullParser.START_TAG) {
continue;
}
String name = parser.getName();
switch (name) {
case TAG_ELEVATION:
ele = readElevation(parser);
break;
case TAG_TIME:
time = readTime(parser);
break;
default:
skip(parser);
break;
}
}
parser.require(XmlPullParser.END_TAG, ns, TAG_POINT);
return new TrackPoint.Builder()
.setElevation(ele)
.setLatitude(lat)
.setLongitude(lng)
.setTime(time)
.build();
}
The library I published is largely incomplete - it only parses the elements I needed to parse - but it can be expanded to be more complete. All contributions to expand this library are most welcome, so fork it at will!
All Tags |
The Netherlands |
61
|
amsterdam |
35
|
Bicycle |
19
|
Chile |
18
|
Valparaiso |
15
|
Australia |
13
|
Art |
12
|
nepal |
8
|
scala |
8
|
akka |
6
|
Santiago |
5
|
France |
4
|
Gouda |
4
|
Paris |
4
|
akka-stream |
3
|
akka-streams |
3
|
dashain |
3
|
everest trek |
3
|
india |
3
|
Italy |
3
|
Melbourne |
3
|
Perth |
3
|
Road trip |
3
|
Rotterdam |
3
|
akka-http |
2
|
Argentina |
2
|
code |
2
|
custom_image |
2
|
custom_summary |
2
|
Delft |
2
|
event-sourcing |
2
|
Geraldton |
2
|
Haarlem |
2
|
leaf_bundle |
2
|
Lille |
2
|
Milan |
2
|
New Delhi |
2
|
New York |
2
|
Punta Arenas |
2
|
Rome |
2
|
Ushuaia |
2
|
Websocket |
2
|
Abcoude |
1
|
akka-cluster |
1
|
amazon web services |
1
|
android |
1
|
aws |
1
|
Berlin |
1
|
Bloemendaal |
1
|
Brisbane |
1
|
chitwan |
1
|
Circus Maximum |
1
|
covid19 |
1
|
Enkhuizen |
1
|
Esperance |
1
|
Fraser Island |
1
|
gps |
1
|
gpx |
1
|
guitars |
1
|
iot |
1
|
Isla Negra |
1
|
japan |
1
|
java |
1
|
Kalgoorlie |
1
|
kathmandu |
1
|
Las Vegas |
1
|
LoPy |
1
|
lora |
1
|
Markem |
1
|
Matisse |
1
|
Mexico |
1
|
Middelburg |
1
|
misc |
1
|
Muiden |
1
|
planning |
1
|
play |
1
|
reactjs |
1
|
refluxjs |
1
|
Reims |
1
|
San Francisco |
1
|
Sodaq |
1
|
Sydney |
1
|
Texel |
1
|
Theo Van Doesburg |
1
|
tokyo |
1
|
Travel |
1
|
tulips |
1
|
USA |
1
|
webjars |
1
|
Weesp |
1
|