# This program, based on an example found on the web, will take a "gpx" extract # file and strip it down to only lat/long and GC Number, in CSV format. # Started 3/1/13 ## We must follow tight PERL conventions here use strict; use warnings; ## This was built using Windows 7 and "Strawberry Perl", which ## you can get from StrawberryPerl.com (Free!) ## Note: You MUST add this "Geo::Gpx" module into your Strawberry perl build, using CPAN ## the program which installs modules and re-compiles PERL, as Geo::Gpx ## is not a base part of the perl distribution. "cpan Geo::Gpx" worked ## but the mixed case is necessary. It orignally said other things were missing, ## but it then went ahead and found everything necessary. Panic not! # These modules are required use Geo::Gpx; use DateTime; ## Download a GPX file (Pick a tiny one to start with!) and place it with your ## perl script in the same folder. Rename the .gpx file to "raw.gpx" ## and make sure it is in fact a .gpx file. If it has the wrong file type, ## this program won't run! Use Windows Explorer to rename it if necessary. ## Here we go! ## To run this, the syntax is "perl gpsparse.pl". ## About 15 seconds later, you should see the count of lines generated ## and there is a new file named "output.csh", comma seperated values. ## Look at that, it should be obvious. Lat/Long and GC number ## (Eventually I shall strip out the GC number, but left it in just in case ## I want the Flora to send that to an LCD display. # Open the GPX file open(my $fh_in, "< raw.gpx") or die "Could not open file:$!"; # Clear the record counter my $linecount = 0; ## if the file opened normally, greet the user print "\nGPX to Lat/Long Conversion, Version 1.0\n"; print "Input file: raw.gpx \n"; print "Output file: output.csv \n"; # Parse GPX my $gpx = Geo::Gpx->new( input => $fh_in ); # Close the GPX file close $fh_in; # Open the output file open(my $fh_out, "> output.csv") or die "Could not open file:$!"; # The waypoints-method of the GEO::GPX-Object returns an array-ref # which we can iterate in a foreach loop foreach my $wp ( @{ $gpx->waypoints() } ) { # Some fields seem to be optional so they are missing in the hash. # We have to add an empty string by iterating over all the possible # hash keys to put '' in them. Map is like a foreach loop. ## Note: If you add/change a field in the Join below, ## So make sure you add it back to this map! (Ask me why I know this?) map { $wp->{$_} ||= '' } qw( lat lon name ); # Join the fields with a comma and print them to the output file print $fh_out join(',', ( $wp->{'lat'}, $wp->{'lon'}, $wp->{'name'}, )), "\n"; # Add a newline at the end #Increment the record counter $linecount++; } # Close the output file close $fh_out; print "Output file: output.csv has been created with $linecount records! \n";