Changes between Initial Version and Version 1 of PerlMapScriptExamples35ex20


Ignore:
Timestamp:
Jan 29, 2009, 7:15:07 AM (13 years ago)
Author:
jmckenna
Comment:

--

Legend:

Unmodified
Added
Removed
Modified
  • PerlMapScriptExamples35ex20

    v1 v1  
     1= lvprint.pl =
     2{{{
     3#!perl
     4#!/usr/bin/perl
     5
     6use pdflib_pl;
     7use CGI qw(:standard :html);
     8use mapscript36;
     9
     10$page_resolution = 72; # dpi
     11$page_width = 11; # inches
     12$page_height = 8.5;
     13$page_width_pixels = $page_width*$page_resolution;
     14$page_height_pixels =$page_height*$page_resolution;
     15
     16$map_resolution = 144; #dpi
     17$map_width = 10; # inches
     18$map_height = 7.5;
     19$map_width_pixels = $map_width*$map_resolution;
     20$map_height_pixels =$map_height*$map_resolution;
     21$map_scaling = $page_resolution/$map_resolution;
     22
     23sub error() {
     24  my($err) = @_;
     25
     26  die $err; 
     27}
     28
     29$cgi = new CGI();
     30
     31print header(-type=>'application/pdf');
     32
     33$pdf = PDF_new();
     34
     35&error("Unable to open output PDF file.") if (PDF_open_file($pdf, "-") == -1);
     36
     37# PDF Metadata
     38PDF_set_info($pdf, 'Creator', 'lvprint.pl');
     39PDF_set_info($pdf, 'Author', 'Minnesota Department of Natural Resources: LandView');
     40
     41#
     42# Build the map
     43#
     44&error("No mapfile specified.") unless $cgi->param('map');
     45$mapfile =  $cgi->param('map');
     46$mapfile = $ENV{$cgi->param('map')} if $ENV{$cgi->param('map')};
     47$map = new mapscript36::mapObj($mapfile) or &error("Unable to open ". $cgi->param('map') .".");
     48
     49# Step 0: override any necessary settings
     50$map->{width} = $map_width_pixels;
     51$map->{height} = $map_height_pixels;
     52$map->{scalebar}->{status} = $mapscript36::MS_OFF;
     53
     54# Step 1: set a new extent
     55($map->{extent}->{minx}, $map->{extent}->{miny}, $map->{extent}->{maxx}, $map->{extent}->{maxy}) = split(/ /, $cgi->param(mapext)) if $cgi->param(mapext);
     56
     57# Step 2: turn the write layers on
     58for(my $i=0; $i<$map->{numlayers}; $i++) {
     59  $layer = $map->getLayer($i);
     60
     61  if($layer->{status} == $mapscript36::MS_DEFAULT) {
     62    next;
     63  }
     64
     65  $layer->{status} = $mapscript36::MS_OFF;
     66
     67  foreach (split(/ /, $cgi->param(layers))) {
     68    if(($layer->{name} eq $_) or ($layer->{group} eq $_)) {
     69      $layer->{status} = $mapscript36::MS_ON;
     70    }
     71  }
     72}
     73
     74# Step 3: draw the map
     75$img = $map->draw();
     76
     77# Step 4: save the image to a temporary file
     78$imgfile = $map->{web}->{imagepath} . $$ . time() . ".gif";
     79$img->saveImage($imgfile, $mapscript36::MS_PNG, $mapscript36::MS_FALSE, $mapscript36::MS_FALSE, -1);
     80
     81# clean up
     82$img->free();
     83undef $map;
     84#
     85# End map building
     86#
     87
     88#
     89# Build the PDF file
     90#
     91$top_margin = $page_resolution*.5; # 1/2"
     92$bottom_margin = $left_margin = $right_margin = $page_resolution/2.0; # 1/2"
     93
     94PDF_begin_page($pdf, $page_width_pixels, $page_height_pixels);
     95
     96# PDF_set_parameter($pdf, "imagewarning", "true");
     97
     98# Open and place the map
     99&error("Could not open image file ($imgfile).") if (($png = PDF_open_image_file($pdf, 'png', $imgfile, '', 0)) == -1);
     100$x = $left_margin+1;
     101$y = $page_height_pixels - $map_height_pixels*$map_scaling - $top_margin - 1;
     102PDF_place_image($pdf, $png, $x, $y, $map_scaling);
     103PDF_close_image($pdf, $png);
     104
     105# Neatline
     106
     107PDF_setrgbcolor_stroke($pdf, .267, .4, .2); # 446633 => 68 102 51
     108$x = $left_margin + 1;
     109$y = $page_height_pixels - $map_height_pixels*$map_scaling - $top_margin - 1;
     110$w = $map_width_pixels*$map_scaling;
     111$h = $map_height_pixels*$map_scaling;
     112PDF_rect($pdf, $x, $y, $w, $h);
     113PDF_stroke($pdf);
     114
     115PDF_end_page($pdf);
     116
     117PDF_close($pdf);
     118PDF_delete($pdf);
     119
     120unlink $imgfile;
     121}}}
     122----
     123back to PerlMapScript