#!/usr/bin/perl
# Copyright       : http://www.fsf.org/copyleft/gpl.html
# Author 作者     : 積丹尼 Dan Jacobson -- https://www.jidanni.org/
# Thanks 特別感謝 : millerliu@gmail.com
# Created On      : Thu Jan 17 11:54:56 2013
# Last Modified On: Sun Jun  7 17:59:02 2020
# Update Count    : 428
## 將 millerliu GPX 廣播電台座標 GPX 檔轉成 KML
use strict;
use warnings FATAL => q(all);
use XML::TreePP;
my $tree->{kml}->{Document}->{name} = "台灣廣播電台發射點位址圖";
$tree->{kml}->{-xmlns} = "http://www.opengis.net/kml/2.2";
my $tpp = XML::TreePP->new(
    indent    => 2,
    first_out => [qw/name open description Style TimeStamp styleUrl Point/]
);
##my @colors = qw/red green blue yellow/;
my @colors = qw/red grn blue ylw/;
my %FolderIndex;
my $c = 0;

for (qw/AM FM SW x/) {
    push @{ $tree->{kml}->{Document}->{Style} }, [
        {
            -id       => $_,
            IconStyle => {
                Icon => {
                    href =>
## 2020: /opt/google/earth/pro/resources/userpalette.kml has these that will work offline?:
##<href>http://maps.google.com/mapfiles/kml/pushpin/blue-pushpin.png</href>
##<href>http://maps.google.com/mapfiles/kml/pushpin/grn-pushpin.png</href>
##<href>http://maps.google.com/mapfiles/kml/pushpin/ltblu-pushpin.png</href>
##<href>http://maps.google.com/mapfiles/kml/pushpin/pink-pushpin.png</href>
##<href>http://maps.google.com/mapfiles/kml/pushpin/purple-pushpin.png</href>
##<href>http://maps.google.com/mapfiles/kml/pushpin/red-pushpin.png</href>
##<href>http://maps.google.com/mapfiles/kml/pushpin/wht-pushpin.png</href>
##<href>http://maps.google.com/mapfiles/kml/pushpin/ylw-pushpin.png</href>

                      ## "http://maps.gstatic.com/intl/en_ALL/mapfiles/ms/icons/"
                      "http://maps.google.com/mapfiles/kml/pushpin/" . ( shift @colors )
                      ##. "-dot.png",
                      . "-pushpin.png",

                }
            }
        }
    ];
    last if /x/;
    $FolderIndex{$_} = $c;
    $tree->{kml}->{Document}->{Folder}[ $c++ ] =
      { name => $_, open => 0 };

}

# my $latest = ''; But he could mark a station as decommissioned and this would miss it.
for ( @{ $tpp->parsefile("/dev/stdin")->{gpx}->{wpt} } ) {

    # if ( ( $_->{time} cmp $latest ) == 1 ) { $latest = $_->{time}; }
    my @name = split /\s+/, $_->{name};
    $name[0] =~ s/^x//;
    my $decommissioned = $& || '';
    my @coordinates;
    for ( $_->{-lon}, $_->{-lat}, $_->{ele} ) {
        if (defined) {
            my $s = sprintf "%.6f", $_;
            $s =~ s/\.?0+$//;
            push @coordinates, $s;
        }
    }
    my $nn = join '', $decommissioned, @name[ 1, 2 ];
    push
      @{ $tree->{kml}->{Document}->{Folder}[ $FolderIndex{ $name[0] } ]->{Placemark}
      }, [
        {
            '-id'       => $nn,
            name        => $nn,
            description => $_->{desc},
            ## https://developers.google.com/kml/documentation/time
            ## but that causes problems for some applications. So commenting out:
            ## TimeStamp => { when => $_->{time} },
            styleUrl => '#' . ( $decommissioned || $name[0] ),
            Point    => {
                coordinates => join ",",
                @coordinates
            }
        }
      ];
}

#$latest =~ s/T.*//;
my ( $sec, $min, $hour, $mday, $mon, $year, $wday, $yday, $isdst ) =
  localtime(time);
$tree->{kml}->{Document}->{description} =
qq{由 <a href="mailto:millerliu\@gmail.com">Miller Liu</a> 逐筆修正 <a href="http://www.ncc.gov.tw/">NCC</a> 電台坐標數據， 再由<a href="https://www.jidanni.org/">積丹尼</a>於 }
  . ( sprintf "%d-%02d-%02d", $year + 1900, $mon + 1, $mday )
  . qq{ 存放於<a href="https://radioscanningtw.fandom.com/wiki/台描:地圖">台描</a>。 "x"=停播。};
print $tpp->write($tree);
