Ejemplo n.º 1
0
    def checkBeforeOpeningParametersDialog(self):
        if SextanteUtils.isWindows():
            path = RUtils.RFolder()
            if path == "":
                return "R folder is not configured.\nPlease configure it before running R scripts."

        R_INSTALLED = "R_INSTALLED"
        settings = QSettings()
        if settings.contains(R_INSTALLED):
            return
        if SextanteUtils.isWindows():
            if SextanteConfig.getSetting(RUtils.R_USE64):
                execDir = "x64"
            else:
                execDir = "i386"
            command = [
                RUtils.RFolder() + os.sep + "bin" + os.sep + execDir + os.sep +
                "R.exe --version"
            ]
        else:
            command = ["R --version"]
        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 "R version" in line:
                settings.setValue(R_INSTALLED, True)
                return
        return "It seems that R is not correctly installed in your system.\nPlease install it before running R Scripts."
Ejemplo n.º 2
0
 def initializeSettings(self):
     AlgorithmProvider.initializeSettings(self)
     SextanteConfig.addSetting(
         Setting(self.getDescription(), RUtils.RSCRIPTS_FOLDER,
                 "R Scripts folder", RUtils.RScriptsFolder()))
     if SextanteUtils.isWindows():
         SextanteConfig.addSetting(
             Setting(self.getDescription(), RUtils.R_FOLDER, "R folder",
                     RUtils.RFolder()))
Ejemplo n.º 3
0
 def _loadAlgorithms(self):
     folder = RUtils.RScriptsFolder()
     for descriptionFile in os.listdir(folder):
         if descriptionFile.endswith("rsx"):
             try:
                 fullpath = os.path.join(RUtils.RScriptsFolder(), descriptionFile)
                 alg = RAlgorithm(fullpath)
                 if alg.name.strip() != "":
                     self.algs.append(alg)
             except WrongScriptException,e:
                 SextanteLog.addToLog(SextanteLog.LOG_ERROR,e.msg)
             except Exception, e:
                 SextanteLog.addToLog(SextanteLog.LOG_ERROR,"Could not load R script:" + descriptionFile)
Ejemplo n.º 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/html/en/docs/user_manual/sextante/3rdParty.html">Click here</a> to know more about how to install and configure R to be used with SEXTANTE</p>'
         return html
Ejemplo n.º 5
0
    def saveAlgorithm(self):
        if self.filename is None:
            self.filename = str(
                QtGui.QFileDialog.getSaveFileName(self, "Save Script",
                                                  RUtils.RScriptsFolder(),
                                                  "SEXTANTE R script (*.rsx)"))

        if self.filename:
            if not self.filename.endswith(".rsx"):
                self.filename += ".rsx"
            text = str(self.text.toPlainText())
            if self.alg is not None:
                self.alg.script = text
            try:
                fout = open(self.filename, "w")
                fout.write(text)
                fout.close()
            except IOError:
                QMessageBox.warning(
                    self, self.tr("I/O error"),
                    self.tr("Unable to save edits. Reason:\n %1").arg(
                        unicode(sys.exc_info()[1])))
                return
            self.update = True
            #if help strings were defined before saving the model for the first time, we do it here
            if self.help:
                f = open(self.filename + ".help", "wb")
                pickle.dump(self.help, f)
                f.close()
                self.help = None
            QtGui.QMessageBox.information(self, "Script saving",
                                          "Script was correctly saved.")
        else:
            self.filename = None
Ejemplo n.º 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 += "GRASS 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/html/en/docs/user_manual/sextante/3rdParty.html">Click here</a> to know more about how to install and configure R to be used with SEXTANTE</p>'

        return html
Ejemplo n.º 7
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/html/en/docs/user_manual/sextante/3rdParty.html">Click here</a> to know more about how to install and configure R to be used with SEXTANTE</p>'
         return html
Ejemplo n.º 8
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 += "GRASS 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/html/en/docs/user_manual/sextante/3rdParty.html">Click here</a> to know more about how to install and configure R to be used with SEXTANTE</p>'

        return html
Ejemplo n.º 9
0
 def processAlgorithm(self, progress):
     if SextanteUtils.isWindows():
         path = RUtils.RFolder()
         if path == "":
             raise GeoAlgorithmExecutionException("R folder is not configured.\nPlease configure it before running R scripts.")
     loglines = []
     loglines.append("R execution commands")
     loglines += self.getFullSetOfRCommands()
     for line in loglines:
         progress.setCommand(line)
     SextanteLog.addToLog(SextanteLog.LOG_INFO, loglines)
     RUtils.executeRAlgorithm(self, progress)
     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, "w")
         f.write(RUtils.getConsoleOutput())
         f.close()
Ejemplo n.º 10
0
 def processAlgorithm(self, progress):
     if SextanteUtils.isWindows():
         path = RUtils.RFolder()
         if path == "":
             raise GeoAlgorithmExecutionException(
                 "R folder is not configured.\nPlease configure it before running R scripts."
             )
     loglines = []
     loglines.append("R execution commands")
     loglines += self.getFullSetOfRCommands()
     for line in loglines:
         progress.setCommand(line)
     SextanteLog.addToLog(SextanteLog.LOG_INFO, loglines)
     RUtils.executeRAlgorithm(self, progress)
     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, "w")
         f.write(RUtils.getConsoleOutput())
         f.close()
Ejemplo n.º 11
0
    def saveAlgorithm(self):
        if self.filename is None:
            self.filename = QtGui.QFileDialog.getSaveFileName(
                self, "Save Script", RUtils.RScriptsFolder(),
                "SEXTANTE R script (*.rsx)")

        if self.filename:
            text = str(self.text.toPlainText())
            if self.alg is not None:
                self.alg.script = text
            fout = open(self.filename, "w")
            fout.write(text)
            fout.close()
            self.update = True
            #if help strings were defined before saving the model for the first time, we do it here
            if self.help:
                f = open(self.filename + ".help", "wb")
                pickle.dump(self.help, f)
                f.close()
                self.help = None
            QtGui.QMessageBox.information(self, "Script saving",
                                          "Script was correctly saved.")
        else:
            self.filename = None
Ejemplo n.º 12
0
 def _loadAlgorithms(self):
     folder = RUtils.RScriptsFolder()
     self.loadFromFolder(folder)
     folder = os.path.join(os.path.dirname(__file__), "scripts")
     self.loadFromFolder(folder)
Ejemplo n.º 13
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
        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 + "=" + str(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:
                        #if not layer.lower().endswith("asc") and not layer.lower().endswith("tif") and not self.passFileNames:
                            #raise GeoAlgorithmExecutionException("Unsupported input file format.\n" + layer)
                        layer = layer.replace("\\", "/")
                        if self.passFileNames:
                            commands.append("tempvar" + str(iLayer)+ " <- \"" + layer + "\"")
                        elif self.useRasterPackage:
                            commands.append("tempvar" + str(iLayer)+ " <- " + "brick(\"" + layer + "\")")
                        else:
                            commands.append("tempvar" + str(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" + str(iLayer)+ " <- \"" + layer + "\"")
                        else:
                            commands.append("tempvar" + str(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" + str(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
Ejemplo n.º 14
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
        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 + "=" + str(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:
                        #if not layer.lower().endswith("asc") and not layer.lower().endswith("tif") and not self.passFileNames:
                        #raise GeoAlgorithmExecutionException("Unsupported input file format.\n" + layer)
                        layer = layer.replace("\\", "/")
                        if self.passFileNames:
                            commands.append("tempvar" + str(iLayer) +
                                            " <- \"" + layer + "\"")
                        elif self.useRasterPackage:
                            commands.append("tempvar" + str(iLayer) + " <- " +
                                            "brick(\"" + layer + "\")")
                        else:
                            commands.append("tempvar" + str(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" + str(iLayer) +
                                            " <- \"" + layer + "\"")
                        else:
                            commands.append("tempvar" + str(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" + str(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