有没有适用于解析GPX文件的Java库?我需要将多个GPX文件解析为我们自己的数据结构(即我们自己的数据库)。
这个问题太老了,答案也是。感谢开源世界,我们现在有了jgpx,在google code上(在Github上分叉多次)和GPXParser,在sourceforge.net上。还有许多Github搜索结果。
我不确定哪一个更成熟(它们中的其中一个标记为Alpha),但您可以尝试它们并在此处告诉我们。
看一下processing-gpx,它看起来很有前途。
这里是一个快速示例:
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
}
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();
....
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());
}
顺便提一下,我使用了http://www.topografix.com/GPX/1/1。
-Ken
这里有一个可供使用的、开源的、完全功能的Java GpxParser(以及更多):https://sourceforge.net/projects/geokarambola/
详情请查看:https://plus.google.com/u/0/communities/110606810455751902142
通过使用上述库,解析GPX文件只需要一行代码:
Gpx gpx = GpxFileIo.parseIn( "SomeGeoCollection.gpx" ) ;
ArrayList<Route> routes = gpx.getRoutes( ) ;
我不知道有没有专门用于解析GPX文件的库,但是由于GPX是XML格式,您可以使用您喜欢的Java XML解析器来读取它。
GPX格式在这里有文档(包括XSD模式):
http://www.topografix.com/gpx.asp