Import GPX using Python (ArcGIS for Desktop)

In the previous article I presented how to import Sports GPX data into ArcGIS for Desktop (example taken with Runtastic). I did the exercise for a single GPX file but what do you do if you have several favorite tracks circuits? After having downloaded your routes, the import of hundred of GPX files by doing it manually through the ArcMap interface would take ages!!!

Simple solution: using a Python script!

Here is the little standalone script (= which does not require ArcMap to be open) that

  • takes all the gpx files from a unique folder
  • convert it into features placed in the specified geodatabase
  • convert the feature layer into .lyr files
  • style them

import arcpy, os
from os import listdir
from os.path import isfile, join
from arcpy import env
arcpy.env.overwriteOutput = True

def convertGPX2feature(gpxFolder, outputGdb, referenceSymbologyLayer, outputLayerFolder):

env.workspace = outputLayerFolder
files = [f for f in listdir(gpxFolder) if isfile(join(gpxFolder,f)) ]
print “Number of files: ” + str(len(files))
for f in files:

print f
if f.endswith(‘.gpx’):

# Convert files from .gpx to feature layer
inputData = gpxFolder + ‘\\’ + f
featureName = f.partition(‘.gpx’)[0]
outputLocation = outputGdb + ‘\\’ + featureName

# Convert the point layer to lines
inputData = outputLocation
featureLineName = featureName + ‘_line’
outputLocationLine = outputGdb + ‘\\’ + featureLineName



# Make a layer from the feature class

# Apply standard Symbology
arcpy.ApplySymbologyFromLayer_management(featureName, referenceSymbologyLayer)

# Save layer in .lyr file
arcpy.SaveToLayerFile_management(featureName, featureName)

# Package Layer
arcpy.PackageLayer_management(featureName, featureName + ‘.lpk’, “PRESERVE”, “CONVERT_ARCSDE”, “DEFAULT”, “ALL”, “ALL”, “ALL”, “”, “”, “”)


print arcpy.GetMessages()

if __name__ == ‘__main__’:
gpxFolder = r’D:\PERSONAL\themagiscian\articles\gpx\rawdata’
outputGdb = r’D:\PERSONAL\themagiscian\articles\gpx\batchimport.gdb’
referenceSymbologyLayer = r’D:\PERSONAL\themagiscian\articles\gpx\gpx_symbology.lyr.lyr’
outputLayerFolder = r’D:\PERSONAL\themagiscian\articles\gpx\batchimport’
convertGPX2feature(gpxFolder, outputGdb, referenceSymbologyLayer, outputLayerFolder)

I saved this script naturally under the name (GRAB IT FROM HERE!) and it splits into two parts:

  • First step, at the bottom of the file, which defines the variables
    • the folder containing the gpx files
    • the output gdb where the converted gpx will be placed
    • the path to reference layer for the symbology (Please the one that I used here: the lyr and the lpk (please unpack it before use))
    • the output folder for the final lyr files
    • the call to the main function convertGPX2feature
  • Second step contents
    • an array called “files”, which is filled with the path to all the gpx files, is put in a ‘for’ loop and each item is used in the GPXtoFeatures_conversion tool.
    • A little “partition” has been applied to keep the feature names not as long as the original data

Of course, this script assumes all the gpx files are in the same folder. The script has to be adapted if they are in different folders.

Spot the circuits on the map

The steps afterwards are styling the map by choosing a nice layout and by adding the necessary cartographic information in order to show on a map your favourite tracks.

My Tracks
My Tracks

This is one example of a presentation but there are many others.

Leave a Reply

Your email address will not be published.

Proove you're not a robot * Time limit is exhausted. Please reload CAPTCHA.