[Qgis-developer] [Processing][Rscript] Use Extent Parameter
Alexander Bruy
alexander.bruy at gmail.com
Tue May 24 06:53:08 PDT 2016
Hi René-Luc,
extent and some other parameters/outputs currently are not available
in modeler and scripts. I plan to add support for missed params/outputs
at hackfest.
2016-05-24 16:40 GMT+03:00 René-Luc Dhont <rldhont at gmail.com>:
> Hi devs,
>
> Has someone already created a Rscript for processing which use an extent in
> input parameters ?
>
> By reading the code, I understand that extent parameter is not set in the
> RScript.
> In the method 'getImportCommands' of the 'RAlgorithm' class, ParameterExtent
> is never transform to an R command.
> Can someone confirm it ?
>
> How to pass translate ParameterExtent to RScript ?
>
> The getImportCommands code:
> ```
> def getImportCommands(self):
> commands = []
>
> # Just use main mirror
> commands.append('options("repos"="http://cran.at.r-project.org/")')
>
> # Try to install packages if needed
> if isWindows():
> commands.append('.libPaths(\"' +
> unicode(RUtils.RLibs()).replace('\\', '/') + '\")')
> packages = RUtils.getRequiredPackages(self.script)
> packages.extend(['rgdal', 'raster'])
> for p in packages:
> commands.append('tryCatch(find.package("' + p
> + '"), error=function(e) install.packages("' + p
> + '", dependencies=TRUE))')
> commands.append('library("raster")')
> commands.append('library("rgdal")')
>
> for param in self.parameters:
> if isinstance(param, ParameterRaster):
> value = param.value
> value = value.replace('\\', '/')
> if self.passFileNames:
> commands.append(param.name + ' = "' + value + '"')
> elif self.useRasterPackage:
> commands.append(param.name + ' = ' + 'brick("' + value
> + '")')
> else:
> commands.append(param.name + ' = ' + 'readGDAL("' +
> value
> + '")')
> if isinstance(param, ParameterVector):
> value = param.getSafeExportedLayer()
> value = value.replace('\\', '/')
> filename = os.path.basename(value)
> filename = filename[:-4]
> folder = os.path.dirname(value)
> if self.passFileNames:
> commands.append(param.name + ' = "' + value + '"')
> else:
> commands.append(param.name + ' = readOGR("' + folder
> + '",layer="' + filename + '")')
> if isinstance(param, ParameterTable):
> value = param.value
> if not value.lower().endswith('csv'):
> raise GeoAlgorithmExecutionException(
> 'Unsupported input file format.\n' + value)
> if self.passFileNames:
> commands.append(param.name + ' = "' + value + '"')
> else:
> commands.append(param.name + ' <- read.csv("' + value
> + '", head=TRUE, sep=",")')
> elif isinstance(param, (ParameterTableField, ParameterString,
> ParameterFile)):
> commands.append(param.name + '="' + param.value + '"')
> elif isinstance(param, (ParameterNumber, ParameterSelection)):
> commands.append(param.name + '=' + unicode(param.value))
> elif isinstance(param, ParameterBoolean):
> if param.value:
> commands.append(param.name + '=TRUE')
> else:
> commands.append(param.name + '=FALSE')
> elif isinstance(param, ParameterMultipleInput):
> iLayer = 0
> if param.datatype == ParameterMultipleInput.TYPE_RASTER:
> layers = param.value.split(';')
> for layer in layers:
> layer = layer.replace('\\', '/')
> if self.passFileNames:
> commands.append('tempvar' + unicode(iLayer) + '
> <- "'
> + layer + '"')
> elif self.useRasterPackage:
> commands.append('tempvar' + unicode(iLayer) + '
> <- '
> + 'brick("' + layer + '")')
> else:
> commands.append('tempvar' + unicode(iLayer) + '
> <- '
> + 'readGDAL("' + layer + '")')
> iLayer += 1
> else:
> exported = param.getSafeExportedLayers()
> layers = exported.split(';')
> for layer in layers:
> if not layer.lower().endswith('shp') \
> and not self.passFileNames:
> raise GeoAlgorithmExecutionException(
> 'Unsupported input file format.\n' + layer)
> layer = layer.replace('\\', '/')
> filename = os.path.basename(layer)
> filename = filename[:-4]
> if self.passFileNames:
> commands.append('tempvar' + unicode(iLayer) + '
> <- "'
> + layer + '"')
> else:
> commands.append('tempvar' + unicode(iLayer) + '
> <- '
> + 'readOGR("' + layer +
> '",layer="'
> + filename + '")')
> iLayer += 1
> s = ''
> s += param.name
> s += ' = c('
> iLayer = 0
> for layer in layers:
> if iLayer != 0:
> s += ','
> s += 'tempvar' + unicode(iLayer)
> iLayer += 1
> s += ')\n'
> commands.append(s)
>
> if self.showPlots:
> htmlfilename = self.getOutputValue(RAlgorithm.RPLOTS)
> self.plotsFilename = htmlfilename + '.png'
> self.plotsFilename = self.plotsFilename.replace('\\', '/')
> commands.append('png("' + self.plotsFilename + '")')
>
> return commands
> ```
>
> René-Luc
> _______________________________________________
> Qgis-developer mailing list
> Qgis-developer at lists.osgeo.org
> List info: http://lists.osgeo.org/mailman/listinfo/qgis-developer
> Unsubscribe: http://lists.osgeo.org/mailman/listinfo/qgis-developer
--
Alexander Bruy
More information about the Qgis-developer
mailing list