gmtperl example

#! /usr/bin/perl -w
#
# gmtperl-example.pl
#
# J.J.Green 2001, 2004
# This code is in the Public Domain

use Cartog::GMT;

$usage = "usage: perl gmtperl-example.pl <filename>\n";

# get output filename (no fancy getopt() here)

$output = shift @ARGV or die $usage;

# abbreviate some GMT command line options, etc

$range = "-R-1.0/1.0/53.3/54.6";
$proj  = "-JM3.5i";
$ticks = "-B1/1";
$res   = "-Df";
$defs  = "gmtperl-example.def";

$common = "$range $proj -P";

# the town data; array populated from a "here" document
# containing data pasted from database output

@towndat = <<TOWNS =~ m/(\S.*\S)/g;
   0.0333333    53.7333333      Withernsea
  -0.4141667    54.2872222      Scarborough
  -0.1666667    53.9            Hornsea
  -0.2891667    54.21           Filey
  -0.2080556    54.095          Bridlington
TOWNS

# callback function to write out the lat/long values
# from a @towndat like array

# callback function to write out the lat/long values
# from a @towndat like array

sub xycat
{
    my ($stream, $lines) = @_;

    foreach my $line (@$lines)
    {
        chomp $line;

        my ($lng, $lat) = split ' ', $line;

        print $stream "$lng $lat\n";
    }
    return 1;
}

# callback function to modify lines of towndata to the
# psxy format. The second argument is a reference to an
# array with components ($size, $angle, $font, $align, $lines) where
#
#   $size   font size in (postscript) points
#   $angle  angle of rotation
#   $font   font number or name
#   $align  alignment
#   $lines  a reference to an array of lines to be processed

sub xyformat
{
    my ($stream, $data) = @_;

    # unpack the arguments

    my ($size, $angle, $font, $align, $lines) = @$data;

    foreach my $line (@$lines)
    {
        chomp $line;

        my ($lng, $lat, $name) = split ' ', $line;

        print $stream "$lng $lat $size $angle $font $align $name\n";
    }
    return 1;
}

# set up the map

$map = new Map;

$map->defaults($defs);
$map->output($output);
$map->verbose(1);

# use the Debian GMT wrapper script

$map->wrapper('GMT');

# get the map's layer set

$layers = $map->layers(new Layers);

# add coast layers ...

$layers->pscoast("$common $res -G240/255/240");
$layers->pscoast("$common $res -W1p/30/120/150");

# town positions, using the xycat callback

$layers->psxy("$common -Sc4p -W3 -G255", \&xycat, \@towndat);

# towns names using the xyformat callback and passing the
# text parameters and raw town data by reference

$layers->pstext("$common -D4p/0p -S5/255", 
                \&xyformat, 
                [10, 0, "Bookman-Demi", "LM", \@towndat]);

# add map furniture

$layers->psbasemap("$common $ticks");

# now draw it

$map->draw();
script output
North-East England (pdf)