8

I have a whole bunch of GPX files that I need to load into QGIS and loading them one at a time using the GPS plugin is a bit painful. It will not allow one to select multiple files -- is there another way?

I did find this: Automating batch load of multiple GPX files into PostGIS but I need to figure out a lot more about PostGIS and GDAL before trying that approach.

BTW I am using Mac version of QGIS -- just realised that this may be OS related

2
  • This link is a tutorial about batching in QGIS. Its mostly about clipping data, but you may be able to tailor it to your needs.
    – TsvGis
    Sep 29, 2015 at 22:17
  • Hi. It helped a little but I still have to open each .GPX file and select which feature type (wp, track, route, track-points or route-points) one by one. I have found that with “Save Selected Features” from QGis geoalgorithms I can save all layers to .SHP, but first I need to rename each layer, since all have the same name (in my data “tracks“)… Only after that it’s possible to execute as batch process the geoalgorithm. Without renaming, the batch process it’s applied always to the same layer. For waypoints the “Convert format” from GDAL/OGR geoalgorithm does the perfect job…! Oct 12, 2015 at 16:18

8 Answers 8

7

Using Add Vector layer you can select multiple files by mouseclicking (at least on Windows), but you have to select for each GPX file what layer you want to add (most probably "tracks").

I guess this could be a good idea for a selfmade plugin, but I have not yet found the time to do that.

2
  • Ah! yes that works. The small catch is that you still need to select between track, waypoint, route, etc. with no indication of which gpx file you are referring to. Still beats going throught all the menus for each file. Also the file names are not reflected in the layer names. Apr 6, 2013 at 7:14
  • True, but you still get the filename as tooltip if you hover over with the mouse. I use that step only temporarily to add the geometries with copy and paste to my spatialite db.
    – AndreJ
    Apr 6, 2013 at 7:59
6

I know this is very late but it might help someone else...

I had exactly similar problem. I had to create a python script. Later on, I decided to contribute by creating a QGIS plugin.

What the plugin does is, it creates a single QGIS vector layer out of all GPX files in a selected path. You can convert the features into points, polygons or lines. You can choose for tracks, waypoints and routs. All gpx features will have the file name of the gpx as an attribute. The plugin is located in QGIS plugin repository with a name Batch GPS Importer or at this page https://plugins.qgis.org/plugins/batch_gps_importer/ you can read the documentation here.

If you still want to segregate them into separate layer, you could make selection and create a new layer.

2
5

You could use the following code in your Python console. Load the .gpx file into QGIS and change the result_path to your desired location:

import processing

result_path = "C:/Users/You/Desktop/Test/Results//"
for layer in QgsMapLayerRegistry.instance().mapLayers().values():
    processing.runalg("gdalogr:convertformat", layer, 0, '', result_path + layer.name())
3
  • 1
    Do I need to load each .gpx into QGis first? Oct 5, 2015 at 14:59
  • @RicardoM.NogueiraMendes - Yes so hopefully you don't have too many .gpx files :)
    – Joseph
    Oct 5, 2015 at 15:01
  • 1
    Unfortunately, I have a few hundreds...! :( Oct 6, 2015 at 17:45
4

I know that this has already been answered .. but I couldn't use the answer and kept coming back to this question on every google search I did. So I made a mini python program to make a bunch of .gpx files into one big file. Thus getting around the problem of loading multiple files. Hopefully this helps someone else.

Once you have your new gpx file load it through the GPXtools plugin.

Copy and paste into a PLAIN text file. This code assumes you have python (2.x) installed. Made for Ubuntu, but should work on Windows or Mac

#!/usr/bin/env python
"""
INSTRUCTIONS:
    1. Open a terminal (Linux, Mac) or command prompt (Windows).
    2. Type in "python <the name of this file> <the name of the folder containing
        you gpx files to combine>" 'Naturally withought quotes ;|'
    3. Visit said folder and look for a file called "CombinedGPX.gpx"
    4. Post if this doesn't work and/or maybe update this program so it does.
"""
# This program will combind all gpx files in a given location into one large file.
# NOTE: the metadata of the first file (this is arbitrary and decided by the OS)
# is kept, all other metadata is forgotten.

# Possible additions .. make program execute from drag and drop **later
#chmod +x <filename>
#!/usr/bin/env bash
#python <source_file.py> "$1"

import sys
import glob

# Get location from user
# This input should be in the form: $ python <gpxcombiner.py> <GPXFolder/>
# NOTE: the second argument MUST end with a "/"

gpxdir = sys.argv[1]
gpxfiles = glob.glob(gpxdir + "*.gpx")
bigGPX = gpxdir + 'CombindedGPX.gpx'

# Remove precombined GPX file so you don't get an infintely expanding file
try:
    gpxfiles.remove(bigGPX)
except ValueError: # Ignore this if file doesn't exist
    pass

print 
print
print "Directory contains: " + str(len(gpxfiles)) + " .gpx files to be amalgamated."

def split_file(filename, body_start_split='</metadata>', body_end_split='</gpx>'):
    data = filename.read()
    start_body = data.find(body_start_split) + len(body_start_split)
    end_body = data.find(body_end_split)
    header = data[:start_body]
    body = data[start_body:end_body]
    footer = data[end_body:]
    return header, body, footer

first_file, gpxfiles = gpxfiles[0], gpxfiles[1:]
with open(first_file, 'r') as infile:
    header, body, footer = split_file(infile)

    with open(bigGPX, 'w') as outfile:
        outfile.write(header)
        outfile.write(body)

        for fname in gpxfiles:
            with open(fname, 'r') as infile:
                header, body, null = split_file(infile)

            outfile.write(body)
        outfile.write(footer)

print bigGPX + " created, contains combined data."
print
print
0
4

you can use this code:

import ogr
import os

#############
# reading a GPX
in_path = os.path.join('C:\\','python','GPX','FeierabendrundeMitJakob.gpx')

# get the correct driver
in_driver = ogr.GetDriverByName('GPX')

# 0 means read-only. 1 means writeable.
places_source = in_driver.Open(in_path, 0) 

# write the data to a shape file
out_file = os.path.join('C:\\','python','GPX','FeierabendrundeMitJakob.shp')
out_driver = ogr.GetDriverByName('ESRI Shapefile')
out_ds  = out_driver.CopyDataSource(places_source,\                                                        out_file)
3

In the Osgeo4w shell that comes with QGIS, you can run this batch file on Windows:

for %%N in (D:\Karten\gpx\Topo\*.gpx) DO ogr2ogr -t_srs epsg:4326 D:\Karten\gpx\Topo\%%~nN-wp.shp %%N waypoints
for %%N in (D:\Karten\gpx\Topo\*.gpx) DO ogr2ogr -t_srs epsg:4326 D:\Karten\gpx\Topo\%%~nN-rt.shp %%N routes
for %%N in (D:\Karten\gpx\Topo\*.gpx) DO ogr2ogr -t_srs epsg:4326 D:\Karten\gpx\Topo\%%~nN-tr.shp %%N tracks
pause

Syntax for Linux and Mac might be slightly different.

Note that you get three shapefiles for every GPX, because the shapefile format does not allow for points and lines in the same file. You might get empty files if no waypoints or routes or tracks are in the source file.

4
  • Hi, it seems promising but I got “%%N was unexpected at this time” Could it be a syntax issue? I believe that what I’m trying to order is something like: For any file included in the folder with the GPX format, convert to SHP format (one for WP, one for routes and one for tracks), and save it that folder Did I misspell it? for %%N in (D:\UsersDocs\RNM\Desktop\gpx2shp*.gpx) DO ogr2ogr -t_srs epsg:4326 D:\UsersDocs\RNM\Desktop\gpx2shp\%%~nN-wp.shp %%N waypoints... pause Oct 5, 2015 at 14:45
  • Did you miss the backslash before *.gpx? Are you on Windows?
    – AndreJ
    Oct 5, 2015 at 15:03
  • Did anyone figure out what the "%%N was unexpected at this time" error was caused by? I get it too, and I have the appropriate backslashes.
    – Johanna
    Jul 31, 2016 at 4:06
  • 1
    It makes a difference if you write the comand in a batch file or typed manually, see stackoverflow.com/questions/7769591/was-unexpected-at-this-time and superuser.com/questions/894475/…
    – AndreJ
    Jul 31, 2016 at 5:26
1

DNR GPS loads multiple GPX files. Then, Save As -> new shapefile.

0

This python script works great for me when importing multiple gpx files from Strava.

Replace path/to/gpx/folder with the path to your gpx files.

Open the python console in QGIS and run following code.

  • Import with all layers:
import os

path = "path/to/gpx/folder"
names = ["waypoint", "route", "track", "route_point", "track_point"]

for dirpath, subdirs, files in os.walk(path):
    for f in files:
        for name in names:
            iface.addVectorLayer(os.path.join(dirpath, f)+"?type="+name, name, "gpx")
  • Import only track layer:
import os

path = "path/to/gpx/folder"
names = ["track"]

for dirpath, subdirs, files in os.walk(path):
    for f in files:
        for name in names:
            iface.addVectorLayer(os.path.join(dirpath, f)+"?type="+name, name, "gpx")
  • Import only track layer and name like file name:
import os

path = "path/to/gpx/folder"
names = ["track"]

for dirpath, subdirs, files in os.walk(path):
    for f in files:
        for name in names:
            iface.addVectorLayer(os.path.join(dirpath, f)+"?type="+name, f, "gpx")

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.