GRASS and Java: Difference between revisions

From GRASS-Wiki
Jump to navigation Jump to search
No edit summary
m (Reverted edits by Reverse22 (talk) to last revision by Neteler)
 
(5 intermediate revisions by 3 users not shown)
Line 1: Line 1:
''ADD MORE PLEASE''
=== vtk-grass-bridge with JAVA bindings ===
 
The vtk-grass-bridge main page
http://code.google.com/p/vtk-grass-bridge/
 
Short introduction how to compile the vtk-grass-bridge:
http://code.google.com/p/vtk-grass-bridge/wiki/HowToCompile
 
==== Examples ====
The Java module v_sample_rast:
http://code.google.com/p/vtk-grass-bridge/source/browse/trunk/Modules/Java/v_sample_rast.java
 
 
=== Calling GRASS from JAVA  ===


Define:
Define:
Line 15: Line 28:
==== Examples ====
==== Examples ====


It seems that calling grass in java by using java.Lang.Runtime.exec() can be implemented easily in Linux platform(Tests in Ubuntu 9.10 passed yet).
===== Installation =====
It seems that calling grass in JAVA by using <tt>java.Lang.Runtime.exec()</tt> can be implemented easily in Linux platform (tested successfully on Ubuntu 9.10).


First install the grass use the apt-get:
First install the grass use the apt-get:
sudo apt-get install grass  //it will install the latest stable version
 
  sudo apt-get install grass  # it will install the latest stable version
 
After the installation completed you can:
After the installation completed you can:
1)Test it in the terminal without java(make sure that you have the GisDataBase installed,,):
open a terminal and enter:
grass or grass64 or grass63 -text, the following the instruction in the screen.


2)Test using grass command directly in the terminal:
  #Test it in the terminal without JAVA (make sure that you have a GRASS database installed):
#set the environment // the grass is installed under /usr/lib/grass64.
 
Open a terminal and enter:
 
  grass64
 
The following the instruction in the screen or check [http://trac.osgeo.org/grass/wiki/HowToTestGrass6 here].
 
===== Test the environment settings =====
 
Test using GRASS command directly in the terminal:
 
Set the environment (we assume GRASS being installed under /usr/lib/grass64):
 
<source lang="bash">
export GISBASE=/usr/lib/grass64
export GISBASE=/usr/lib/grass64
export GISRC=/home/kk/gisrc
export GISRC=/home/kk/gisrc
export ldLibraryPath=$GISBASE/lib
export LD_LIBRARY_PATH=$GISBASE/lib
export PATH=$PATH:$GISBASE/bin:$GISBASE/scripts
export PATH=$PATH:$GISBASE/bin:$GISBASE/scripts
</source>


Then try the grass command directly, for example:
Then try some GRASS command directly, for example:
<source lang="bash">
g.version
g.version
g.gisenv
g.gisenv
........
...
</source>


Now call GRASS in JAVA if the above test 2 passed.


3)Easily call grass in java if the test 2 passed.
Just use the <tt>Runtime.getRuntime.exec(String cmd,String[] env)</tt>:
Just use the Runtime.getRuntime.exec(String cmd,String[] env):
<source lang="java">
String cmd="g.version";
String cmd="g.version";
String[] exe = { "bash", "-c", cmd };
String[] exe = { "bash", "-c", cmd };
String[] env={"GISBASE=..","GISRC="".......}; the environment variables which you used in the  
String[] env={"GISBASE=..","GISRC="".......}; // the environment variables which you set in the step 2.
Process p=Runtime.getRuntime.exec(exe,env);
p=Runtime.getRuntime.exec(exe,env);
</source>


//FIXME for the format.
TODO


[[Category:Development]]
[[Category:Development]]
[[Category:Linking to other languages]]
[[Category:Linking to other languages]]
[[Category:Java]]
[[Category:Java]]

Latest revision as of 21:36, 30 December 2011

vtk-grass-bridge with JAVA bindings

The vtk-grass-bridge main page http://code.google.com/p/vtk-grass-bridge/

Short introduction how to compile the vtk-grass-bridge: http://code.google.com/p/vtk-grass-bridge/wiki/HowToCompile

Examples

The Java module v_sample_rast: http://code.google.com/p/vtk-grass-bridge/source/browse/trunk/Modules/Java/v_sample_rast.java


Calling GRASS from JAVA

Define:

       public String run(String[] cmd)  {
       ...
                       Process p1=rt.exec(cmd, env);

and use:

       gm.run({"r.info", "map=roads"});

Examples

Installation

It seems that calling grass in JAVA by using java.Lang.Runtime.exec() can be implemented easily in Linux platform (tested successfully on Ubuntu 9.10).

First install the grass use the apt-get:

 sudo apt-get install grass  # it will install the latest stable version

After the installation completed you can:

 #Test it in the terminal without JAVA (make sure that you have a GRASS database installed):

Open a terminal and enter:

 grass64

The following the instruction in the screen or check here.

Test the environment settings

Test using GRASS command directly in the terminal:

Set the environment (we assume GRASS being installed under /usr/lib/grass64):

export GISBASE=/usr/lib/grass64
export GISRC=/home/kk/gisrc
export LD_LIBRARY_PATH=$GISBASE/lib
export PATH=$PATH:$GISBASE/bin:$GISBASE/scripts

Then try some GRASS command directly, for example:

g.version
g.gisenv
...

Now call GRASS in JAVA if the above test 2 passed.

Just use the Runtime.getRuntime.exec(String cmd,String[] env):

String cmd="g.version";

String[] exe = { "bash", "-c", cmd };
String[] env={"GISBASE=..","GISRC="".......};  // the environment variables which you set in the step 2.
p=Runtime.getRuntime.exec(exe,env);