1

I am using ArcMap 10.7.

How do I write a script to insert multiple GPX Files as a Feature Class?

What I was thinking is batching the files together in to one file and then running the code I already have for that one file.

import arcpy     
arcpy.GPXtoFeatures_conversion('c:\\GPX_Files\\Hike.gpx', 'c:\\gisData\\Hike.shp')
2

I haven't tested this, but it should work. I create the output shapefile using the first gpx file. Then I convert the next gpx file in memory and append it to the output and continue this to the end of the gpx files.

import arcpy
import os

#input/output paths
source_folder = r'path\to\gpx\files'
out_fc = r'c:\gisData\Hike.shp'

#build list of gpx files from source folder
gpx_files = []
for root, dir, files in os.walk(source_folder):
  for file in files:
    if os.path.splitext(file)[1] == '.gpx':
      gpx_files.append(os.path.join(root,file))

#create the output feature class using the first gpx file
arcpy.GPXtoFeatures_conversion(gpx_files[0], out_fc) #first gpx gets converted to shp

#append each gpx to the output feature class, skipping the first one (already converted the first gpx to create the output file)
mem_layer_list = []
for gpx in gpx_files[1:]:
  idx = gpx_files.index(gpx)
  mem_layer = '_'.join([r'in_memory\gpx_temp',str(idx)])
  mem_layer_list.append(mem_layer)
  arcpy.GPXtoFeatures_conversion(gpx, mem_layer)

#append all the memory layers to the output feature class
arcpy.Append_management(mem_layer_list,out_fc,'TEST','','')
  • Append only needs to run once, you should create a list of in_memory featureclasses. It would be much more efficient. – Hornbydd Jan 27 at 21:50
  • @Hornbydd, could you explain how I could go about doing so? I had been able to get the script to run up until this point but the append had not been working! – user157149 Jan 27 at 21:58
  • @joey22 see my edit – jbalk Jan 27 at 22:09

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy