Alright, The JGrass console engine can be used without JGrass. All you need are the two console plugins from the JGrass package:
- eu.hydrologis.jgrass.console
- eu.hydrologis.jgrass.console.editor
At that point lets code:
/*
* prepare to use the console environment
*/
PipedOutputStream out = new PipedOutputStream();
PrintStream pStream = new PrintStream(out);
JGrass console = ConsolePlugin.console();
if (null == console) {
// throw something
}
Set some options to the engine:
ProjectOptions projectOptions = new ProjectOptions(pStream, pStream, pStream);
PreferencesInitializer.initializeStartupProjectOptions(projectOptions);
projectOptions.setOption(ProjectOptions.COMMON_GRASS_DATABASE, new File(
locationPath).getParent());
projectOptions.setOption(ProjectOptions.COMMON_GRASS_LOCATION, new File(
locationPath).getName());
projectOptions.setOption(ProjectOptions.COMMON_GRASS_MAPSET, mapsetName);
projectOptions.setOption(ProjectOptions.CONSOLE_COMPILE_ONLY, new Boolean(false));
projectOptions.setOption(ProjectOptions.CONSOLE_ASYNC_MODE, new Boolean(false));
URL rtUrl = Platform.getBundle(ConsolePlugin.PLUGIN_ID).getResource("rt"); //$NON-NLS-1$
String rtPath = null;
try {
rtPath = FileLocator.toFileURL(rtUrl).getPath();
} catch (IOException e) {
HydrocarePlugin.log("HydrocarePlugin problem", e); //$NON-NLS-1$
e.printStackTrace();
return;
}
projectOptions.setOption(ProjectOptions.CONSOLE_DIRECTORY_INCLUDE,
new String[]{rtPath});
Note that the FileLocator.toFileURL stuff is needed only if inside an RCP application.
Now create the command you want to execute:
String command = "g.region -p";
And execute it!
console.dispatch(projectOptions, command);
UPDATE
_________
I just created a class to use the console in standalone mode:
/**
* Standalone console engine
*
* @author Andrea Antonello - www.hydrologis.com
*/
public class JGrassConsole {
private final String grassDbPath;
private final String locationName;
private final String mapsetName;
private final String rtPath;
private boolean doDebug = false;
private String gisbase = "/"; //$NON-NLS-1$
public JGrassConsole(String grassDbPath, String locationName,
String mapsetName, String rtPath, String cmd, boolean debug,
String grass) {
this.grassDbPath = grassDbPath;
this.locationName = locationName;
this.mapsetName = mapsetName;
this.rtPath = rtPath;
this.doDebug = debug;
if (grass != null) {
gisbase = grass;
}
/*
* prepare to use the console environment
*/
PrintStream outStream = new PrintStream(System.out);
PrintStream errStream = new PrintStream(System.err);
JGrass console = (JGrass) new ConsoleEngine();
if (null == console) {
// throw something
}
ProjectOptions projectOptions = new ProjectOptions(outStream,
outStream, errStream);
initialize(projectOptions);
console.dispatch(projectOptions, cmd);
}
/**
* @param args
*/
@SuppressWarnings("nls")
public static void main(String[] args) {
String grassDbPath = null;
String locationName = null;
String mapsetName = null;
String rtPath = null;
String cmd = null;
String grass = null;
boolean debug = false;
for (String arg : args) {
System.out.println(arg);
if (arg.startsWith("-db")) {
grassDbPath = arg.split("=")[1];
}
if (arg.startsWith("-loc")) {
locationName = arg.split("=")[1];
}
if (arg.startsWith("-maps")) {
mapsetName = arg.split("=")[1];
}
if (arg.startsWith("-rt")) {
rtPath = arg.split("=")[1];
}
if (arg.startsWith("-cmd")) {
cmd = arg.split("=")[1];
}
if (arg.startsWith("-deb")) {
debug = true;
}
if (arg.startsWith("-grass")) {
grass = arg.split("=")[1];
}
}
if (grassDbPath == null || locationName == null || mapsetName == null
|| rtPath == null || cmd == null) {
System.out
.println("USAGE: -db=-loc= -maps= -rt=








