如何将多个 gpx 文件加载到 PostGIS 中?

发布于 2024-12-01 18:16:46 字数 1674 浏览 2 评论 0原文

我有一堆来自 GPSLogger for Android 应用程序的 gpx 文件。

文件看起来像:

<?xml version="1.0" encoding="UTF-8"?>
<gpx  version="1.0" creator="GPSLogger - http://gpslogger.mendhak.com/"  
  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
  xmlns="http://www.topografix.com/GPX/1/0" 
  xsi:schemaLocation="http://www.topografix.com/GPX/1/0 
    http://www.topografix.com/GPX/1/0/gpx.xsd" >
  <time>2011-08-26T06:25:20Z</time>
  <bounds></bounds>
  <trk>
    <trkseg>
      <trkpt  lat="46.94681501102746"  lon="7.398453755309032" >
        <ele>634.0</ele>
        <speed>0.0</speed>
        <src>gps</src>
        <sat>6</sat>
        <time>2011-08-26T06:25:20Z</time>
      </trkpt>
      <trkpt  lat="46.94758878281887"  lon="7.398622951942811" >
        <ele>748.0</ele>
        <speed>0.0</speed>
        <src>gps</src>
        <sat>5</sat>
        <time>2011-08-26T06:30:56Z</time>
      </trkpt>

...   ...   ...

    </trkseg>
  </trk>
</gpx>

是否可以使用 SQL 或 Python 遍历包含这些文件的目录并将它们加载到一个 PostGIS 表中?

我偶然发现这篇博客文章提到:

我不知道有什么可以直接从 GPX 转换为 PostGIS

这篇文章 给出了使用 SQL 来执行此操作的示例,但我无法理解代码:/

I have a bunch of gpx files that come from GPSLogger for Android app.

Files look like:

<?xml version="1.0" encoding="UTF-8"?>
<gpx  version="1.0" creator="GPSLogger - http://gpslogger.mendhak.com/"  
  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
  xmlns="http://www.topografix.com/GPX/1/0" 
  xsi:schemaLocation="http://www.topografix.com/GPX/1/0 
    http://www.topografix.com/GPX/1/0/gpx.xsd" >
  <time>2011-08-26T06:25:20Z</time>
  <bounds></bounds>
  <trk>
    <trkseg>
      <trkpt  lat="46.94681501102746"  lon="7.398453755309032" >
        <ele>634.0</ele>
        <speed>0.0</speed>
        <src>gps</src>
        <sat>6</sat>
        <time>2011-08-26T06:25:20Z</time>
      </trkpt>
      <trkpt  lat="46.94758878281887"  lon="7.398622951942811" >
        <ele>748.0</ele>
        <speed>0.0</speed>
        <src>gps</src>
        <sat>5</sat>
        <time>2011-08-26T06:30:56Z</time>
      </trkpt>

...   ...   ...

    </trkseg>
  </trk>
</gpx>

Is it possible to traverse a directory containing these files and load them into one PostGIS table using either SQL or Python?

I've stubled upon this blog post mentioning that:

I’m not aware of anything that can convert straight from GPX to
PostGIS

This post gives an example of working with SQL to do that but I can't get my head around the code :/

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

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

发布评论

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

评论(2

爱人如己 2024-12-08 18:16:46

ogr2ogr (GDAL 的一部分)是一个简单直接的 Unix shell 工具,用于将 GPX 文件加载到 PostGIS 中。

ogr2ogr -append -f PostgreSQL PG:dbname=walks walk.gpx

ogr2ogr 在 PostGIS 中使用自己的模式创建自己的数据库表。 tracks 表中每个 GPS 轨迹占一行; tracks.wkb_geometry 包含 GPS 轨迹本身作为 MultiLineString。 track_points 表包含各个位置修正(带有时间戳)。

这是导入之前数据库 walks 的样子:

walks=# \d
               List of relations
 Schema |       Name        | Type  |  Owner   
--------+-------------------+-------+----------
 public | geography_columns | view  | postgres
 public | geometry_columns  | view  | postgres
 public | raster_columns    | view  | postgres
 public | raster_overviews  | view  | postgres
 public | spatial_ref_sys   | table | postgres
(5 rows)

...和导入之后:

walks=# \d
                    List of relations
 Schema |           Name           |   Type   |  Owner   
--------+--------------------------+----------+----------
 public | geography_columns        | view     | postgres
 public | geometry_columns         | view     | postgres
 public | raster_columns           | view     | postgres
 public | raster_overviews         | view     | postgres
 public | route_points             | table    | postgres
 public | route_points_ogc_fid_seq | sequence | postgres
 public | routes                   | table    | postgres
 public | routes_ogc_fid_seq       | sequence | postgres
 public | spatial_ref_sys          | table    | postgres
 public | track_points             | table    | postgres
 public | track_points_ogc_fid_seq | sequence | postgres
 public | tracks                   | table    | postgres
 public | tracks_ogc_fid_seq       | sequence | postgres
 public | waypoints                | table    | postgres
 public | waypoints_ogc_fid_seq    | sequence | postgres
(15 rows)

ogr2ogr (part of GDAL) is a simple and straightforward Unix shell tool to load a GPX file into PostGIS.

ogr2ogr -append -f PostgreSQL PG:dbname=walks walk.gpx

ogr2ogr creates its own database tables in PostGIS with its own schema. The table tracks has one row per GPS track; tracks.wkb_geometry contains the GPS track itself as a MultiLineString. The table track_points contains individual location fixes (with timestamps).

Here is what the database walks looks like before the import:

walks=# \d
               List of relations
 Schema |       Name        | Type  |  Owner   
--------+-------------------+-------+----------
 public | geography_columns | view  | postgres
 public | geometry_columns  | view  | postgres
 public | raster_columns    | view  | postgres
 public | raster_overviews  | view  | postgres
 public | spatial_ref_sys   | table | postgres
(5 rows)

... and after the import:

walks=# \d
                    List of relations
 Schema |           Name           |   Type   |  Owner   
--------+--------------------------+----------+----------
 public | geography_columns        | view     | postgres
 public | geometry_columns         | view     | postgres
 public | raster_columns           | view     | postgres
 public | raster_overviews         | view     | postgres
 public | route_points             | table    | postgres
 public | route_points_ogc_fid_seq | sequence | postgres
 public | routes                   | table    | postgres
 public | routes_ogc_fid_seq       | sequence | postgres
 public | spatial_ref_sys          | table    | postgres
 public | track_points             | table    | postgres
 public | track_points_ogc_fid_seq | sequence | postgres
 public | tracks                   | table    | postgres
 public | tracks_ogc_fid_seq       | sequence | postgres
 public | waypoints                | table    | postgres
 public | waypoints_ogc_fid_seq    | sequence | postgres
(15 rows)
不可一世的女人 2024-12-08 18:16:46

如果您使用的是 Linux,您可以尝试以下操作:

  1. 使用程序将 GPX 转换为 SHP:gpx2shp

    sudo apt-get install gpx2shp
    ...
    gpx2shp -o 输出文件.shp infile.gpx
    
  2. 然后使用 shp2pgsql

    将该文件加载到支持 postgis 的数据库中

    sudo apt-get install postgis
    ...
    shp2pgsql 输出文件.shp gis_table
    

您当然可以使用管道并在一个命令行中进行所有操作

有关详细信息,请参阅联机帮助页。

编辑
如果您仍然需要 python 脚本,可以在这里找到帮助
http://pypi.python.org/pypi/gpxtools

If you are using linux, you may try this:

  1. Use a program to convert GPX to SHP: gpx2shp

    sudo apt-get install gpx2shp
    ...
    gpx2shp -o output_file.shp infile.gpx
    
  2. then load that file into a postgis enabled database with shp2pgsql

    sudo apt-get install postgis
    ...
    shp2pgsql output_file.shp gis_table
    

you may of course use pipe and make all in one command line

For more info see the manpages.

EDIT
If you still want a python script, you may find help here
http://pypi.python.org/pypi/gpxtools

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