Skip to content
Permalink
Browse files
better raster support in R/SEXTANTE
  • Loading branch information
volaya committed Feb 7, 2013
1 parent c2b8ae0 commit 28e594bed363615a8ed0ea9dc43e6b5d57bd7260
Showing with 41 additions and 5 deletions.
  1. +34 −5 python/plugins/sextante/r/RAlgorithm.py
  2. +7 −0 python/plugins/sextante/r/scripts/Advanced_Raster_histogram.rsx
@@ -134,15 +134,31 @@ def processParameterLine(self,line):
if tokens[1].lower().strip() == "group":
self.group = tokens[0]
return
if tokens[1].lower().strip() == "raster":
if tokens[1].lower().strip().startswith("raster"):
param = ParameterRaster(tokens[0], desc, False)
try:
lib = tokens[1].strip()[len("raster")+1:]
if lib == "R.raster":
self.useRraster = True
else:
self.useRraster = False
except:
self.useRraster = False
elif tokens[1].lower().strip() == "vector":
param = ParameterVector(tokens[0], desc,ParameterVector.VECTOR_TYPE_ANY)
elif tokens[1].lower().strip() == "table":
param = ParameterTable(tokens[0], desc, False)
elif tokens[1].lower().strip() == "multiple raster":
elif tokens[1].lower().strip().startswith("multiple raster"):
param = ParameterMultipleInput(tokens[0], desc, ParameterMultipleInput.TYPE_RASTER)
param.optional = False
try:
lib = tokens[1].strip()[len("multiple raster")+1:]
if lib == "R.raster":
self.useRraster = True
else:
self.useRraster = False
except:
self.useRraster = False
elif tokens[1].lower().strip() == "multiple vector":
param = ParameterMultipleInput(tokens[0], desc, ParameterMultipleInput.TYPE_VECTOR_ANY)
param.optional = False
@@ -233,7 +249,10 @@ def getExportCommands(self):
if not value.endswith("tif"):
value = value + ".tif"
value = value.replace("\\", "/")
commands.append("writeGDAL(" + out.name + ",\"" + value + "\")")
if self.useRraster:
commands.append("writeRaster(" + out.name + ",\"" + value + "\", datatype=dataType(" + out.name + "), overwrite=TRUE)")
else:
commands.append("writeGDAL(" + out.name + ",\"" + value + "\")")
if isinstance(out, OutputVector):
value = out.value
if not value.endswith("shp"):
@@ -260,11 +279,18 @@ def getImportCommands(self):
commands.append(
'tryCatch(find.package("rgdal"), error=function(e) install.packages("rgdal", lib="%s"))' % rLibDir)
commands.append("library(\"rgdal\")");
if self.useRraster:
commands.append(
'tryCatch(find.package("raster"), error=function(e) install.packages("raster", lib="%s"))' % rLibDir)
commands.append("library(\"raster\")");
for param in self.parameters:
if isinstance(param, ParameterRaster):
value = param.value
value = value.replace("\\", "/")
commands.append(param.name + " = " + "readGDAL(\"" + value + "\")")
if self.useRraster:
commands.append(param.name + " = " + "brick(\"" + value + "\")")
else:
commands.append(param.name + " = " + "readGDAL(\"" + value + "\")")
if isinstance(param, ParameterVector):
value = param.getSafeExportedLayer()
value = value.replace("\\", "/")
@@ -294,7 +320,10 @@ def getImportCommands(self):
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 + "\"")
if self.useRraster:
commands.append("tempvar" + str(iLayer)+ " = " + "brick(\"" + layer + "\"")
else:
commands.append("tempvar" + str(iLayer)+ " = " + "readGDAL(\"" + layer + "\"")
iLayer+=1
else:
exported = param.getSafeExportedLayers()
@@ -0,0 +1,7 @@
##Classification=group
##Layer = raster|R.raster
##no_data_value = number 0
##showplots
Layer <- raster(Layer, 1)
NAvalue(Layer) = no_data_value
hist(as.matrix(Layer), breaks=100, xlab = basename(filename(Layer)))

0 comments on commit 28e594b

Please sign in to comment.