Jump to content

Python program to trim PIDs from datasheets embedded in GPX files


Rotareneg

Recommended Posts

I made this little Python script to process the GPX files output from NGS»GPX 2 and strip out the PIDs that head each and every line in the embedded datasheet, making them much easier to read on a hand-held GPS.

 

First, you need Python 3.x installed. Then copy and paste the script into a text file and rename it with a .py extension. Then you can just drag a .gpx file onto the script to run it. A console window will appear and show the PIDs as it trims them, and then it will overwrite the .gpx file with the trimmed version. Don't run it more than once on the same file or it'll trim actual text from each datasheet line. Also, you probably shouldn't feed it really huge files unless you've got plenty of memory: a 90 meg GPX with all the marks in Kansas peaked at 3 gigs of memory while running.

 

And, of course, the disclaimer: If the program causes problems which end up with you thinking "I have no mouth, and I must scream" or somthing even worse, don't blame me. :D

 

import io,sys,time
from xml.dom.minidom import parse

if len(sys.argv) > 1:
   print('File to read: '+sys.argv[1])
else:
print('No file to read.')
time.sleep(3)
quit()

doc = parse(sys.argv[1])
gpx = doc.getElementsByTagName('gpx')[0]
wpts = gpx.getElementsByTagName('wpt')

for i in wpts:
   disc = i.getElementsByTagName(
       'Groundspeak:cache')[0].getElementsByTagName(
           'Groundspeak:long_description')[0].firstChild
   if disc != None:
       print(disc.data[0:8])
       txt = io.StringIO()
       out = io.StringIO()
       txt.write(disc.data)
       txt.seek(0)
       pid = txt.readline()[0:6]
       txt.seek(0)
       for line in txt:
           if line == (pid + '\n'):
               print('\n',end='',file=out)
           else:
               print(line[7:len(line)],end='',file=out)
       disc.data = out.getvalue()

o = open(sys.argv[1],'w')
o.write(doc.toxml())
o.close()
doc.unlink()

Link to comment

Well I installed python and saved the text to a script file and ran it. Doesn't work for me. And I don't work with python so I have no idea how to help fix it.

 

The forum broke it by capitalizing the 'Groundspeak:cache' string on lines 17 and 18, something that the "

" block is [i]supposed[/i] to prevent. To fix the program just make the G in Groundspeak lower case and it'll run.
Link to comment

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
×
×
  • Create New...