Example #1
0
    def getPostProcessingErrorMessage(self, wrongLayers):
        html = GeoAlgorithm.getPostProcessingErrorMessage(self, wrongLayers)
        msg = RUtils.checkRIsInstalled(True)
        html += '<p>This algorithm requires R to be run. A test to check if \
                 R is correctly installed and configured in your system has \
                 been performed, with the following result:</p><ul><i>'

        if msg is None:
            html += 'R seems to be correctly installed and \
                     configured</i></li></ul>'

            html += '<p>The script you have executed needs the following \
                     packages:</p><ul>'

            packages = RUtils.getRequiredPackages(self.script)
            for p in packages:
                html += '<li>' + p + '</li>'
            html += '</ul><p>Make sure they are installed in your R \
                     environment before trying to execute this script.</p>'

        else:
            html += msg + '</i></li></ul>'
            html += '<p><a href= "http://docs.qgis.org/2.0/en/docs/user_manual/processing/3rdParty.html">Click here</a> to know more about how to install and configure R to be used with QGIS</p>'

        return html
Example #2
0
 def initializeSettings(self):
     AlgorithmProvider.initializeSettings(self)
     ProcessingConfig.addSetting(Setting(self.getDescription(),
                                 RUtils.RSCRIPTS_FOLDER, 'R Scripts folder'
                                 , RUtils.RScriptsFolder()))
     if isWindows():
         ProcessingConfig.addSetting(Setting(self.getDescription(),
                 RUtils.R_FOLDER, 'R folder', RUtils.RFolder()))
         ProcessingConfig.addSetting(Setting(self.getDescription(),
                 RUtils.R_USE64, 'Use 64 bit version', False))
Example #3
0
 def initializeSettings(self):
     AlgorithmProvider.initializeSettings(self)
     ProcessingConfig.addSetting(
         Setting(self.getDescription(), RUtils.RSCRIPTS_FOLDER,
                 self.tr('R Scripts folder'), RUtils.RScriptsFolder()))
     if isWindows():
         ProcessingConfig.addSetting(
             Setting(self.getDescription(), RUtils.R_FOLDER,
                     self.tr('R folder'), RUtils.RFolder()))
         ProcessingConfig.addSetting(
             Setting(self.getDescription(), RUtils.R_LIBS_USER,
                     self.tr('R user library folder'), RUtils.RLibs()))
         ProcessingConfig.addSetting(
             Setting(self.getDescription(), RUtils.R_USE64,
                     self.tr('Use 64 bit version'), False))
Example #4
0
 def checkBeforeOpeningParametersDialog(self):
     msg = RUtils.checkRIsInstalled()
     if msg is not None:
         html = '<p>This algorithm requires R to be run.Unfortunately, \
                it seems that R is not installed in your system, or it \
                is not correctly configured to be used from QGIS</p>'
         html += '<p><a href= "http://docs.qgis.org/2.0/en/docs/user_manual/processing/3rdParty.html">Click here</a> to know more about how to install and configure R to be used with QGIS</p>'
         return html
Example #5
0
 def checkBeforeOpeningParametersDialog(self):
     msg = RUtils.checkRIsInstalled()
     if msg is not None:
         html = '<p>This algorithm requires R to be run.Unfortunately, \
                it seems that R is not installed in your system, or it \
                is not correctly configured to be used from QGIS</p>'
         html += '<p><a href= "http://docs.qgis.org/2.0/en/docs/user_manual/processing/3rdParty.html">Click here</a> to know more about how to install and configure R to be used with QGIS</p>'
         return html
Example #6
0
    def getPostProcessingErrorMessage(self, wrongLayers):
        html = GeoAlgorithm.getPostProcessingErrorMessage(self, wrongLayers)
        msg = RUtils.checkRIsInstalled(True)
        html += '<p>This algorithm requires R to be run. A test to check if \
                 R is correctly installed and configured in your system has \
                 been performed, with the following result:</p><ul><i>'
        if msg is None:
            html += 'R seems to be correctly installed and \
                     configured</i></li></ul>'
            html += '<p>The script you have executed needs the following \
                     packages:</p><ul>'
            packages = RUtils.getRequiredPackages(self.script)
            for p in packages:
                html += '<li>' + p + '</li>'
            html += '</ul><p>Make sure they are installed in your R \
                     environment before trying to execute this script.</p>'
        else:
            html += msg + '</i></li></ul>'
            html += '<p><a href= "http://docs.qgis.org/2.0/en/docs/user_manual/processing/3rdParty.html">Click here</a> to know more about how to install and configure R to be used with QGIS</p>'

        return html
Example #7
0
 def processAlgorithm(self, progress):
     if isWindows():
         path = RUtils.RFolder()
         if path == '':
             raise GeoAlgorithmExecutionException(
                 self.tr('R folder is not configured.\nPlease configure it '
                         'before running R scripts.'))
     loglines = []
     loglines.append(self.tr('R execution commands'))
     loglines += self.getFullSetOfRCommands()
     for line in loglines:
         progress.setCommand(line)
     ProcessingLog.addToLog(ProcessingLog.LOG_INFO, loglines)
     RUtils.executeRAlgorithm(self, progress)
     if self.showPlots:
         htmlfilename = self.getOutputValue(RAlgorithm.RPLOTS)
         f = open(htmlfilename, 'w')
         f.write('<html><img src="' + self.plotsFilename + '"/></html>')
         f.close()
     if self.showConsoleOutput:
         htmlfilename = self.getOutputValue(RAlgorithm.R_CONSOLE_OUTPUT)
         f = open(htmlfilename, 'w')
         f.write(RUtils.getConsoleOutput())
         f.close()
Example #8
0
    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
Example #9
0
 def _loadAlgorithms(self):
     folder = RUtils.RScriptsFolder()
     self.loadFromFolder(folder)
     folder = os.path.join(os.path.dirname(__file__), 'scripts')
     self.loadFromFolder(folder)