[GRASSLIST:251] Re: Grass interface to other languages

I wrote the following subroutine to configure the PERL environment to
execute Grass commands. All you'd have to do is call the subroutine with
your GISDBASE, LOCATION, and MAPSET. Then you would be able to invoke
any grass command via the system command (e.g. system "r.mapcalc
\'newgrid=sin(a)+cos(b)\'"); )

Matt

Ps. GISBASE in the subroutine is set to the location Grass is installed
on my system. You'd have to set that to wherever you've got grass
installed.

#/***************************************
# 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: owner-GRASSLIST@baylor.edu [mailto:owner-GRASSLIST@baylor.edu]

On

Behalf Of David Finlayson
Sent: Thursday, May 29, 2003 10:59 AM
To: Ganesh Kuppuswamy
Cc: GRASSLIST@baylor.edu
Subject: [GRASSLIST:248] Re: Grass interface to other languages

I don't know specifically about Perl, but in Python it is easy to call
Grass functions and capture the result with os.system(). The only

hitch

is that you need to execute the program from within a Grass shell.

David

Ganesh Kuppuswamy wrote:

>I need to use some of the grass functions(v.distance) from the

another

>program which is written in perl. Are there any perl modules already
>created for
>this. If not I will try create this, and will need some direction

from

>grass developers regarding the API into the grass library and which

lib

>to use.
>
>Thanks
>--
>Ganesh Kuppuswamy
>
>
>

Matt Doggett wrote:

I wrote the following subroutine to configure the PERL environment to
execute Grass commands. All you'd have to do is call the subroutine with
your GISDBASE, LOCATION, and MAPSET. Then you would be able to invoke
any grass command via the system command (e.g. system "r.mapcalc
\'newgrid=sin(a)+cos(b)\'"); )

    $ENV {'LOCATION_NAME'}=$LOCATION_NAME;
    $ENV {'MAPSET'} = $MAPSET;
    $ENV {'LOCATION'} = $LOCATION;

FWIW, there is no need to do this for 5.0.0 and later. These settings
are always read from $GISRC, not the environment.

--
Glynn Clements <glynn.clements@virgin.net>

In order to run GRASS commands from a script, the GRASS shell must be running.
If it is impossible, eg. you want to run GRASS commands from a PHP script,
I'll suggest you to download the script 'batch-grass.sh' (Google will find it for you).
Maybe the script will need some suble changes to accomodate it to your needs (eg. change the way it treats its parameters), but for me, it works fine.

Peter