#!/usr/bin/perl
# Copyright       : http://www.fsf.org/copyleft/gpl.html
# Author          : Dan Jacobson -- http://jidanni.org/geo/taiwan_datums/
# Created On      : Thu Mar 16 17:37:29 2006
# Last Modified On: Mon Jun 29 03:30:56 2009
# Update Count    : 121

=head1 DESCRIPTION

This is an example program to convert Taiwan's TWD67 -> TWD97
coordinates.

=head1 USAGE
$ xylonlat
with standard input of lines like
   #A comment
   235571[.123] 2675382[.456]
gives output:
   ##A comment
   #1 235571 2675382
   TWD67:
   235571 2675382
   120.857978243173 24.1836801864713
   120d 51m 28.72s 24d 11m 1.25s
   TWD97:
   236399 2675176
   120.866138239638 24.1819144465914
   120d 51m 58.10s 24d 10m 54.89s
For Penghu, append its meridian, 119:
   276486 2614675 119
Jinmen and Mazu are not yet supported.

=cut

use strict;
use warnings; # FATAL => 'all';
use constant II    => "invalid input";
use Geography::NationalGrid;
use Geography::NationalGrid::TW;
my $conversion;
while (<>) {
    if (/^(#|$)/) { print "#$_"; next }    #comments
    print "#", ++$conversion, " ", $_;
    chomp;
    die II unless @_ = /^([\d.]+)\s+([\d.]+)(\s+\d+)?$/;
    my $projection = 'TWD67';
    my $point      = new Geography::NationalGrid::TW(
        'Projection' => $projection,
        'Easting'    => shift @_,
        'Northing'   => shift @_,
    );
    print "$projection:\n";
    print_point( $point, $_[0] );
    $projection = 'TWD97';
    $point      = $point->transform($projection);
    print "$projection:\n";
    print_point( $point, $_[0] );
    print "-" x 22, "\n";
}

sub print_point {
    my $correction = 0;
    $_ = shift;
    print $_->easting, " ", $_->northing;
    for ( $_[0] ) {
        if (defined) {
            die "Append 119 for Penghu" unless $_ == 119;
            print " $_";
            $correction = -2;
        }
    }
    print "\n", $_->longitude + $correction, " ", $_->latitude, "\n",
      $_->deg2string( $_->longitude + $correction ), " ",
      $_->deg2string( $_->latitude ), "\n";
}