Example #1
0
 def initializeSettings(self):
     AlgorithmProvider.initializeSettings(self)
     if OTBUtils.findOtbPath() is None:
         ProcessingConfig.addSetting(
             Setting(self.getDescription(),
                     OTBUtils.OTB_FOLDER,
                     self.tr("OTB command line tools folder"),
                     OTBUtils.otbPath(),
                     valuetype=Setting.FOLDER))
     if OTBUtils.findOtbLibPath() is None:
         ProcessingConfig.addSetting(
             Setting(self.getDescription(),
                     OTBUtils.OTB_LIB_FOLDER,
                     self.tr("OTB applications folder"),
                     OTBUtils.otbLibPath(),
                     valuetype=Setting.FOLDER))
     ProcessingConfig.addSetting(
         Setting(self.getDescription(),
                 OTBUtils.OTB_SRTM_FOLDER,
                 self.tr("SRTM tiles folder"),
                 OTBUtils.otbSRTMPath(),
                 valuetype=Setting.FOLDER))
     ProcessingConfig.addSetting(
         Setting(self.getDescription(),
                 OTBUtils.OTB_GEOID_FILE,
                 self.tr("Geoid file"),
                 OTBUtils.otbGeoidPath(),
                 valuetype=Setting.FOLDER))
Example #2
0
 def initializeSettings(self):
     AlgorithmProvider.initializeSettings(self)
     if OTBUtils.findOtbPath() is None:
         ProcessingConfig.addSetting(
             Setting(
                 self.getDescription(),
                 OTBUtils.OTB_FOLDER,
                 self.tr("OTB command line tools folder"),
                 OTBUtils.otbPath(),
             )
         )
     if OTBUtils.findOtbLibPath() is None:
         ProcessingConfig.addSetting(
             Setting(
                 self.getDescription(),
                 OTBUtils.OTB_LIB_FOLDER,
                 self.tr("OTB applications folder"),
                 OTBUtils.otbLibPath(),
             )
         )
     ProcessingConfig.addSetting(
         Setting(
             self.getDescription(), OTBUtils.OTB_SRTM_FOLDER, self.tr("SRTM tiles folder"), OTBUtils.otbSRTMPath()
         )
     )
     ProcessingConfig.addSetting(
         Setting(self.getDescription(), OTBUtils.OTB_GEOID_FILE, self.tr("Geoid file"), OTBUtils.otbGeoidPath())
     )
 def initializeSettings(self):
     AlgorithmProvider.initializeSettings(self)
     ProcessingConfig.addSetting(
         Setting(self.getDescription(), OTBUtils.OTB_FOLDER,
                 "OTB command line tools folder", OTBUtils.otbPath()))
     ProcessingConfig.addSetting(
         Setting(self.getDescription(), OTBUtils.OTB_LIB_FOLDER,
                 "OTB applications folder", OTBUtils.otbLibPath()))
     ProcessingConfig.addSetting(
         Setting(self.getDescription(), OTBUtils.OTB_SRTM_FOLDER,
                 "SRTM tiles folder", OTBUtils.otbSRTMPath()))
     ProcessingConfig.addSetting(
         Setting(self.getDescription(), OTBUtils.OTB_GEOID_FILE,
                 "Geoid file", OTBUtils.otbGeoidPath()))
Example #4
0
    def processAlgorithm(self, progress):
        currentOs = os.name

        path = OTBUtils.otbPath()
        libpath = OTBUtils.otbLibPath()
        if path == "" or libpath == "":
            raise GeoAlgorithmExecutionException("OTB folder is not configured.\nPlease configure it before running OTB algorithms.")

        commands = []
        commands.append(path + os.sep + self.cliName)

        self.roiVectors = {}
        self.roiRasters = {}
        for param in self.parameters:
            # get the given input(s)
            if param.name in ["-il", "-in"] :
                newparams = ""
                listeParameters = param.value.split(";")
                for inputParameter in listeParameters :
                    # if HDF5 file
                    if "HDF5" in inputParameter :
                        if currentOs == "posix" :
                            data = inputParameter[6:]
                        else :
                            data = inputParameter[5:]
                        dataset = data

                        #on windows, there isn't "
                        #if data[-1] == '"':
                        if currentOs == "posix" :
                            data = data[:data.index('"')]
                        else :
                            data = data[:data.index('://')]
                        #try :
                        if currentOs == "posix" :
                            dataset.index('"')
                            dataset = os.path.basename( data ) + dataset[dataset.index('"'):]
                        #except ValueError :
                        else :
                            #dataset = os.path.basename( data ) + '"' + dataset[dataset.index('://'):]
                            dataset = dataset[dataset.index('://'):]

                        #get index of the subdataset with gdal
                        if currentOs == "posix" :
                            commandgdal = "gdalinfo " + data + " | grep '" + dataset + "$'"
                        else :
                            commandgdal = "gdalinfo " + data + " | findstr \"" + dataset + "$\""
                        resultGDAL = os.popen( commandgdal ).readlines()
                        indexSubdataset = -1
                        if resultGDAL :
                            indexSubdatasetString = re.search("SUBDATASET_(\d+)_", resultGDAL[0])
                            if indexSubdatasetString :
                                #match between ()
                                indexSubdataset = indexSubdatasetString.group(1)
                            else :
                                indexSubdataset = -1
                        else :
                            #print "Error : no match of ", dataset, "$ in gdalinfo " + data
                            indexSubdataset = -1


                        if not indexSubdataset == -1 :
                            indexSubdataset = int(indexSubdataset) -1
                            newParam = "\'" + data + "?&sdataidx=" + str(indexSubdataset) + "\'"

                        else :
                            newParam = inputParameter

                        newparams += newParam
                    # no hdf5
                    else :
                        newparams += inputParameter
                    newparams += ";"
                if newparams[-1] == ";":
                    newparams = newparams[:-1]
                param.value = newparams

            if param.value == None or param.value == "":
                continue
            if isinstance(param, ParameterVector):
                commands.append(param.name)
                if self.hasROI:
                    roiFile = getTempFilename('shp')
                    commands.append(roiFile)
                    self.roiVectors[param.value] = roiFile
                else:
                    commands.append("\"" + param.value+ "\"")
            elif isinstance(param, ParameterRaster):
                commands.append(param.name)
                if self.hasROI:
                    roiFile = getTempFilename('tif')
                    commands.append(roiFile)
                    self.roiRasters[param.value] = roiFile
                else:
                    commands.append("\"" + param.value+ "\"")
            elif isinstance(param, ParameterMultipleInput):
                commands.append(param.name)
                files = str(param.value).split(";")
                paramvalue = " ".join(["\"" + f + "\"" for f in files])
                commands.append(paramvalue)
            elif isinstance(param, ParameterSelection):
                commands.append(param.name)
                idx = int(param.value)
                commands.append(str(param.options[idx]))
            elif isinstance(param, ParameterBoolean):
                if param.value:
                    commands.append(param.name)
                    commands.append(str(param.value).lower())
            elif isinstance(param, ParameterExtent):
                self.roiValues = param.value.split(",")
            else:
                commands.append(param.name)
                commands.append(str(param.value))

        for out in self.outputs:
            commands.append(out.name)
            commands.append('"' + out.value + '"')
        for roiInput, roiFile in self.roiRasters.items():
            startX, startY = float(self.roiValues[0]), float(self.roiValues[1])
            sizeX = float(self.roiValues[2]) - startX
            sizeY = float(self.roiValues[3]) - startY
            helperCommands = [
                    "otbcli_ExtractROI",
                    "-in",       roiInput,
                    "-out",      roiFile,
                    "-startx",   str(startX),
                    "-starty",   str(startY),
                    "-sizex",    str(sizeX),
                    "-sizey",    str(sizeY)]
            ProcessingLog.addToLog(ProcessingLog.LOG_INFO, helperCommands)
            progress.setCommand(helperCommands)
            OTBUtils.executeOtb(helperCommands, progress)

        if self.roiRasters:
            supportRaster = self.roiRasters.itervalues().next()
            for roiInput, roiFile in self.roiVectors.items():
                helperCommands = [
                        "otbcli_VectorDataExtractROIApplication",
                        "-vd.in",           roiInput,
                        "-io.in",           supportRaster,
                        "-io.out",          roiFile,
                        "-elev.dem.path",   OTBUtils.otbSRTMPath()]
                ProcessingLog.addToLog(ProcessingLog.LOG_INFO, helperCommands)
                progress.setCommand(helperCommands)
                OTBUtils.executeOtb(helperCommands, progress)

        loglines = []
        loglines.append("OTB execution command")
        for line in commands:
            loglines.append(line)
            progress.setCommand(line)

        ProcessingLog.addToLog(ProcessingLog.LOG_INFO, loglines)
        import processing.algs.otb.OTBSpecific_XMLLoading
        module = processing.algs.otb.OTBSpecific_XMLLoading

        found = False
        if 'adapt%s' % self.appkey in dir(module):
            found = True
            commands = getattr(module, 'adapt%s' % self.appkey)(commands)
        else:
            the_key = 'adapt%s' % self.appkey
            if '-' in the_key:
                base_key = the_key.split("-")[0]
                if base_key in dir(module):
                    found = True
                    commands = getattr(module, base_key)(commands)

        if not found:
            ProcessingLog.addToLog(ProcessingLog.LOG_INFO, "Adapter for %s not found" % the_key)

        frames = inspect.getouterframes(inspect.currentframe())[1:]
        for a_frame in frames:
            frame,filename,line_number,function_name,lines,index = a_frame
            ProcessingLog.addToLog(ProcessingLog.LOG_INFO, "%s %s %s %s %s %s" % (frame,filename,line_number,function_name,lines,index))

        OTBUtils.executeOtb(commands, progress)
Example #5
0
    def processAlgorithm(self, progress):
        currentOs = os.name

        path = OTBUtils.otbPath()
        libpath = OTBUtils.otbLibPath()
        if path == "" or libpath == "":
            raise GeoAlgorithmExecutionException(
                "OTB folder is not configured.\nPlease configure it before running OTB algorithms."
            )

        commands = []
        commands.append(path + os.sep + self.cliName)

        self.roiVectors = {}
        self.roiRasters = {}
        for param in self.parameters:
            # get the given input(s)
            if param.name in ["-il", "-in"]:
                newparams = ""
                listeParameters = param.value.split(";")
                for inputParameter in listeParameters:
                    # if HDF5 file
                    if "HDF5" in inputParameter:
                        if currentOs == "posix":
                            data = inputParameter[6:]
                        else:
                            data = inputParameter[5:]
                        dataset = data

                        #on windows, there isn't "
                        #if data[-1] == '"':
                        if currentOs == "posix":
                            data = data[:data.index('"')]
                        else:
                            data = data[:data.index('://')]
                        #try :
                        if currentOs == "posix":
                            dataset.index('"')
                            dataset = os.path.basename(
                                data) + dataset[dataset.index('"'):]
                        #except ValueError :
                        else:
                            #dataset = os.path.basename( data ) + '"' + dataset[dataset.index('://'):]
                            dataset = dataset[dataset.index('://'):]

                        #get index of the subdataset with gdal
                        if currentOs == "posix":
                            commandgdal = "gdalinfo " + data + " | grep '" + dataset + "$'"
                        else:
                            commandgdal = "gdalinfo " + data + " | findstr \"" + dataset + "$\""
                        resultGDAL = os.popen(commandgdal).readlines()
                        indexSubdataset = -1
                        if resultGDAL:
                            indexSubdatasetString = re.search(
                                "SUBDATASET_(\d+)_", resultGDAL[0])
                            if indexSubdatasetString:
                                #match between ()
                                indexSubdataset = indexSubdatasetString.group(
                                    1)
                            else:
                                indexSubdataset = -1
                        else:
                            #print "Error : no match of ", dataset, "$ in gdalinfo " + data
                            indexSubdataset = -1

                        if not indexSubdataset == -1:
                            indexSubdataset = int(indexSubdataset) - 1
                            newParam = "\'" + data + "?&sdataidx=" + str(
                                indexSubdataset) + "\'"

                        else:
                            newParam = inputParameter

                        newparams += newParam
                    # no hdf5
                    else:
                        newparams += inputParameter
                    newparams += ";"
                if newparams[-1] == ";":
                    newparams = newparams[:-1]
                param.value = newparams

            if param.value == None or param.value == "":
                continue
            if isinstance(param, ParameterVector):
                commands.append(param.name)
                if self.hasROI:
                    roiFile = getTempFilename('shp')
                    commands.append(roiFile)
                    self.roiVectors[param.value] = roiFile
                else:
                    commands.append("\"" + param.value + "\"")
            elif isinstance(param, ParameterRaster):
                commands.append(param.name)
                if self.hasROI:
                    roiFile = getTempFilename('tif')
                    commands.append(roiFile)
                    self.roiRasters[param.value] = roiFile
                else:
                    commands.append("\"" + param.value + "\"")
            elif isinstance(param, ParameterMultipleInput):
                commands.append(param.name)
                files = str(param.value).split(";")
                paramvalue = " ".join(["\"" + f + "\"" for f in files])
                commands.append(paramvalue)
            elif isinstance(param, ParameterSelection):
                commands.append(param.name)
                idx = int(param.value)
                commands.append(str(param.options[idx]))
            elif isinstance(param, ParameterBoolean):
                if param.value:
                    commands.append(param.name)
                    commands.append(str(param.value).lower())
            elif isinstance(param, ParameterExtent):
                self.roiValues = param.value.split(",")
            else:
                commands.append(param.name)
                commands.append(str(param.value))

        for out in self.outputs:
            commands.append(out.name)
            commands.append('"' + out.value + '"')
        for roiInput, roiFile in self.roiRasters.items():
            startX, startY = float(self.roiValues[0]), float(self.roiValues[1])
            sizeX = float(self.roiValues[2]) - startX
            sizeY = float(self.roiValues[3]) - startY
            helperCommands = [
                "otbcli_ExtractROI", "-in", roiInput, "-out", roiFile,
                "-startx",
                str(startX), "-starty",
                str(startY), "-sizex",
                str(sizeX), "-sizey",
                str(sizeY)
            ]
            ProcessingLog.addToLog(ProcessingLog.LOG_INFO, helperCommands)
            progress.setCommand(helperCommands)
            OTBUtils.executeOtb(helperCommands, progress)

        if self.roiRasters:
            supportRaster = self.roiRasters.itervalues().next()
            for roiInput, roiFile in self.roiVectors.items():
                helperCommands = [
                    "otbcli_VectorDataExtractROIApplication", "-vd.in",
                    roiInput, "-io.in", supportRaster, "-io.out", roiFile,
                    "-elev.dem.path",
                    OTBUtils.otbSRTMPath()
                ]
                ProcessingLog.addToLog(ProcessingLog.LOG_INFO, helperCommands)
                progress.setCommand(helperCommands)
                OTBUtils.executeOtb(helperCommands, progress)

        loglines = []
        loglines.append("OTB execution command")
        for line in commands:
            loglines.append(line)
            progress.setCommand(line)

        ProcessingLog.addToLog(ProcessingLog.LOG_INFO, loglines)
        import processing.algs.otb.OTBSpecific_XMLLoading
        module = processing.algs.otb.OTBSpecific_XMLLoading

        found = False
        if 'adapt%s' % self.appkey in dir(module):
            found = True
            commands = getattr(module, 'adapt%s' % self.appkey)(commands)
        else:
            the_key = 'adapt%s' % self.appkey
            if '-' in the_key:
                base_key = the_key.split("-")[0]
                if base_key in dir(module):
                    found = True
                    commands = getattr(module, base_key)(commands)

        if not found:
            ProcessingLog.addToLog(ProcessingLog.LOG_INFO,
                                   "Adapter for %s not found" % the_key)

        #frames = inspect.getouterframes(inspect.currentframe())[1:]
        #for a_frame in frames:
        #    frame,filename,line_number,function_name,lines,index = a_frame
        #    ProcessingLog.addToLog(ProcessingLog.LOG_INFO, "%s %s %s %s %s %s" % (frame,filename,line_number,function_name,lines,index))

        OTBUtils.executeOtb(commands, progress)