#!/usr/bin/perl
# -----------------------------------------------------------------
#
# 'csv2pgx.pl'
#
# Convert a comma-delimited list of waypoints to GPX format.
#
# This script is very ... basic.
# I use this to prepare trips and/or add geographic locations to GPX files.
#
# NOTE: For a list of symbols, see https://gist.github.com/tonymorris/8778137
# NOTE: If you use csv from Google Docs, you may need to run "dos2unix" first
#
# -----------------------------------------------------------------
# This program is free software; you can redistribute it and/or
# modify it under the terms of the version 2 of the GNU General
# Public License as published by the Free Software Foundation.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
# -----------------------------------------------------------------
#
# Copyright (c) 2007...2021 Joerg Hau <joerg.hau@schweizerschrauber.ch>
#
# Revision History:
#   2007-05-21, JHa, first working version.
#   2018-01-04, JHa, input file format adapted
#   2018-01-05, JHa, gpx header now works with Garmin Mapsource
#   2021-04-11, JHa, added phone number
#   2021-07-16, JHa, added skipping of # lines
#   2021-07-27, JHa, added rounding to 5 digits
# -----------------------------------------------------------------

use strict;
use warnings;

# input file is delimited with TAB and comma (!) as follows 
#   # name TAB address TAB phone TAB latitude, longitude TAB symbol TAB sequence
#   Some Place	Via SomeRoad 5, 12345 SomeCity	021-345.678	47.1234, 6.5678	Flag	12
#
# Put a # as first char if you want to skip that line.
#
# output is:
# <wpt lat="47.1234" lon="6.5678">
#   <name>12-Some Place</name>
#   <cmt>Via SomeRoad 5, 12345 SomeCity</cmt>
#   <desc>Some Place, Via SomeRoad 5, 12345 SomeCity. 021-345.678</desc>
#   <sym>Flag</sym>
# </wpt>
#
# NOTE: Garmin Mapsource ignores the DESC text. If you need the address, use the CMT.
#
# Some typical SYM tags are: 
# Scenic Area
# Museum
# Restaurant
# Lodging
# Parking Area
# Flag
# Shopping Center

my $template="<wpt lat=\"LAT\" lon=\"LON\">
  <name>SEQ-NAME</name>
  <cmt>CMT</cmt>
  <desc>NAME, DESC. FON</desc>
  <sym>SYM</sym>
</wpt>\n";


# -----------------------------------------------------------------
# Print usage mode
# -----------------------------------------------------------------
sub usage
{
print <<EOF
$0 - Convert a list of waypoints to GPX.

Copyright (c) 2007, 2018, 2021 Joerg Hau <joerg.hau(at)schweizerschrauber.ch>.

This program is free software; you can redistribute it and-or
modify it under the terms of version 2 of the GNU General Public
License as published by the Free Software Foundation.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

Usage: $0 listfile

EOF
}

# -----------------------------------------------------------------
# testing
# -----------------------------------------------------------------
my $numArgs = $#ARGV + 1;
unless ($numArgs==1) {
    usage;
    die "Error: Please provide input filename.\n"
    }


# -----------------------------------------------------------------
# read file
# -----------------------------------------------------------------
my ($infile) = @ARGV;

if (! -r $infile) {
    die "Can't read '$infile', stopped.\n";
    }
if (! -f $infile) {
    die "Input $infile is not a plain file, stopped.\n";
	}

open (FILE, $infile) or die ("Could not open '$infile' for reading, stopped.");
my ($coords,$lon,$lat,$lo,$la,$name,$seq,$cmt,$desc,$fon,$sym);

# This header works with Garmin Mapsource
#
my $hdr = <<'END_HDR';
<?xml version="1.0" encoding="UTF-8" standalone="no" ?>
<gpx xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" version="1.1" xmlns="http://www.topografix.com/GPX/1/1" creator="csv2gpx.pl by Joerg Hau">
END_HDR

print $hdr;

while (<FILE>)
    { 
    next if /^#/;
    chop $_;
    ($name,$desc,$fon,$coords,$sym,$seq) = split '\t', $_;
    $coords =~ tr/ //ds;
    ($la,$lo) = split ',', $coords;
    
    # do some rounding 
    $lat = sprintf("%.5f", $la);
    $lon = sprintf("%.5f", $lo);
    
	my $temp = $template;
	$temp =~ s/LAT/$lat/g;
	$temp =~ s/LON/$lon/g;
	$temp =~ s/SEQ/$seq/g;
	$temp =~ s/NAME/$name/g;
    $temp =~ s/CMT/$desc/g;
    $temp =~ s/DESC/$desc/g;
    $temp =~ s/FON/$fon/g;
    $temp =~ s/SYM/$sym/g;
	print $temp;
    }

print "</gpx>\n";

close (FILE);

1;
