Friday 29 October 2010

proj4 and Perl for windows

All amazing people the world over use Perl to get stuff done. Sometimes the world conspires to make this a tiny bit harder than it should be. Today I want to convert coordinates using the proj4 library, but could not because the geo::proj4 library doesn't build nicely under windows. But, ugly to the rescue:

2) Do the ugly:

# this works, would you adam and eve it
sub coords2pols {
my ($lat, $lon) = @_;
if ($lon > -50) {
($lon, $lat) = ($lat, $lon);
}

open my $fhi, '>tmp.txt' or die "tmp file $!";
print $fhi "$lon $lat\n";
close $fhi;

system('cs2cs +proj=longlat +ellps=WGS84 +datum=WGS84 +no_defs +to +proj=stere +lat_0=-90 +lat_ts=-71 +lon_0=0 +k=1 +x_0=0 +y_0=0 +ellps=WGS84 +datum=WGS84 +units=m +no_defs tmp.txt > tmp.out.txt') and die "$@";
open my $fh, '
my $line = <$fh>;
close($fh);
chomp($line);
my ($x, $y) = split(/\s+/, $line);
unlink('tmp.txt');
unlink('tmp.out.txt');
($x, $y);
}

2 comments:

  1. Boo, Polar Stereographic makes stuff upside down...

    ReplyDelete
  2. As a former resident of 26W 77S, I beg to differ :)

    ReplyDelete