Wednesday, August 12, 2015

GPX to postgres bash script

To import a gpx file into postgres do this:

# ogr2ogr -append -f PostgreSQL "PG:dbname=ctbto user=postgres" 2016114.gpx

cool


And a bash script would look like this (compliments of Mapbox from http://www.mapbox.com/guides/postgis-manual/):

files=(
        2016114.gpx
        2016115.gpx
)
for file in ${files[@]}; do
        ogr2ogr -append -f PostgreSQL "PG:dbname=ctbto user=postgres" $file
done


or

for file in /dir/*.gpx
do
     ogr2ogr -append -f PostgreSQL "PG:dbname=ctbto user=postgres" $file
done


Adding a column while importing:

ogrinfo -f PostgreSQL "PG:dbname=your_db user=xxxx password=yyyy" -sql "UPDATE tracks SET track_id = 1234 WHERE ogc_fid = (SELECT max( ogc_fid ) FROM tracks)"

(from: http://gis.stackexchange.com/questions/101825/add-gpx-files-to-postgresql-database-and-add-one-column-with-a-given-value)