128 changes: 58 additions & 70 deletions python/plugins/sextante/grass/GrassUtils.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,7 @@
import plugin_installer

class GrassUtils:

GRASS_LATLON = "GRASS_LATLON"
#GRASS_AUTO_REGION = "GRASS_AUTO_REGION"

GRASS_REGION_XMIN = "GRASS_REGION_XMIN"
GRASS_REGION_YMIN = "GRASS_REGION_YMIN"
GRASS_REGION_XMAX = "GRASS_REGION_XMAX"
Expand All @@ -50,6 +48,8 @@ class GrassUtils:

sessionRunning = False
sessionLayers = {}
projectionSet = False


@staticmethod
def grassBatchJobFilename():
Expand Down Expand Up @@ -129,12 +129,11 @@ def createGrassScript(commands):
#temporary gisrc file
output = open(gisrc, "w")
location = "temp_location"
mapset = "user"
gisdbase = GrassUtils.grassDataFolder()
#gisdbase = os.path.join(os.path.expanduser("~"), "sextante", "tempdata", "grassdata")
output.write("GISDBASE: " + gisdbase + "\n");
output.write("LOCATION_NAME: " + location + "\n");
output.write("MAPSET: " + mapset + "\n");
output.write("MAPSET: PERMAMENT \n");
output.write("GRASS_GUI: text\n");
output.close()

Expand All @@ -159,7 +158,7 @@ def createGrassScript(commands):
output.write("set PATHEXT=%PATHEXT%;.PY\n")
output.write("set PYTHONPATH=%PYTHONPATH%;%WINGISBASE%\\etc\\python;%WINGISBASE%\\etc\\wxpython\\n")
output.write("\n")
output.write("g.gisenv.exe set=\"MAPSET=" + mapset + "\"\n")
output.write("g.gisenv.exe set=\"MAPSET=PERMANENT\"\n")
output.write("g.gisenv.exe set=\"LOCATION=" + location + "\"\n")
output.write("g.gisenv.exe set=\"LOCATION_NAME=" + location + "\"\n")
output.write("g.gisenv.exe set=\"GISDBASE=" + gisdbase + "\"\n")
Expand Down Expand Up @@ -194,88 +193,50 @@ def grassDataFolder():
@staticmethod
def createTempMapset():
'''Creates a temporary location and mapset(s) for GRASS data processing. A minimal set of folders and files is created in the
system's default temporary directory. The settings files are written with sane defaults, so GRASS can do its work. File
structure and content will vary slightly depending on whether the user wants to process lat/lon or x/y data.'''
system's default temporary directory. The settings files are written with sane defaults, so GRASS can do its work. The mapset
projection will be set later, based on the projection of the first input image or vector'''

latlon = SextanteConfig.getSetting(GrassUtils.GRASS_LATLON)
folder = GrassUtils.grassMapsetFolder()
mkdir(os.path.join(folder, "PERMANENT"))
mkdir(os.path.join(folder, "user"))
mkdir(os.path.join(folder, "PERMANENT", ".tmp"))
GrassUtils.writeGrassWindow(os.path.join(folder, "PERMANENT", "DEFAULT_WIND"));
outfile = open(os.path.join(folder, "PERMANENT", "MYNAME"), "w")
if not latlon:
outfile.write("SEXTANTE GRASS interface: temporary x/y data processing location.\n");
else:
outfile.write("SEXTANTE GRASS interface: temporary lat/lon data processing location.\n")
outfile.write("SEXTANTE GRASS interface: temporary data processing location.\n");
outfile.close();
if latlon:
outfile = open(os.path.join(folder, "PERMANENT", "PROJ_INFO"), "w")
outfile.write("name: Latitude-Longitude\n")
outfile.write("proj: ll\n")
outfile.write("ellps: wgs84\n")
outfile.close()
outfile = open(os.path.join(folder, "PERMANENT", "PROJ_UNITS"), "w")
outfile.write("unit: degree\n")
outfile.write("units: degrees\n")
outfile.write("meters: 1.0\n")
outfile.close();
GrassUtils.writeGrassWindow(os.path.join(folder, "PERMANENT", "WIND"))
mkdir(os.path.join(folder, "user", "dbf"))
mkdir(os.path.join(folder, "user", ".tmp"))
outfile = open(os.path.join(folder, "user", "VAR"), "w")
mkdir(os.path.join(folder, "PERMANENT", "dbf"))
outfile = open(os.path.join(folder, "PERMANENT", "VAR"), "w")
outfile.write("DB_DRIVER: dbf\n")
outfile.write("DB_DATABASE: $GISDBASE/$LOCATION_NAME/$MAPSET/dbf/\n")
outfile.close()
GrassUtils.writeGrassWindow(os.path.join(folder, "user", "WIND"))

@staticmethod
def writeGrassWindow(filename):
out = open(filename, "w")
latlon = SextanteConfig.getSetting(GrassUtils.GRASS_LATLON)
if not latlon:
out.write("proj: 0\n")
out.write("zone: 0\n")
out.write("north: 1\n")
out.write("south: 0\n")
out.write("east: 1\n")
out.write("west: 0\n")
out.write("cols: 1\n")
out.write("rows: 1\n")
out.write("e-w resol: 1\n")
out.write("n-s resol: 1\n")
out.write("top: 1\n")
out.write("bottom: 0\n")
out.write("cols3: 1\n")
out.write("rows3: 1\n")
out.write("depths: 1\n")
out.write("e-w resol3: 1\n")
out.write("n-s resol3: 1\n")
out.write("t-b resol: 1\n")
else:
out.write("proj: 3\n")
out.write("zone: 0\n")
out.write("north: 1N\n")
out.write("south: 0\n")
out.write("east: 1E\n")
out.write("west: 0\n")
out.write("cols: 1\n")
out.write("rows: 1\n")
out.write("e-w resol: 1\n")
out.write("n-s resol: 1\n")
out.write("top: 1\n")
out.write("bottom: 0\n")
out.write("cols3: 1\n")
out.write("rows3: 1\n")
out.write("depths: 1\n")
out.write("e-w resol3: 1\n")
out.write("n-s resol3: 1\n")
out.write("t-b resol: 1\n")
out.write("proj: 0\n")
out.write("zone: 0\n")
out.write("north: 1\n")
out.write("south: 0\n")
out.write("east: 1\n")
out.write("west: 0\n")
out.write("cols: 1\n")
out.write("rows: 1\n")
out.write("e-w resol: 1\n")
out.write("n-s resol: 1\n")
out.write("top: 1\n")
out.write("bottom: 0\n")
out.write("cols3: 1\n")
out.write("rows3: 1\n")
out.write("depths: 1\n")
out.write("e-w resol3: 1\n")
out.write("n-s resol3: 1\n")
out.write("t-b resol: 1\n")

out.close()


@staticmethod
def executeGrass(commands, progress):
def prepareGrassExecution(commands):
if SextanteUtils.isWindows():
GrassUtils.createGrassScript(commands)
command = ["cmd.exe", "/C ", GrassUtils.grassScriptFilename()]
Expand All @@ -290,8 +251,15 @@ def executeGrass(commands, progress):
command = GrassUtils.grassPath() + os.sep + "grass.sh " + GrassUtils.grassMapsetFolder() + "/user"
else:
command = "grass64 " + GrassUtils.grassMapsetFolder() + "/user"

return command

@staticmethod
def executeGrass(commands, progress, outputCommands ):
loglines = []
loglines.append("GRASS execution console output")
grassOutDone = False
command = GrassUtils.prepareGrassExecution(commands)
proc = subprocess.Popen(command, shell=True, stdout=subprocess.PIPE, stdin=subprocess.PIPE,stderr=subprocess.STDOUT, universal_newlines=True).stdout
for line in iter(proc.readline, ""):
if "GRASS_INFO_PERCENT" in line:
Expand All @@ -300,10 +268,28 @@ def executeGrass(commands, progress):
except:
pass
else:
if "r.out" in line or "v.out" in line:
grassOutDone = True
loglines.append(line)
progress.setConsoleInfo(line)
# Some GRASS scripts, like r.mapcalculator or r.fillnulls, call other GRASS scripts during execution. This may override any commands that are
# still to be executed by the subprocess, which are usually the output ones. If that is the case runs the output commands again.
if not grassOutDone and outputCommands:
command = GrassUtils.prepareGrassExecution(outputCommands)
proc = subprocess.Popen(command, shell=True, stdout=subprocess.PIPE, stdin=subprocess.PIPE,stderr=subprocess.STDOUT, universal_newlines=True).stdout
for line in iter(proc.readline, ""):
if "GRASS_INFO_PERCENT" in line:
try:
progress.setPercentage(int(line[len("GRASS_INFO_PERCENT")+ 2:]))
except:
pass
else:
loglines.append(line)
progress.setConsoleInfo(line)

if SextanteConfig.getSetting(GrassUtils.GRASS_LOG_CONSOLE):
SextanteLog.addToLog(SextanteLog.LOG_INFO, loglines)
return loglines;

@staticmethod
def getGrassVersion():
Expand All @@ -328,14 +314,16 @@ def endGrassSession():
shutil.rmtree(GrassUtils.grassMapsetFolder(), True)
GrassUtils.sessionRunning = False
GrassUtils.sessionLayers = {}
GrassUtils.projectionSet = False

@staticmethod
def getSessionLayers():
return GrassUtils.sessionLayers

@staticmethod
def addSessionLayers(exportedLayers):
GrassUtils.sessionLayers = dict(GrassUtils.sessionLayers.items() + exportedLayers.items())
GrassUtils.sessionLayers = dict(GrassUtils.sessionLayers.items() + exportedLayers.items())




Expand Down
1 change: 1 addition & 0 deletions python/plugins/sextante/grass/description/r.fillnulls.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ r.fillnulls
r.fillnulls - Fills no-data areas in raster maps using v.surf.rst splines interpolation or v.surf.bspline interpolation
Raster (r.*)
ParameterRaster|input|Name of input raster map in which to fill nulls|False
ParameterSelection|method|Method|bilinear;bicubic;rst|2
ParameterNumber|tension|Spline tension parameter|None|None|40.0
ParameterNumber|smooth|Spline smoothing parameter|None|None|0.1
OutputRaster|output|Name for output raster map with nulls filled by interpolation
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ Join Attributes from a Table (Shapes)
table_tools
ParameterVector|TABLE_A|Shapes|-1|False
ParameterTable|TABLE_B|Join Table|False
ParameterTableField|ID_A|Identifier|TABLE_B
ParameterTableField|ID_B|Identifier|TABLE_B
ParameterTableField|ID_A|Attribute (Table)|TABLE_A
ParameterTableField|ID_B|Attribute (Join Table)|TABLE_B
ParameterBoolean|KEEPALL |Keep All|True
OutputVector|RESULT|Result
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ Join Attributes from a Table
table_tools
ParameterTable|TABLE_A|Table|False
ParameterTable|TABLE_B|Join Table|False
ParameterTableField|ID_A|Identifier|TABLE_B
ParameterTableField|ID_B|Identifier|TABLE_B
ParameterTableField|ID_A|Attribute (Table)|TABLE_A
ParameterTableField|ID_B|Attribute (Join Table)|TABLE_B
ParameterBoolean|KEEPALL |Keep All|True
OutputTable|RESULT|Result