[Mapserver-users] grass from mapscript with system()-call?

Matt Doggett mdoggett at coas.oregonstate.edu
Tue May 27 15:28:27 EDT 2003


I'm doing exactly what you are after and have succeeded (finally!).
However, I think my method is a little different. What I do (from my
mapserver app) is call a CGI script which (after doing some other stuff)
calls a couple of Grass scripts that do the calculations.  I have a
subroutine (Grass_Init) which initializes the Grass environment to the
specified database, location, and mapset. At some point in the process,
this subroutine is called, after which the script can run any grass
command simply by running a system command. E.g. (in Perl):
System "r.mapcalc \'newgrid=sin(A)+cos(B)\'";
If you can't get anywhere with your method, maybe this will help.

-Matt



#/***************************************
# Usage:  Grass_Init( Grass_Database, Location, Mapset);
# Sets up a GRASS environment and creates a GISRC file.
# Returns the name of the GISRC file created.
sub Grass_Init {
    my ($GISDBASE,$LOCATION_NAME,$MAPSET) = @_;
		   die "Usage: Grass_Init( Grass_Database, Location,
Mapset)!"  if (@_<3);
			         
    my $GISBASE="/usr/local/grass5";
    my $GISRC = "/tmp/.grassrc.$$";
    my $ETC = "$GISBASE/etc";
    my $PATH ="$GISBASE/bin:$GISBASE/scripts:$GISBASE/garden/bin";
    my $LOCATION = "$GISDBASE/$LOCATION_NAME/$MAPSET";
    
    # Setup environment variables GRASS requires
    $ENV {'GISBASE'}= $GISBASE;
    $ENV {'GISRC'} = $GISRC;
    $ENV {'ETC'}= $ETC;
    $ENV {'PATH'}= "$PATH:$ENV{'PATH'}" if (index($ENV{'PATH'},$GISBASE)
< 0); 
    $ENV {'LOCATION_NAME'}=$LOCATION_NAME;
    $ENV {'MAPSET'} = $MAPSET;
    $ENV {'LOCATION'} = $LOCATION;
    
    # Create .grassrc file #
	  open (RC,">$GISRC")
		  or die "Error opening $GISRC! $!";
    print RC <<EOF;
GISDBASE: $GISDBASE
LOCATION_NAME: $LOCATION_NAME
MAPSET: $MAPSET
GISLIB: $GISBASE/src/libes/gis
VECT_INCLUDE: $GISBASE/src/mapdev/Vlib
XDRIVER_HEIGHT: 480
XDRIVER_WIDTH:  640
EOF
    
    close RC;
		print "GRASS initialized using $GISRC.\n";
		return $GISRC;
}#end Grass_Init



> -----Original Message-----
> From: mapserver-users-admin at lists.gis.umn.edu [mailto:mapserver-users-
> admin at lists.gis.umn.edu] On Behalf Of Stephan Holl
> Sent: Saturday, May 24, 2003 3:54 AM
> To: mapserver-users
> Subject: [Mapserver-users] grass from mapscript with system()-call?
> 
> Dear list,
> 
> I am struggling around  with starting a non-interactive session of
> grass, which should produce some raster-maps.
> these maps should then be added to the running mapserver.
> The Idea is to perform some calculations with r.mapcalc directly in
the
> browser.
> whenever I start grass with
> 
> system("SHELL=./grass_commands.sh ; /path/where/grass/lives/grass5
> testlocation/PERMANENT ; SHELL=/bin/sh");
> 
> the output says, that grass is already in use from the apache-user....
> does anybody how to solve this?
> 
> 
> Thank you.
> 
> cheers
>  Stephan Holl
> --
> Stephan Holl
> 
> GnuPG Key-ID: 11946A09




More information about the mapserver-users mailing list