How can I load multiple gpx files into PostGIS?
Asked Answered
P

2

7

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

Posture answered 29/8, 2011 at 20:43 Comment(0)
G
10

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)
Germanophile answered 16/4, 2012 at 23:5 Comment(0)
F
3

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

Fulgurite answered 30/8, 2011 at 5:47 Comment(6)
thanks for pointers. would that solution give me separate shp file for each gpx input? or is it possible to combine all gpx files into one shp?Posture
@radek You could import gpx files to R, merge them and export them using to a shape file using rgdal. Of course, once you have the gps files in R, you could also use SQL to import the data into a PostGIS DB.Personalty
@Roman: thanks. i did some work with R + SQL + Postgres so should be able to sort this part. is there any particular method/package you could recommend to work with gpx files tho?Posture
There's a function to read gpx (readGPS I think) files in maptools (uses gpsbabel). I also wrote a gpx scraper function that uses only the XML package. Drop me a line if you'd be interested in giving it a try (looking for beta testers anyway :) ).Personalty
@Roman: i'm trying to find my way through this problem in R as well. afaik readGPS() is interface to GPSBabel software. i tried using system() call with manual definition of parameters, but had no success so far. would greatly appreciate if you could share your work :]Posture
If you're on Windows, you can add the gpsbabel directory (where the exe is located) into the environmental path (PATH) and you'll have access to it directly through the readGPS function (that calls system).Personalty

© 2022 - 2024 — McMap. All rights reserved.