Java 的 GPX 解析器?

发布于 2024-09-14 17:33:42 字数 1539 浏览 7 评论 0原文

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

鲸落 2024-09-21 17:33:42

这个问题太老了,答案也太老了。
感谢开源世界,我们现在在 google 代码上有了 jgpx (在 github 上分叉多次)和 GPXParser,位于 sourceforge.netGithub 上的搜索

我不确定哪一个更成熟(其中一个被标记为 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
  }

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
  }
樱娆 2024-09-21 17:33:42

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

使用本教程:http://www.oracle.com /technetwork/articles/javase/index-140168.html

步骤:
1.下载GPX 1.0和1.1架构文件(xsd)
2. 使用 Eclipse 插件< br>
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();
....

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();
....
亚希 2024-09-21 17:33:42

很不错。但是,我需要这样做:

    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

-肯

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.

-Ken

薄荷→糖丶微凉 2024-09-21 17:33:42

即用型、开源且功能齐全的 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( ) ;

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

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

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

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

Getting its points, routes or tracks trivial too:

ArrayList<Route> routes = gpx.getRoutes( ) ;
甲如呢乙后呢 2024-09-21 17:33:42

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

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文