#!/usr/bin/perl
#
# Makes a map with fixed width using GMT3 and
# converts to a GIF for WWW display using
# ImageMagick's "convert" routine.
$webmaster="rsignell\@usgs.gov";
#
# add /usr/local/bin and /usr/local/bin/gmt to path
#
$ENV{"PATH"} = $ENV{"PATH"} .
":/usr/bin:/usr/local/bin:/usr/local/gmt/bin";
$ENV{"TMPDIR"} = "/usr/tmp";
# set maximum width of plot
$WIDTH = 4.5;
if ($ENV{'REQUEST_METHOD'} eq 'POST') {
#
# Get WWW Form parameters and put in associative array %FORM
#
&Get_Params;
#
&html_header("Your Map!");
$cruise_track = $FORM{cruise_track}; #cruise_track contains lon,lat pairs now
#
# write lon/lat data to temp file
#
$cruise_track =~ s/\x0D//g;
$cruise_track =~ s/\x09//g;
open(CRUISE_DATA,">/usr/tmp/$$.cruise");
print CRUISE_DATA $cruise_track;
close(CRUISE_DATA);
$labels = $FORM{labels};
#Labels for points
# --- Write to a temporary file --
$labels =~ s/\x0D//g;
$labels =~ s/\x09//g;
open(CRUISE_LABELS,">/usr/tmp/$$.labels");
print CRUISE_LABELS $labels;
close (CRUISE_LABELS);
#
# Some browsers apparantly put control characters like line feeds
# and other garbage into $cruise_track, which screws up the GMT
# routine minmax. Here I've used "dos2unix" to get rid of the line
# feeds and other DOS control characters, but it would be better
# to just use Perl to do this (I don't have my Perl book handy
# right now). Of course, it would be better just to have a
# perl minmax routine also! (dos2unix is a one line script that
# calls GNU's "recode" thusly: "recode ibmpc:ascii $1" )
#system("(dos2unix /usr/tmp/$$.cruise) > /dev/null");
#system("/usr/local/bin/dos2unix /usr/tmp/$$.cruise");
#
# find min and max of lon,lat data using GMT routine minmax
($lon_min, $lon_max, $lat_min, $lat_max) = &minmax ();
#$str = `minmax /usr/tmp/$$.cruise`;
#$str =~ s/>//g; # strip out all ">"
#$str =~ s/\s//g; # strip out all white space
#@fields = split(/, $str); # split on "<" sign
#($lon_min,$lon_max)=split(/\//,$fields[1]); # split lon min/max pair on "/"
#($lat_min,$lat_max)=split(/\//,$fields[2]); # split lat min/max pair on "/"
$sym = $FORM{symbol};
$range = $FORM{range};
$topo = $FORM{topo};
$pol = $FORM{pol};
$river = $FORM{river};
#
&Plot_Coast;
system("mv /usr/tmp/$$.png /home/www/tmp/$$.png");
system("mv /usr/tmp/$$.ps /home/www/tmp/$$.ps");
system("rm /usr/tmp/$$.*");
print("
\n");
print("
You might want to grab the high-quality PostScript version of the above map
\n"); # print out glab for debugging # print("glab= $glab\n"); &html_trailer; #system("rm /home/www/tmp/$$$.gif"); } else { &html_header("Comment Form Error"); print "
\n"; print "Form input was not processed."; &html_trailer; } sub html_header { $document_title = $_[0]; print "Content-type: text/html\n\n"; # print "Cache-Control: no-cache\n"; print "\n"; print "
\n"; print "\n";
}
sub html_trailer {
print "\n";
print "\n";
}
sub Plot_Coast {
#
# use GMT to draw coastline, then convert PS to GIF
$dlon = $lon_max - $lon_min;
$dlat = $lat_max - $lat_min;
# handle case when user only enters 1 point!
if($dlon == 0){
$dlon=2;
}
if($dlat == 0){
$dlat=2;
}
#
# make bounds of plot 10% greater than data range on all sides
# if selected by user
if($range =~ /^a/){
$lon_min = $lon_min - 0.1 * $dlon;
$lon_max = $lon_max + 0.1 * $dlon;
$lat_min = $lat_min - 0.1 * $dlat;
$lat_max = $lat_max + 0.1 * $dlat;}
else {
$lon_min = $lon_min - 0.0 * $dlon;
$lon_max = $lon_max + 0.0 * $dlon;
$lat_min = $lat_min - 0.0 * $dlat;
$lat_max = $lat_max + 0.0 * $dlat;
}
if($lon_min<-180) {$lon_min=-180}
# if($lon_max>180) {$lon_max=180}
$dlon = $lon_max - $lon_min;
$dlat = $lat_max - $lat_min;
#
# Don't want the plot too tall, or it'll blow out the image converter
# since we have a fixed width. Kludge: set the maximum aspect ratio
# of dlat/dlon to 4.
#
if ($dlat/$dlon > 4) {
$lon_max = $lon_min + $dlat/4.;
$dlon = $dlat/4;
}
# determine inches per degree
$scale=$WIDTH/$dlon;
# Figure out what resolution switch to use in pscoast
# (may have to tinker with this a bit)
if($scale > 8) {
$res='f';
}
elsif ($scale > 2) {
$res='h';
}
elsif ($scale > .5) {
$res='i';
}
elsif ($scale > .1) {
$res='l';
}
else{
$res='c';
}
#
# figure out how often to put labels. Every 1 inch would be nice
# Also try to make labels nave "nice" values (intervals of 5 minutes)
$glab = 1/$scale;
if ($glab > 5) {
$glab = int(($glab/5)+.5)*5;
}
elsif ($glab > 1) {
$glab = int($glab+.5);
}
elsif ($glab > (1/12)) {
$glab = int(($glab*12)+.5)/12;
if ($glab < 1) {
$glab = int($glab*60+.5);
$glab = $glab . "m";
}
}
# call GMT routine PSCOAST to draw the coastline using Mercator projection.
# call GMT routine PSXY to draw symbols on top of coastline
# convert PostScript GMT map to GIF using the ImageMagick routine
# "convert"
system("nice pscoast -JM$WIDTH -W1/150/80/80 -G240/220/80 -S80/160/230 -K -D$res -P -R${lon_min}/${lon_max}/${lat_min}/${lat_max} -B$glab > /usr/tmp/$$.ps");
if ($topo eq "yes") {
system("nice grdimage /var/www/cgi-bin/etopo2.grd -R/${lon_min}/${lon_max}/${lat_min}/${lat_max} -JM$WIDTH -C/var/www/cgi-bin/etopo2.cpt -O -K -P >> /usr/tmp/$$.ps");
}
if ($pol eq "yes") {
system("nice pscoast -JM$WIDTH -Na/1/1ta -D$res -R${lon_min}/${lon_max}/${lat_min}/${lat_max} -O -K -P >> /usr/tmp/$$.ps");
}
if ($river eq "yes") {
system("nice pscoast -JM$WIDTH -Ia/1/0/0/255 -D$res -R${lon_min}/${lon_max}/${lat_min}/${lat_max} -O -K -P >> /usr/tmp/$$.ps");
}
system("nice pstext /usr/tmp/$$.labels -JM$WIDTH -K -O -P -N -R/${lon_min}/${lon_max}/${lat_min}/${lat_max} >> /usr/tmp/$$.ps");
if ($sym eq "c") {
system("cat /usr/tmp/$$.cruise |
nice psxy -JM$WIDTH -O -P -Sc0.1 -G255/0/0 -R/${lon_min}/${lon_max}/${lat_min}/${lat_max} >> /usr/tmp/$$.ps");
}
elsif ($sym eq "l") {
system("cat /usr/tmp/$$.cruise |
nice psxy -JM$WIDTH -W2/153/0/0 -O -P -R/${lon_min}/${lon_max}/${lat_min}/${lat_max} >> /usr/tmp/$$.ps");
}
elsif ($sym eq "b") {
system("cat /usr/tmp/$$.cruise |
nice psxy -JM$WIDTH -W2/153/0/0 -O -K -P -R${lon_min}/${lon_max}/${lat_min}/${lat_max} >> /usr/tmp/$$.ps");
system("cat /usr/tmp/$$.cruise |
nice psxy -JM$WIDTH -Sc0.1 -G255/0/0 -O -P -R/${lon_min}/${lon_max}/${lat_min}/${lat_max} >> /usr/tmp/$$.ps");
}
else {
}
system("nice convert -trim /usr/tmp/$$.ps /usr/tmp/$$.png");
}
sub Get_Params {
read(STDIN, $buffer, $ENV{'CONTENT_LENGTH'});
@pairs = split(/&/,$buffer);
foreach $pair (@pairs) {
($name, $value) = split(/=/, $pair);
$value =~ tr/+/ /;
$value =~ s/%([a-fA-F0-9][a-fA-f0-9])/pack("C", hex($1))/eg;
$FORM{$name} = $value;
}
}
sub minmax {
$lon_min = 1e20;
$lon_max = -1e20;
$lat_min = 1e20;
$lat_max = -1e20;
open(CRUISE_DATA,"/usr/tmp/$$.cruise");
while (