GPX from Google Maps KML J Script

In preparation for my Arizona jaunt to watch the May 20th annular eclipse I spent a few hours on Google Maps selecting locations to visit.  Here are my prime targets.

After selecting targets the next step is to load them onto my “GPS device.” Currently my GPS device is the MotionX GPS iPhone app.  MotionX can read GPX files in many ways but you need GPX files not Google Maps KML files. Converting KML to GPX is a recurring nuisance. I’ve used online converters for this chore but today, after being annoyed by this problem for the zillionth time, I dashed off a J script that transforms Google Maps KML to GPX.  The main verb gpxfrmapkml is shown below. The entire script is available here. Happy KML to GPX’ing my friends.

gpxfrmapkml=:3 : 0

NB.*gpxfrmapkml v-- gpx from Google maps kml.
NB. monad:  clGpx =. gpxfrmapkml clKml
NB.   NB. download Google map waypoints as kml
NB.   kml=. read 'c:/temp/arizona annular eclipse.kml'
NB.   NB. convert to gpx and save
NB.   gpx=. gpxfrmapkml kml
NB.   gpx write 'c:/temp/arizona annular eclipse.gpx'  

NB. parse kml form waypoint table
dname=. ;'name' geteletext '<Placemark>' beforestr y
wpt=.   ;'Placemark' geteletext y
wpt=.   ('name' geteletext wpt) ,. <;._1&> ','&,&.> 'coordinates' geteletext wpt
hdr=.   ;:'phototitle longitude latitude'

NB. format gpx header 
gpxstamp=. 'Waypoints: ',(":#wpt),' GPX generated: ',timestamp''
gpxheader=. ('/{{headername}}/',dname,'/{{headerdescription}}/',gpxstamp) changestr GPXFRKMLHEADER
gpxtrailer=. GPXTRAILER

rvarbs=. idx htmlvarbs pkml

NB. all row varibles must exist in data header
assert. *./ rvarbs e. hdr
rows=. (#wpt) # ,: pkml
rows=. ((hdr i. <'phototitle'){"1 wpt) (<a:;(rvarbs i. <'phototitle'){idx)} rows
rows=. ((hdr i. <'latitude'){"1 wpt) (<a:;(rvarbs i. <'latitude'){idx)} rows
rows=. ((hdr i. <'longitude'){"1 wpt) (<a:;(rvarbs i. <'longitude'){idx)} rows