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

Matt Doggett mdoggett at coas.oregonstate.edu
Thu May 29 20:19:24 EDT 2003


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 at baylor.edu [mailto:owner-GRASSLIST at baylor.edu]
On
> Behalf Of David Finlayson
> Sent: Thursday, May 29, 2003 10:59 AM
> To: Ganesh Kuppuswamy
> Cc: GRASSLIST at 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
> >
> >
> >




More information about the grass-user mailing list