Skip to content

PerlMapScriptExamples35ex16

Thomas Bonfort edited this page Apr 6, 2012 · 2 revisions

Fetchmap.pl is CGI backend wrapper interface that uses mapscript. This example will accept the following inputs: LAT/LON, mapfile, mapext (extents), mapsize input arguments via URL and generates map images. Demonstrates mapserv error handling, establishing and overriding parameter defaults, URL/CGI inputs and showing a few old methods for different version of mapserv. This example shows some simplistic functionality of perl mapscript in a CGI setting. These wese designed to emulate a few inputs of the Tiger Map Server.

Here are a couple of URL examples:

Miami, FL - http://www.siliconmapping.com/mapper/cgi-bin/fetchmap.pl?lat=25.561&lon=-80.462&mapsize=300+210&mapext=1642750+2865665+1816633+2987024 with URL specifying lat/lon, size and extents

Default US Map - http://www.siliconmapping.com/mapper/cgi-bin/fetchmap.pl with default lat/lon, size and extents

#!perl                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                      
#!/usr/bin/perl                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                             
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                            
use CGI;   # prerequisite package/module                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                    
use mapscript;                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                              
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                            
$ENV{MS_ERRORFILE}="/var/log/mapserver";                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                    
$mapfile = $query->param('map') or $mapfile = "/home/httpd/html/mapper/tiger35.map";                                                                                                                                                                                                                                                                                                                                                                                                                                                        
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                            
# create new CGI object                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                     
$query = new CGI;                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                           
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                            
@extent = split(' ', $query->param('mapext')) or                                                                                                                                                                                                                                                                                                                                                                                                                                                                                            
        @extent = split('\,',"-3300000,2200000,3400000,7200000"); # the default                                                                                                                                                                                                                                                                                                                                                                                                                                                             
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                            
die $mapscript::ms_error->{code}.": ".$mapscript::ms_error->{message} unless $map = new mapObj($mapfile);                                                                                                                                                                                                                                                                                                                                                                                                                                   
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                            
if (defined($query->param(mapsize))) {                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                      
   ($map->{width},$map->{height}) = split(' ', $query->param('mapsize'));                                                                                                                                                                                                                                                                                                                                                                                                                                                                   
} else {                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                    
   $map->{width} = 640; $map->{height} = 480;  # default size                                                                                                                                                                                                                                                                                                                                                                                                                                                                               
}                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                           
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                            
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                            
$map->{extent}->{minx} = $extent[0];                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                        
$map->{extent}->{miny} = $extent[1];                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                        
$map->{extent}->{maxx} = $extent[2];                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                        
$map->{extent}->{maxy} = $extent[3];                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                        
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                            
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                            
$img = $map->draw() or LogDie "Unable to render map.", $mapscript::ms_error->{message};                                                                                                                                                                                                                                                                                                                                                                                                                                                     
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                            
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                            
print $query->header('image/gif');                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                          
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                            
# mapserv 3.2 method                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                        
# mapscript::msSaveImage($img, undef, 0,0);                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                 
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                            
# mapserv 3.5 method                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                        
mapscript::msSaveImage($img,undef,$mapscript::MS_GIF,$map->{transparent},$map->{interlace},undef);                                                                                                                                                                                                                                                                                                                                                                                                                                          
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                            
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                            
#  LogDie is a routine that appends a user define "MyErrorMessage"                                                                                                                                                                                                                                                                                                                                                                                                                                                                          
#  to the tail end of the mapserver log                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                     
#                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                           
sub LogDie {                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                
   $msg = shift(@_);                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                        
   open(LOG,$ENV{MS_ERRORLOG});                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                             
   print LOG $msg;                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                          
   close(LOG);                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                              
   exit;                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                    
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                            
}                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                           

back to PerlMapScrip

Clone this wiki locally