Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Improvements in R Connection
git-svn-id: http://sextante.googlecode.com/svn/trunk/soft/bindings/qgis-plugin@30 881b9c09-3ef8-f3c2-ec3d-21d735c97f4d
  • Loading branch information
volayaf@gmail.com committed Mar 7, 2012
1 parent fd72ae8 commit 1b68f20
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 21 deletions.
39 changes: 23 additions & 16 deletions src/sextante/r/RAlgorithm.py
Expand Up @@ -17,8 +17,6 @@
from sextante.outputs.OutputHTML import OutputHTML
from sextante.r.RUtils import RUtils
from sextante.core.GeoAlgorithmExecutionException import GeoAlgorithmExecutionException
from sextante.core.SextanteUtils import SextanteUtils
import time

class RAlgorithm(GeoAlgorithm):

Expand Down Expand Up @@ -132,8 +130,17 @@ def processParameterLine(self,line):
raise WrongScriptException("Could not load R script:" + self.descriptionFile + ".\n Problem with line \"" + line + "\"")

def processAlgorithm(self, progress):
RUtils.ExecuteRAlgorithm(self)

RUtils.executeRAlgorithm(self)
if self.showPlots:
htmlfilename = self.getOutputValue(RAlgorithm.RPLOTS)
f = open(htmlfilename, "w")
f.write("<img src=\"" + self.plotsFilename + "/>")
f.close()
if self.showConsoleOutput:
htmlfilename = self.getOutputValue(RAlgorithm.R_CONSOLE_OUTPUT)
f = open(htmlfilename)
f.write(RUtils.getConsoleOutput())
f.close()

def getFullSetOfRCommands(self):

Expand All @@ -148,7 +155,7 @@ def getExportCommands(self):

commands = []

for out in self.output:
for out in self.outputs:
if isinstance(out, OutputRaster):
value = out.value
if not value.endswith("tif"):
Expand All @@ -162,10 +169,10 @@ def getExportCommands(self):
value = value.replace("\\", "/")
filename = os.path.basename(value)
filename = filename[-4]
commands.add("writeOGR(" + out.name + ",\"" + value + "\",\""
commands.append("writeOGR(" + out.name + ",\"" + value + "\",\""
+ filename + "\", driver=\"ESRI Shapefile\")");

if self.showPlotOutput:
if self.showPlots:
commands.append("dev.off()");

return commands
Expand All @@ -180,18 +187,18 @@ def getImportCommands(self):
for param in self.parameters:
if isinstance(param, ParameterRaster):
value = param.value
if not value.lower.endswith("asc") and not value.lower.endswith("tif"):
if not value.lower().endswith("asc") and not value.lower().endswith("tif"):
raise GeoAlgorithmExecutionException("Unsupported input file format.\n" + value)
value = value.replace("\\", "/")
commands.append(param.name + " = " + "readGDAL(\"" + value + "\"")
if isinstance(param, ParameterVector):
value = param.value
if not value.lower.endswith("shp"):
if not value.lower().endswith("shp"):
raise GeoAlgorithmExecutionException("Unsupported input file format.\n" + value)
value = value.replace("\\", "/")
filename = os.path.basename(value)
filename = filename[-4]
commands.append(param.getParameterName() + " = " + "readOGR(\"" + value + "\",layer=\"" + filename + "\")")
commands.append(param.name + " = " + "readOGR(\"" + value + "\",layer=\"" + filename + "\")")
if isinstance(param, (ParameterTableField, ParameterString)):
commands.append(param.name + "=\"" + param.value + "\"")
if isinstance(param, ParameterNumber):
Expand All @@ -207,14 +214,14 @@ def getImportCommands(self):
iLayer = 0;
if param.datatype == ParameterMultipleInput.TYPE_RASTER:
for layer in layers:
if not layer.lower.endswith("asc") and not layer.lower.endswith("tif"):
if not layer.lower().endswith("asc") and not layer.lower().endswith("tif"):
raise GeoAlgorithmExecutionException("Unsupported input file format.\n" + layer)
layer = layer.replace("\\", "/")
commands.append("tempvar" + str(iLayer)+ " = " + "readGDAL(\"" + layer + "\"")
iLayer+=1
else:
for layer in layers:
if not layer.lower.endswith("shp"):
if not layer.lower().endswith("shp"):
raise GeoAlgorithmExecutionException("Unsupported input file format.\n" + layer)
layer = layer.replace("\\", "/")
filename = os.path.basename(layer)
Expand All @@ -231,18 +238,18 @@ def getImportCommands(self):
s += "tempvar" + str(iLayer)
iLayer += 1
s+=")\n"
commands.append(s)

if self.showPlots:
seconds = str(time.time())
self.plotsFilename = SextanteUtils.tempFolder() + os.sep + seconds + ".png"
htmlfilename = self.getOutputValue(RAlgorithm.RPLOTS)
self.plotsFilename = htmlfilename +".png"
self.plotsFilename = self.plotsFilename.replace("\\", "/");
commands.add("png(\"" + self.plotsFilename + "\")");
commands.append("png(\"" + self.plotsFilename + "\")");

return commands


def getRCommands(self):

return self.commands


5 changes: 2 additions & 3 deletions src/sextante/r/RUtils.py
Expand Up @@ -27,9 +27,9 @@ def RScriptsFolder():

@staticmethod
def createRScriptFromRCommands(commands):
scriptfile = open(RUtils.getRScriptFilename())
scriptfile = open(RUtils.getRScriptFilename(), "w")
for command in commands:
scriptfile.write(command + "\n");
scriptfile.write(command + "\n")
scriptfile.close()


Expand All @@ -47,7 +47,6 @@ def getConsoleOutputFilename():
def executeRAlgorithm(alg):
RUtils.consoleResults = []
RUtils.verboseCommands = alg.getVerboseCommands();
RUtils.addConsoleOutput = False;
RUtils.createRScriptFromRCommands(alg.getFullSetOfRCommands())
if SextanteUtils.isWindows():
command = ["\"" + RUtils.RFolder() + os.sep + "bin" + os.sep + "R.exe\"", "CMD", "BATCH", "--vanilla",
Expand Down
4 changes: 2 additions & 2 deletions src/sextante/saga/SagaAlgorithm.py
Expand Up @@ -314,8 +314,8 @@ def resampleRasterLayer(self,layer):
ymin = auto = SextanteConfig.getSetting(SagaUtils.SAGA_RESAMPLING_REGION_YMIN)
ymax = auto = SextanteConfig.getSetting(SagaUtils.SAGA_RESAMPLING_REGION_YMAX)
cellsize = auto = SextanteConfig.getSetting(SagaUtils.SAGA_RESAMPLING_REGION_CELLSIZE)
s = "grid_tools \"Resampling\" -INPUT " + inputFilename + "-TARGET 0 -SCALE_UP_METHOD 4 -SCALE_DOWN_METHOD 4 -USER_XMIN " +
xmin + " -USER_XMAX " + xmax + " -USER_YMIN " + ymin + " -USER_YMAX " + ymax +
s = "grid_tools \"Resampling\" -INPUT " + inputFilename + "-TARGET 0 -SCALE_UP_METHOD 4 -SCALE_DOWN_METHOD 4 -USER_XMIN " +\
xmin + " -USER_XMAX " + xmax + " -USER_YMIN " + ymin + " -USER_YMAX " + ymax +\
" -USER_SIZE " + str(cellsize) + " -USER_GRID " + destFilename

def exportRasterLayer(self,layer):
Expand Down

0 comments on commit 1b68f20

Please sign in to comment.