Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I load multiple gpx files into PostGIS?

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 :/

like image 814
radek Avatar asked Aug 29 '11 20:08

radek


2 Answers

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)
like image 192
Nelson Avatar answered Oct 04 '22 14:10

Nelson


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

like image 29
jperelli Avatar answered Oct 04 '22 13:10

jperelli