适用于 Java 的 GPX 解析器?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3495052/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-30 02:05:58  来源:igfitidea点击:

GPX Parser for Java?

javaparsingapigpx

提问by Buju

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

是否有任何用于解析 GPX 文件的 Java 库?我需要将许多 GPX 文件解析为我们自己的数据结构(我们自己的数据库)。

采纳答案by Buju

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

经过一番研究,确实没有用于解析 GPX 文件的 Java API/Lib,但我发现了一种使用JAXB解析它的好方法

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

使用本教程: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

步骤:
1. 下载 GPX 1.0 和 1.1 Schema 文件 (xsd)
2. 使用Eclipse 插件从中生成 Java 文件
3.使用生成的 GPX java 文件的包名初始化 JAXBContext(我的是“topografix.gpx.schema10”)
4. 解析GPX文件

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();
....

回答by Mohamed Taher Alrefaie

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.

这个问题太老了,答案也是如此。感谢开源世界,我们现在在 google 代码上jgpx在 github 上多次分叉)和GPXParser,在 sourceforge.net 上在 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.

我不确定哪个更成熟(其中一个被标记为 Alpha),但您可以尝试并在此处告诉我们。

Edit

编辑

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

看看processing-gpx,它看起来很有希望。

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
  }

回答by Ken

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.

顺便说一句,我使用了http://www.topografix.com/GPX/1/1

-Ken

-肯

回答by Afonso Santos

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

随时可用、开源且功能齐全的 java GpxParser(以及更多)在这里 https://sourceforge.net/projects/geokarambola/

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

详情在这里 https://plus.google.com/u/0/communities/110606810455751902142

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

使用上述库解析 GPX 文件是一个单行:

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

Getting its points, routes or tracks trivial too:

获取它的点、路线或轨迹也很简单:

ArrayList<Route> routes = gpx.getRoutes( ) ;

回答by Volker Voecking

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

我不知道有任何专门用于解析 GPX 文件的库,但是由于 GPX 是 XML,您可以使用首选的 Java XML 解析器来读取它。
GPX 格式记录在此处(包括 XSD 架构):http: //www.topografix.com/gpx.asp