Exemplo n.º 1
0
    def processAlgorithm(self, progress):
        '''Here is where the processing itself takes place'''

        input = self.getParameterValue(self.INPUT_LAYER)
        ogrLayer = self.ogrConnectionString(input)
        output = self.getOutputValue(self.OUTPUT_LAYER)

        #dst_ds = self.getParameterValue(self.DEST_DS)
        dst_ds = self.ogrConnectionString(output)
        dst_format = self.getParameterValue(self.DEST_FORMAT)
        ogr_dsco = [self.getParameterValue(self.DEST_DSCO)] #TODO: split
        #dst_ds = "PG:dbname='glarus_np' options='-c client_encoding=LATIN9'"
        #dst_format ="PostgreSQL"

        qDebug("Opening data source '%s'" % ogrLayer)
        poDS = ogr.Open( ogrLayer, False )
        if poDS is None:
            SextanteLog.addToLog(SextanteLog.LOG_ERROR, self.failure(ogrLayer))
            return

        srs = osr.SpatialReference()
        srs.ImportFromEPSG( 21781 ) #FIXME
        qDebug("Creating output '%s'" % dst_ds)
        if dst_format == "SQLite" and os.path.isfile(dst_ds):
            os.remove(dst_ds)
        qDebug("Using driver '%s'" % dst_format)
        driver = ogr.GetDriverByName(dst_format)
        poDstDS = driver.CreateDataSource(dst_ds, options = ogr_dsco)
        if poDstDS is None:
            SextanteLog.addToLog(SextanteLog.LOG_ERROR, "Error creating %s" % dst_ds)
            return
        self.ogrtransform(poDS, poDstDS, bOverwrite = True)
Exemplo n.º 2
0
    def processAlgorithm(self, progress):
        if not ogrAvailable:
            SextanteLog.addToLog(SextanteLog.LOG_ERROR, "OGR bindings not installed" )
            return

        input = self.getParameterValue(self.INPUT_LAYER)
        sql = self.getParameterValue(self.SQL)
        ogrLayer = self.ogrConnectionString(input)

        output = self.getOutputValue(self.OUTPUT)

        qDebug("Opening data source '%s'" % ogrLayer)
        poDS = ogr.Open( ogrLayer, False )
        if poDS is None:
            SextanteLog.addToLog(SextanteLog.LOG_ERROR, self.failure(ogrLayer))
            return

        result = self.select_values(poDS, sql)

        f = open(output, "w")
        f.write("<table>")
        for row in result:
            f.write("<tr>")
            for col in row:
                f.write("<td>"+col+"</td>")
            f.write("</tr>")
        f.write("</table>")
        f.close()
 def createAlgsList(self):
     #First we populate the list of algorihtms with those created extending
     #GeoAlgorithm directly (those that execute GDAL using the console)
     self.preloadedAlgs = [
         nearblack(),
         information(),
         warp(),
         translate(),
         rgb2pct(),
         pct2rgb(),
         merge(),
         polygonize(),
         gdaladdo(),
         OgrInfo(),
         Ogr2Ogr(),
         OgrSql()
     ]
     #And then we add those that are created as python scripts
     folder = self.scriptsFolder()
     for descriptionFile in os.listdir(folder):
         if descriptionFile.endswith("py"):
             try:
                 fullpath = os.path.join(self.scriptsFolder(),
                                         descriptionFile)
                 alg = GdalAlgorithm(fullpath)
                 self.preloadedAlgs.append(alg)
             except WrongScriptException, e:
                 SextanteLog.addToLog(SextanteLog.LOG_ERROR, e.msg)
Exemplo n.º 4
0
 def executeGrass(commands, progress):
     if SextanteUtils.isWindows():
         GrassUtils.createGrassScript(commands)
         command = ["cmd.exe", "/C ", GrassUtils.grassScriptFilename()]
     else:
         gisrc =  SextanteUtils.userFolder() + os.sep + "sextante.gisrc"
         os.putenv("GISRC", gisrc)
         os.putenv("GRASS_MESSAGE_FORMAT", "gui")
         os.putenv("GRASS_BATCH_JOB", GrassUtils.grassBatchJobFilename())
         GrassUtils.createGrassBatchJobFileFromGrassCommands(commands)
         os.chmod(GrassUtils.grassBatchJobFilename(), stat.S_IEXEC | stat.S_IREAD | stat.S_IWRITE)
         if SextanteUtils.isMac():
             command = GrassUtils.grassPath() + os.sep + "grass.sh " + GrassUtils.grassMapsetFolder() + "/user"
         else:
             command = "grass64 " + GrassUtils.grassMapsetFolder() + "/user"
     loglines = []
     loglines.append("GRASS execution console output")
     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 "GRASS_INFO_PERCENT" in line:
             try:
                 progress.setPercentage(int(line[len("GRASS_INFO_PERCENT")+ 2:]))
             except:
                 pass
         else:
             loglines.append(line)
             progress.setConsoleInfo(line)
     if SextanteConfig.getSetting(GrassUtils.GRASS_LOG_CONSOLE):
         SextanteLog.addToLog(SextanteLog.LOG_INFO, loglines)
Exemplo n.º 5
0
 def error(self, msg):
     QApplication.restoreOverrideCursor()
     QMessageBox.critical(self, "Error", msg)
     SextanteLog.addToLog(SextanteLog.LOG_ERROR, msg)
     if self.algEx:
         self.algEx.terminate()
     self.table.setEnabled(True)
Exemplo n.º 6
0
    def processAlgorithm(self, progress):
        layer = QGisLayers.getObjectFromUri(self.getParameterValue(self.INPUT))
        tolerance = self.getParameterValue(self.TOLERANCE)

        pointsBefore = 0
        pointsAfter = 0

        writer = self.getOutputFromName(self.OUTPUT).getVectorWriter(
            layer.pendingFields().toList(), layer.wkbType(), layer.crs())

        current = 0
        selection = QGisLayers.features(layer)
        total = 100.0 / float(len(selection))
        for f in selection:
            featGeometry = QgsGeometry(f.geometry())
            attrs = f.attributes()
            pointsBefore += self.geomVertexCount(featGeometry)
            newGeometry = featGeometry.simplify(tolerance)
            pointsAfter += self.geomVertexCount(newGeometry)
            feature = QgsFeature()
            feature.setGeometry(newGeometry)
            feature.setAttributes(attrs)
            writer.addFeature(feature)
            current += 1
            progress.setPercentage(int(current * total))

        del writer

        SextanteLog.addToLog(
            SextanteLog.LOG_INFO,
            "Simplify: Input geometries have been simplified from" +
            str(pointsBefore) + " to " + str(pointsAfter) + " points.")
Exemplo n.º 7
0
    def processAlgorithm(self, progress):
        commands = []
        commands.append(os.path.join(TauDEMUtils.mpiexecPath(), "mpiexec"))

        processNum = SextanteConfig.getSetting(TauDEMUtils.MPI_PROCESSES)
        if processNum <= 0:
          raise GeoAlgorithmExecutionException("Wrong number of MPI processes used.\nPlease set correct number before running TauDEM algorithms.")

        commands.append("-n")
        commands.append(str(processNum))
        commands.append(os.path.join(TauDEMUtils.taudemPath(), self.cmdName))
        commands.append("-plen")
        commands.append(self.getParameterValue(self.LENGTH_GRID))
        commands.append("-ad8")
        commands.append(self.getParameterValue(self.CONTRIB_AREA_GRID))
        commands.append("-par")
        commands.append(str(self.getParameterValue(self.THRESHOLD)))
        commands.append(str(self.getParameterValue(self.EXPONENT)))
        commands.append("-ss")
        commands.append(self.getOutputValue(self.STREAM_SOURCE_GRID))

        loglines = []
        loglines.append("TauDEM execution command")
        for line in commands:
            loglines.append(line)
        SextanteLog.addToLog(SextanteLog.LOG_INFO, loglines)

        TauDEMUtils.executeTauDEM(commands, progress)
Exemplo n.º 8
0
    def processAlgorithm(self, progress):
        '''Here is where the processing itself takes place'''

        input = self.getParameterValue(self.INPUT_LAYER)
        sql = self.getParameterValue(self.SQL)
        ogrLayer = self.ogrConnectionString(input)

        output = self.getOutputValue(self.OUTPUT)

        qDebug("Opening data source '%s'" % ogrLayer)
        poDS = ogr.Open(ogrLayer, False)
        if poDS is None:
            SextanteLog.addToLog(SextanteLog.LOG_ERROR, self.failure(ogrLayer))
            return

        result = self.select_values(poDS, sql)

        f = open(output, "w")
        f.write("<table>")
        for row in result:
            f.write("<tr>")
            for col in row:
                f.write("<td>" + col + "</td>")
            f.write("</tr>")
        f.write("</table>")
        f.close()
Exemplo n.º 9
0
    def processAlgorithm(self, progress):
        '''Here is where the processing itself takes place'''

        input = self.getParameterValue(self.INPUT_LAYER)
        ogrLayer = self.ogrConnectionString(input)
        output = self.getOutputValue(self.OUTPUT_LAYER)

        #dst_ds = self.getParameterValue(self.DEST_DS)
        dst_ds = self.ogrConnectionString(output)
        dst_format = self.getParameterValue(self.DEST_FORMAT)
        ogr_dsco = [self.getParameterValue(self.DEST_DSCO)]  # TODO: split
        #dst_ds = "PG:dbname='glarus_np' options='-c client_encoding=LATIN9'"
        #dst_format ="PostgreSQL"

        qDebug("Opening data source '%s'" % ogrLayer)
        poDS = ogr.Open(ogrLayer, False)
        if poDS is None:
            SextanteLog.addToLog(SextanteLog.LOG_ERROR, self.failure(ogrLayer))
            return

        srs = osr.SpatialReference()
        srs.ImportFromEPSG(21781)  # FIXME
        qDebug("Creating output '%s'" % dst_ds)
        if dst_format == "SQLite" and os.path.isfile(dst_ds):
            os.remove(dst_ds)
        qDebug("Using driver '%s'" % dst_format)
        driver = ogr.GetDriverByName(dst_format)
        poDstDS = driver.CreateDataSource(dst_ds, options=ogr_dsco)
        if poDstDS is None:
            SextanteLog.addToLog(SextanteLog.LOG_ERROR,
                                 "Error creating %s" % dst_ds)
            return
        self.ogrtransform(poDS, poDstDS, bOverwrite=True)
Exemplo n.º 10
0
 def executeSaga(progress):
     if SextanteUtils.isWindows():
         command = ["cmd.exe", "/C ", SagaUtils.sagaBatchJobFilename()]
     else:
         os.chmod(SagaUtils.sagaBatchJobFilename(), stat.S_IEXEC | stat.S_IREAD | stat.S_IWRITE)
         command = [SagaUtils.sagaBatchJobFilename()]
     loglines = []
     loglines.append("SAGA execution console output")
     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 "%" in line:
             s = "".join([x for x in line if x.isdigit()])
             try:
                 progress.setPercentage(int(s))
             except:
                 pass
         else:
             line = line.strip()
             if line != "/" and line != "-" and line != "\\" and line != "|":
                 loglines.append(line)
                 progress.setConsoleInfo(line)
     if SextanteConfig.getSetting(SagaUtils.SAGA_LOG_CONSOLE):
         SextanteLog.addToLog(SextanteLog.LOG_INFO, loglines)
Exemplo n.º 11
0
    def executeGrass(commands, progress, outputCommands = None):
        loglines = []
        loglines.append("GRASS execution console output")
        grassOutDone = False
        command = GrassUtils.prepareGrassExecution(commands)
        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 "GRASS_INFO_PERCENT" in line:
                try:
                    progress.setPercentage(int(line[len("GRASS_INFO_PERCENT")+ 2:]))
                except:
                    pass
            else:
                if "r.out" in line or "v.out" in line:
                    grassOutDone = True
                loglines.append(line)
                progress.setConsoleInfo(line)
        # Some GRASS scripts, like r.mapcalculator or r.fillnulls, call other GRASS scripts during execution. This may override any commands that are
        # still to be executed by the subprocess, which are usually the output ones. If that is the case runs the output commands again.
        if not grassOutDone and outputCommands:
            command = GrassUtils.prepareGrassExecution(outputCommands)
            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 "GRASS_INFO_PERCENT" in line:
                    try:
                        progress.setPercentage(int(line[len("GRASS_INFO_PERCENT")+ 2:]))
                    except:
                        pass
                else:
                    loglines.append(line)
                    progress.setConsoleInfo(line)

        if SextanteConfig.getSetting(GrassUtils.GRASS_LOG_CONSOLE):
            SextanteLog.addToLog(SextanteLog.LOG_INFO, loglines)
        return loglines;
Exemplo n.º 12
0
    def processAlgorithm(self, progress):
        commands = []
        commands.append(os.path.join(TauDEMUtils.mpiexecPath(), "mpiexec"))

        processNum = SextanteConfig.getSetting(TauDEMUtils.MPI_PROCESSES)
        if processNum <= 0:
          raise GeoAlgorithmExecutionException("Wrong number of MPI processes used.\nPlease set correct number before running TauDEM algorithms.")

        commands.append("-n")
        commands.append(str(processNum))
        commands.append(os.path.join(TauDEMUtils.taudemPath(), self.cmdName))
        commands.append("-ang")
        commands.append(self.getParameterValue(self.DINF_FLOW_DIR_GRID))
        commands.append("-fel")
        commands.append(self.getParameterValue(self.PIT_FILLED_GRID))
        commands.append("-m")
        commands.append(str(self.STAT_DICT[self.getParameterValue(self.STAT_METHOD)]))
        commands.append(str(self.DIST_DICT[self.getParameterValue(self.DIST_METHOD)]))
        commands.append("-thresh")
        commands.append(str(self.getParameterValue(self.THRESHOLD)))
        if str(self.getParameterValue(self.EDGE_CONTAM)).lower() == "false":
            commands.append("-nc")
        commands.append("-du")
        commands.append(self.getOutputValue(self.DIST_UP_GRID))

        loglines = []
        loglines.append("TauDEM execution command")
        for line in commands:
            loglines.append(line)
        SextanteLog.addToLog(SextanteLog.LOG_INFO, loglines)

        TauDEMUtils.executeTauDEM(commands, progress)
Exemplo n.º 13
0
    def executeRAlgorithm(alg, progress):
        RUtils.verboseCommands = alg.getVerboseCommands()
        RUtils.createRScriptFromRCommands(alg.getFullSetOfRCommands())
        if SextanteUtils.isWindows():
            command = [
                RUtils.RFolder() + os.sep + "bin" + os.sep + "R.exe", "CMD",
                "BATCH", "--vanilla",
                RUtils.getRScriptFilename(),
                RUtils.getConsoleOutputFilename()
            ]
        else:
            os.chmod(RUtils.getRScriptFilename(),
                     stat.S_IEXEC | stat.S_IREAD | stat.S_IWRITE)
            command = "R CMD BATCH --vanilla " + RUtils.getRScriptFilename(
            ) + " " + RUtils.getConsoleOutputFilename()

        proc = subprocess.Popen(command,
                                shell=True,
                                stdout=subprocess.PIPE,
                                stdin=subprocess.PIPE,
                                stderr=subprocess.STDOUT,
                                universal_newlines=True)
        proc.wait()
        RUtils.createConsoleOutput()
        loglines = []
        loglines.append("R execution console output")
        loglines += RUtils.allConsoleResults
        for line in loglines:
            progress.setConsoleInfo(line)
        SextanteLog.addToLog(SextanteLog.LOG_INFO, loglines)
Exemplo n.º 14
0
 def runFusion(commands, progress):
     loglines = []
     loglines.append("Fusion execution console output")
     proc = subprocess.Popen(commands, shell=True, stdout=subprocess.PIPE, stdin=subprocess.PIPE,stderr=subprocess.STDOUT, universal_newlines=False).stdout
     for line in iter(proc.readline, ""):
         loglines.append(line)
     SextanteLog.addToLog(SextanteLog.LOG_INFO, loglines)
Exemplo n.º 15
0
    def executeGrass(commands, progress, outputCommands = None):
        loglines = []
        loglines.append("GRASS execution console output")
        grassOutDone = False
        command = GrassUtils.prepareGrassExecution(commands)
        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 "GRASS_INFO_PERCENT" in line:
                try:
                    progress.setPercentage(int(line[len("GRASS_INFO_PERCENT")+ 2:]))
                except:
                    pass
            else:
                if "r.out" in line or "v.out" in line:
                    grassOutDone = True
                loglines.append(line)
                progress.setConsoleInfo(line)
        # Some GRASS scripts, like r.mapcalculator or r.fillnulls, call other GRASS scripts during execution. This may override any commands that are
        # still to be executed by the subprocess, which are usually the output ones. If that is the case runs the output commands again.
        if not grassOutDone and outputCommands:
            command = GrassUtils.prepareGrassExecution(outputCommands)
            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 "GRASS_INFO_PERCENT" in line:
                    try:
                        progress.setPercentage(int(line[len("GRASS_INFO_PERCENT")+ 2:]))
                    except:
                        pass
                else:
                    loglines.append(line)
                    progress.setConsoleInfo(line)

        if SextanteConfig.getSetting(GrassUtils.GRASS_LOG_CONSOLE):
            SextanteLog.addToLog(SextanteLog.LOG_INFO, loglines)
        return loglines;
Exemplo n.º 16
0
 def defineCharacteristicsFromFile(self):
     lines = open(self.descriptionFile)
     line = lines.readline().strip("\n").strip()
     self.grassName = line
     line = lines.readline().strip("\n").strip()
     self.name = line
     line = lines.readline().strip("\n").strip()
     self.group = line
     hasRasterOutput = False
     hasVectorOutput = False
     while line != "":
         try:
             line = line.strip("\n").strip()
             if line.startswith("Parameter"):
                 parameter = ParameterFactory.getFromString(line);
                 self.addParameter(parameter)
                 if isinstance(parameter, ParameterVector):
                    hasVectorOutput = True
                 if isinstance(parameter, ParameterMultipleInput) and parameter.datatype < 3:
                    hasVectorOutput = True                        
             elif line.startswith("*Parameter"):
                 param = ParameterFactory.getFromString(line[1:])
                 param.isAdvanced = True
                 self.addParameter(param)
             else:
                 output = OutputFactory.getFromString(line)
                 self.addOutput(output);
                 if isinstance(output, OutputRaster):
                     hasRasterOutput = True
             line = lines.readline().strip("\n").strip()
         except Exception,e:
             SextanteLog.addToLog(SextanteLog.LOG_ERROR, "Could not open GRASS algorithm: " + self.descriptionFile + "\n" + line)
             raise e
Exemplo n.º 17
0
 def executeGrass(commands, progress):
     if SextanteUtils.isWindows():
         GrassUtils.createGrassScript(commands)
         command = ["cmd.exe", "/C ", GrassUtils.grassScriptFilename()]
     else:
         gisrc =  SextanteUtils.userFolder() + os.sep + "sextante.gisrc"
         os.putenv("GISRC", gisrc)
         os.putenv("GRASS_MESSAGE_FORMAT", "gui")
         os.putenv("GRASS_BATCH_JOB", GrassUtils.grassBatchJobFilename())
         GrassUtils.createGrassBatchJobFileFromGrassCommands(commands)
         os.chmod(GrassUtils.grassBatchJobFilename(), stat.S_IEXEC | stat.S_IREAD | stat.S_IWRITE)
         if SextanteUtils.isMac():
             command = GrassUtils.grassPath() + os.sep + "grass.sh " + GrassUtils.grassMapsetFolder() + "/user"
         else:
             command = "grass64 " + GrassUtils.grassMapsetFolder() + "/user"
     loglines = []
     loglines.append("GRASS execution console output")
     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 "GRASS_INFO_PERCENT" in line:
             try:
                 progress.setPercentage(int(line[len("GRASS_INFO_PERCENT")+ 2:]))
             except:
                 pass
         else:
             loglines.append(line)
             progress.setConsoleInfo(line)
     if SextanteConfig.getSetting(GrassUtils.GRASS_LOG_CONSOLE):
         SextanteLog.addToLog(SextanteLog.LOG_INFO, loglines)
     shutil.rmtree(GrassUtils.grassMapsetFolder(), True)
Exemplo n.º 18
0
    def processAlgorithm(self, progress):
        polyLayer = QGisLayers.getObjectFromUri(self.getParameterValue(self.POLYGONS))
        pointLayer = QGisLayers.getObjectFromUri(self.getParameterValue(self.POINTS))
        fieldName = self.getParameterValue(self.FIELD)
        classFieldName = self.getParameterValue(self.CLASSFIELD)

        polyProvider = polyLayer.dataProvider()
        pointProvider = pointLayer.dataProvider()
        if polyProvider.crs() != pointProvider.crs():
            SextanteLog.addToLog(SextanteLog.LOG_WARNING,
                                 "CRS warning: Input layers have non-matching CRS. This may cause unexpected results.")

        classFieldIndex = pointProvider.fieldNameIndex(classFieldName)
        idxCount, fieldList = utils.findOrCreateField(polyLayer, polyLayer.pendingFields(), fieldName)

        writer = self.getOutputFromName(self.OUTPUT).getVectorWriter(fieldList,
                     polyProvider.geometryType(), polyProvider.crs())

        spatialIndex = utils.createSpatialIndex(pointLayer)

        ftPoint = QgsFeature()
        outFeat = QgsFeature()
        geom = QgsGeometry()

        current = 0
        hasIntersections = False

        features = QGisLayers.features(polyLayer)
        total = 100.0 / float(len(features))
        for ftPoly in features:
            geom = ftPoly.geometry()
            atMap = ftPoly.attributes()

            classes = []
            hasIntersections = False
            points = spatialIndex.intersects(geom.boundingBox())
            if len(points) > 0:
                hasIntersections = True

            if hasIntersections:
                for i in points:
                    pointLayer.featureAtId(int(i), ftPoint, True, True)
                    tmpGeom = QgsGeometry(ftPoint.geometry())
                    if geom.contains(tmpGeom):
                        clazz = ftPoint.attributes()[classFieldIndex].toString()
                        if not clazz in classes:
                            classes.append(clazz)

            outFeat.setGeometry(geom)        
            if idxCount == len(atMap):
                atMap.append(QVariant(len(classes)))
            else:
                atMap[idxCount] =  QVariant(len(classes))
            outFeat.setAttributes(atMap)
            writer.addFeature(outFeat)

            current += 1
            progress.setPercentage(current / total)

        del writer
Exemplo n.º 19
0
    def processAlgorithm(self, progress):
        layer = QGisLayers.getObjectFromUri(self.getParameterValue(self.INPUT))        
        tolerance =self.getParameterValue(self.TOLERANCE)        

        pointsBefore = 0
        pointsAfter = 0

        provider = layer.dataProvider()
        layer.select(layer.pendingAllAttributesList())

        writer = self.getOutputFromName(self.OUTPUT).getVectorWriter(layer.pendingFields(),
                     layer.wkbType(), provider.crs())

        current = 0        
        selection = QGisLayers.features(layer)
        total =  100.0 / float(len(selection))            
        for f in selection:
            featGeometry = QgsGeometry(f.geometry())
            attrMap = f.attributeMap()            
            pointsBefore += self.geomVertexCount(featGeometry)
            newGeometry = featGeometry.simplify(tolerance)
            pointsAfter += self.geomVertexCount(newGeometry)            
            feature = QgsFeature()
            feature.setGeometry(newGeometry)
            feature.setAttributeMap(attrMap)
            writer.addFeature(feature)
            current += 1
            progress.setPercentage(int(current * total))
    
        del writer

        SextanteLog.addToLog(SextanteLog.LOG_INFO, "Simplify: Input geometries have been simplified from"
                             + str(pointsBefore) + " to "  + str(pointsAfter) + " points.")
Exemplo n.º 20
0
    def defineCharacteristicsFromFile(self):
        lines = open(self.descriptionFile)
        line = lines.readline().strip("\n").strip()
        self.appkey = line
        line = lines.readline().strip("\n").strip()
        self.cliName = line
        line = lines.readline().strip("\n").strip()
        self.name = line
        line = lines.readline().strip("\n").strip()
        self.group = line
        while line != "":
            try:
                line = line.strip("\n").strip()
                if line.startswith("Parameter"):
                    param = ParameterFactory.getFromString(line)

                    # Hack for initializing the elevation parameters from Sextante configuration
                    if param.name == "-elev.dem.path":
                        param.default = OTBUtils.otbSRTMPath()
                    if param.name == "-elev.dem.geoid":
                        param.default = OTBUtils.otbGeoidPath()
                    self.addParameter(param)
                elif line.startswith("Extent"):
                    self.addParameter(ParameterExtent(self.REGION_OF_INTEREST, "Region of interest", "0,1,0,1"))
                    self.hasROI = True
                else:
                    self.addOutput(OutputFactory.getFromString(line))
                line = lines.readline().strip("\n").strip()
            except Exception,e:
                SextanteLog.addToLog(SextanteLog.LOG_ERROR, "Could not open OTB algorithm: " + self.descriptionFile + "\n" + line)
                raise e
Exemplo n.º 21
0
 def defineCharacteristicsFromFile(self):
     lines = open(self.descriptionFile)
     line = lines.readline().strip("\n").strip()
     self.grassName = line
     line = lines.readline().strip("\n").strip()
     self.name = line
     line = lines.readline().strip("\n").strip()
     self.group = line
     while line != "":
         try:
             line = line.strip("\n").strip()
             if line.startswith("Parameter"):
                 self.addParameter(ParameterFactory.getFromString(line))
             elif line.startswith("*Parameter"):
                 param = ParameterFactory.getFromString(line[1:])
                 param.isAdvanced = True
                 self.addParameter(param)
             else:
                 self.addOutput(OutputFactory.getFromString(line))
             line = lines.readline().strip("\n").strip()
         except Exception, e:
             SextanteLog.addToLog(
                 SextanteLog.LOG_ERROR, "Could not open GRASS algorithm: " +
                 self.descriptionFile + "\n" + line)
             raise e
Exemplo n.º 22
0
    def processAlgorithm(self, progress):
        commands.append(os.path.join(TauDEMUtils.mpiexecPath(), "mpiexec"))

        processNum = SextanteConfig.getSetting(TauDEMUtils.MPI_PROCESSES)
        if processNum <= 0:
          raise GeoAlgorithmExecutionException("Wrong number of MPI processes used.\nPlease set correct number before running TauDEM algorithms.")

        commands.append("-n")
        commands.append(str(processNum))
        commands.append(os.path.join(TauDEMUtils.taudemPath(), self.cmdName))
        commands.append("-ad8")
        commands.append(self.getParameterValue(self.D8_CONTRIB_AREA_GRID))
        commands.append("-p")
        commands.append(self.getParameterValue(self.D8_FLOW_DIR_GRID))
        commands.append("-fel")
        commands.append(self.getParameterValue(self.PIT_FILLED_GRID))
        commands.append("-ssa")
        commands.append(self.getParameterValue(self.ACCUM_STREAM_SOURCE_GRID))
        commands.append("-o")
        commands.append(self.getParameterValue(self.OUTLETS_SHAPE))
        commands.append("-par")
        commands.append(str(self.getParameterValue(self.MIN_TRESHOLD)))
        commands.append(str(self.getParameterValue(self.MAX_THRESHOLD)))
        commands.append(str(self.getParameterValue(self.TRESHOLD_NUM)))
        commands.append(str(self.getParameterValue(self.STEPS)))
        commands.append("-drp")
        commands.append(self.getOutputValue(self.DROP_ANALYSIS_FILE))

        loglines = []
        loglines.append("TauDEM execution command")
        for line in commands:
            loglines.append(line)
        SextanteLog.addToLog(SextanteLog.LOG_INFO, loglines)

        TauDEMUtils.executeTauDEM(commands, progress)
Exemplo n.º 23
0
 def processAlgorithm(self, progress):
     inField = self.getParameterValue(PointsInPolygon.FIELD)
     polyLayer = QGisLayers.getObjectFromUri(
         self.getParameterValue(PointsInPolygon.POLYGONS))
     pointLayer = QGisLayers.getObjectFromUri(
         self.getParameterValue(PointsInPolygon.POINTS))
     polyProvider = polyLayer.dataProvider()
     pointProvider = pointLayer.dataProvider()
     if polyProvider.crs() <> pointProvider.crs():
         SextanteLog.addToLog(
             SextanteLog.LOG_WARNING,
             "CRS warning!Warning: Input layers have non-matching CRS.\nThis may cause unexpected results."
         )
     allAttrs = polyProvider.attributeIndexes()
     polyProvider.select(allAttrs)
     allAttrs = pointProvider.attributeIndexes()
     pointProvider.select(allAttrs)
     fieldList = ftools_utils.getFieldList(polyLayer)
     index = polyProvider.fieldNameIndex(unicode(inField))
     if index == -1:
         index = polyProvider.fieldCount()
         field = QgsField(unicode(inField), QVariant.Double, "real", 24, 15,
                          "point count field")
         fieldList[index] = field
     sRs = polyProvider.crs()
     writer = self.getOutputFromName(
         PointsInPolygon.OUTPUT).getVectorWriter(
             fieldList, polyProvider.geometryType(), sRs)
     inFeat = QgsFeature()
     inFeatB = QgsFeature()
     outFeat = QgsFeature()
     inGeom = QgsGeometry()
     start = 15.00
     add = 85.00 / polyProvider.featureCount()
     spatialIndex = ftools_utils.createIndex(pointProvider)
     while polyProvider.nextFeature(inFeat):
         inGeom = inFeat.geometry()
         atMap = inFeat.attributeMap()
         outFeat.setAttributeMap(atMap)
         outFeat.setGeometry(inGeom)
         pointList = []
         count = 0
         #(check, pointList) = pointLayer.featuresInRectangle(inGeom.boundingBox(), True, True)
         #pointLayer.select(inGeom.boundingBox(), False)
         #pointList = pointLayer.selectedFeatures()
         pointList = spatialIndex.intersects(inGeom.boundingBox())
         if len(pointList) > 0: check = 0
         else: check = 1
         if check == 0:
             for i in pointList:
                 pointProvider.featureAtId(int(i), inFeatB, True, allAttrs)
                 tmpGeom = QgsGeometry(inFeatB.geometry())
                 if inGeom.contains(tmpGeom):
                     count = count + 1
         outFeat.setAttributeMap(atMap)
         outFeat.addAttribute(index, QVariant(count))
         writer.addFeature(outFeat)
         start = start + add
         progress.setPercentage(start)
     del writer
Exemplo n.º 24
0
    def defineCharacteristicsFromFile(self):
        lines = open(self.descriptionFile)
        line = lines.readline().strip("\n").strip()
        self.appkey = line
        line = lines.readline().strip("\n").strip()
        self.cliName = line
        line = lines.readline().strip("\n").strip()
        self.name = line
        line = lines.readline().strip("\n").strip()
        self.group = line
        while line != "":
            try:
                line = line.strip("\n").strip()
                if line.startswith("Parameter"):
                    param = ParameterFactory.getFromString(line)

                    # Hack for initializing the elevation parameters from Sextante configuration
                    if param.name == "-elev.dem.path":
                        param.default = OTBUtils.otbSRTMPath()
                    if param.name == "-elev.dem.geoid":
                        param.default = OTBUtils.otbGeoidPath()
                    self.addParameter(param)
                elif line.startswith("Extent"):
                    self.addParameter(
                        ParameterExtent(self.REGION_OF_INTEREST,
                                        "Region of interest", "0,1,0,1"))
                    self.hasROI = True
                else:
                    self.addOutput(OutputFactory.getFromString(line))
                line = lines.readline().strip("\n").strip()
            except Exception, e:
                SextanteLog.addToLog(
                    SextanteLog.LOG_ERROR, "Could not open OTB algorithm: " +
                    self.descriptionFile + "\n" + line)
                raise e
Exemplo n.º 25
0
 def executeSaga(progress):
     if SextanteUtils.isWindows():
         command = ["cmd.exe", "/C ", SagaUtils.sagaBatchJobFilename()]
     else:
         os.chmod(SagaUtils.sagaBatchJobFilename(),
                  stat.S_IEXEC | stat.S_IREAD | stat.S_IWRITE)
         command = [SagaUtils.sagaBatchJobFilename()]
     loglines = []
     loglines.append("SAGA execution console output")
     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 "%" in line:
             s = "".join([x for x in line if x.isdigit()])
             try:
                 progress.setPercentage(int(s))
             except:
                 pass
         else:
             line = line.strip()
             if line != "/" and line != "-" and line != "\\" and line != "|":
                 loglines.append(line)
                 progress.setConsoleInfo(line)
     if SextanteConfig.getSetting(SagaUtils.SAGA_LOG_CONSOLE):
         SextanteLog.addToLog(SextanteLog.LOG_INFO, loglines)
    def processAlgorithm(self, progress):
        polyLayer = QGisLayers.getObjectFromUri(self.getParameterValue(self.POLYGONS))
        pointLayer = QGisLayers.getObjectFromUri(self.getParameterValue(self.POINTS))
        fieldName = self.getParameterValue(self.FIELD)

        polyProvider = polyLayer.dataProvider()
        pointProvider = pointLayer.dataProvider()
        if polyProvider.crs() != pointProvider.crs():
            SextanteLog.addToLog(SextanteLog.LOG_WARNING,
                                 "CRS warning: Input layers have non-matching CRS. This may cause unexpected results.")

        idxCount, fieldList = utils.findOrCreateField(polyLayer, polyLayer.pendingFields(), fieldName)

        writer = self.getOutputFromName(self.OUTPUT).getVectorWriter(fieldList,
                     polyProvider.geometryType(), polyProvider.crs())

        spatialIndex = utils.createSpatialIndex(pointLayer)

        pointProvider.rewind()
        pointProvider.select()

        allAttrs = polyLayer.pendingAllAttributesList()
        polyLayer.select(allAttrs)

        ftPoly = QgsFeature()
        ftPoint = QgsFeature()
        outFeat = QgsFeature()
        geom = QgsGeometry()

        current = 0
        hasIntersections = False

        features = QGisLayers.features(polyLayer)
        total = 100.0 / float(len(features))
        for ftPoly in features:
            geom = ftPoly.geometry()
            atMap = ftPoly.attributeMap()

            count = 0
            hasIntersections = False
            points = spatialIndex.intersects(geom.boundingBox())
            if len(points) > 0:
                hasIntersections = True

            if hasIntersections:
                for i in points:
                    pointLayer.featureAtId(int(i), ftPoint, True, False)
                    tmpGeom = QgsGeometry(ftPoint.geometry())
                    if geom.contains(tmpGeom):
                        count += 1

            outFeat.setGeometry(geom)
            outFeat.setAttributeMap(atMap)
            outFeat.addAttribute(idxCount, QVariant(count))
            writer.addFeature(outFeat)

            current += 1
            progress.setPercentage(int(current * total))

        del writer
Exemplo n.º 27
0
    def processAlgorithm(self, progress):
        settings = QSettings()
        encoding = settings.value( "/UI/encoding", "System" ).toString()

        layer = QGisLayers.getObjectFromUri(self.getParameterValue(self.INPUT))
        useSelection = self.getParameterValue(self.USE_SELECTION)
        tolerance =self.getParameterValue(self.TOLERANCE)
        output = self.getOutputValue(self.OUTPUT)

        pointsBefore = 0
        pointsAfter = 0

        provider = layer.dataProvider()
        layer.select(layer.pendingAllAttributesList())

        writer = self.getOutputFromName(self.OUTPUT).getVectorWriter(layer.pendingFields(),
                     layer.wkbType(), provider.crs())

        current = 0
        if useSelection:
            selection = layer.selectedFeatures()
            total =  100.0 / float(len(selection))
            for f in selection:
              featGeometry = QgsGeometry(f.geometry())
              attrMap = f.attributeMap()

              pointsBefore += self.geomVertexCount(featGeometry)
              newGeometry = featGeometry.simplify(tolerance)
              pointsAfter += self.geomVertexCount(newGeometry)

              feature = QgsFeature()
              feature.setGeometry(newGeometry)
              feature.setAttributeMap(attrMap)
              writer.addFeature(feature)
              current += 1
              progress.setPercentage(int(current * total))
        else:
            total =  100.0 / float(provider.featureCount())
            f = QgsFeature()
            while layer.nextFeature(f):
                featGeometry = QgsGeometry(f.geometry())
                attrMap = f.attributeMap()

                pointsBefore += self.geomVertexCount(featGeometry)
                newGeometry = featGeometry.simplify(tolerance)
                pointsAfter += self.geomVertexCount(newGeometry)

                feature = QgsFeature()
                feature.setGeometry(newGeometry)
                feature.setAttributeMap(attrMap)
                writer.addFeature(feature)

                current += 1
                progress.setPercentage(int(current * total))

        del writer

        SextanteLog.addToLog(SextanteLog.LOG_INFO, "Simplify: Input geometries have been simplified from"
                             + str(pointsBefore) + " to "  + str(pointsAfter) + " points.")
    def runLwgeomFunc(self, lwgeom_in, lib, **kwargs):
        # call the liblwgeom buildarea
        lwgeom_out = lib.lwgeom_buildarea( lwgeom_in )
        if not lwgeom_out:
            SextanteLog.addToLog(SextanteLog.LOG_ERROR, "FAILURE: liblwgeom wasn't able to build area!")
            return

        return lwgeom_out
Exemplo n.º 29
0
 def executeTauDEM(command, progress):
     loglines = []
     loglines.append("TauDEM execution console output")
     fused_command = ''.join(['"%s" ' % c for c in command])
     proc = subprocess.Popen(fused_command, shell=True, stdout=subprocess.PIPE, stdin=subprocess.PIPE, stderr=subprocess.STDOUT, universal_newlines=True).stdout
     for line in iter(proc.readline, ""):
         loglines.append(line)
     SextanteLog.addToLog(SextanteLog.LOG_INFO, loglines)
    def runLwgeomFunc(self, lwgeom_in, lib, **kwargs):
        # call the liblwgeom make_valid
        lwgeom_out = lib.lwgeom_make_valid( lwgeom_in )
        if not lwgeom_out:
            SextanteLog.addToLog(SextanteLog.LOG_ERROR, "FAILURE: liblwgeom wasn't able to make the geometry valid!")
            return

        return lwgeom_out
Exemplo n.º 31
0
    def processAlgorithm(self, progress):
        layer = QGisLayers.getObjectFromUri(self.getParameterValue(self.INPUT))
        useSelection = self.getParameterValue(self.USE_SELECTION)
        tolerance = self.getParameterValue(self.TOLERANCE)
        output = self.getOutputValue(self.OUTPUT)

        pointsBefore = 0
        pointsAfter = 0

        provider = layer.dataProvider()
        layer.select(layer.pendingAllAttributesList())

        writer = self.getOutputFromName(self.OUTPUT).getVectorWriter(
            layer.pendingFields(), layer.wkbType(), provider.crs())

        current = 0
        if useSelection:
            selection = layer.selectedFeatures()
            total = 100.0 / float(len(selection))
            for f in selection:
                featGeometry = QgsGeometry(f.geometry())
                attrMap = f.attributeMap()

                pointsBefore += self.geomVertexCount(featGeometry)
                newGeometry = featGeometry.simplify(tolerance)
                pointsAfter += self.geomVertexCount(newGeometry)

                feature = QgsFeature()
                feature.setGeometry(newGeometry)
                feature.setAttributeMap(attrMap)
                writer.addFeature(feature)
                current += 1
                progress.setPercentage(int(current * total))
        else:
            total = 100.0 / float(provider.featureCount())
            f = QgsFeature()
            while layer.nextFeature(f):
                featGeometry = QgsGeometry(f.geometry())
                attrMap = f.attributeMap()

                pointsBefore += self.geomVertexCount(featGeometry)
                newGeometry = featGeometry.simplify(tolerance)
                pointsAfter += self.geomVertexCount(newGeometry)

                feature = QgsFeature()
                feature.setGeometry(newGeometry)
                feature.setAttributeMap(attrMap)
                writer.addFeature(feature)

                current += 1
                progress.setPercentage(int(current * total))

        del writer

        SextanteLog.addToLog(
            SextanteLog.LOG_INFO,
            "Simplify: Input geometries have been simplified from" +
            str(pointsBefore) + " to " + str(pointsAfter) + " points.")
Exemplo n.º 32
0
    def generalize(self, inputLayer, useSelection, tolerance):
        self.inputLayer = inputLayer
        self.useSelection = useSelection
        self.tolerance = tolerance
        self.pointsBefore = 0
        self.pointsAfter = 0
        vProvider = self.inputLayer.dataProvider()
        allAttrs = vProvider.attributeIndexes()
        vProvider.select(allAttrs)
        shapeFields = vProvider.fields()
        crs = vProvider.crs()
        wkbType = self.inputLayer.wkbType()
        if not crs.isValid():
            crs = None
        shapeFileWriter = self.getOutputFromName(
            SimplifyGeometries.OUTPUT).getVectorWriter(shapeFields, wkbType,
                                                       crs)
        featureId = 0
        if self.useSelection:
            selection = self.inputLayer.selectedFeatures()
            self.maxRange = len(selection)
            for f in selection:
                featGeometry = QgsGeometry(f.geometry())
                attrMap = f.attributeMap()
                self.pointsBefore += geomVertexCount(featGeometry)
                newGeometry = featGeometry.simplify(self.tolerance)
                self.pointsAfter += geomVertexCount(newGeometry)
                feature = QgsFeature()
                feature.setGeometry(newGeometry)
                feature.setAttributeMap(attrMap)
                shapeFileWriter.addFeature(feature)
                featureId += 1
                self.emit(SIGNAL("featureProcessed()"))
        else:
            self.maxRange = vProvider.featureCount()
            f = QgsFeature()
            while vProvider.nextFeature(f):
                featGeometry = QgsGeometry(f.geometry())
                attrMap = f.attributeMap()
                self.pointsBefore += self.geomVertexCount(featGeometry)
                newGeometry = featGeometry.simplify(self.tolerance)
                self.pointsAfter += self.geomVertexCount(newGeometry)
                feature = QgsFeature()
                feature.setGeometry(newGeometry)
                feature.setAttributeMap(attrMap)
                shapeFileWriter.addFeature(feature)
                featureId += 1
                self.progress.setPercentage(self.processedFeatures /
                                            self.maxRange * 100)

        if shapeFileWriter != None:
            del shapeFileWriter

        SextanteLog.addToLog(
            SextanteLog.LOG_INFO,
            "Simplify: Input geometries have been simplified from" +
            str(self.pointsBefore) + " to " + str(self.pointsAfter) +
            " points.")
Exemplo n.º 33
0
    def processAlgorithm(self, progress):
        layerA = QGisLayers.getObjectFromUri(self.getParameterValue(Difference.INPUT))
        layerB = QGisLayers.getObjectFromUri(self.getParameterValue(Difference.OVERLAY))

        GEOS_EXCEPT = True

        FEATURE_EXCEPT = True

        writer = self.getOutputFromName(Difference.OUTPUT).getVectorWriter(layerA.pendingFields(),
                     layerA.dataProvider().geometryType(), layerA.dataProvider().crs())

        inFeatA = QgsFeature()
        inFeatB = QgsFeature()
        outFeat = QgsFeature()

        index = utils.createSpatialIndex(layerB)

        selectionA = QGisLayers.features(layerA)

        current = 0
        total = 100.0 / float(len(selectionA))

        for inFeatA in selectionA:
            add = True
            geom = QgsGeometry(inFeatA.geometry())
            diff_geom = QgsGeometry(geom)
            attrs = inFeatA.attributes()
            intersections = index.intersects(geom.boundingBox())
            for i in intersections:
                request = QgsFeatureRequest().setFilterFid(i)
                inFeatB = layerB.getFeatures(request).next()
                tmpGeom = QgsGeometry(inFeatB.geometry())
                try:
                    if diff_geom.intersects(tmpGeom):
                        diff_geom = QgsGeometry(diff_geom.difference(tmpGeom))
                except:
                    GEOS_EXCEPT = False
                    add = False
                    break

            if add:
                try:
                    outFeat.setGeometry(diff_geom)
                    outFeat.setAttributes(attrs)
                    writer.addFeature(outFeat)
                except:
                    FEATURE_EXCEPT = False
                    continue

            current += 1
            progress.setPercentage(int(current * total))

        del writer

        if not GEOS_EXCEPT:
            SextanteLog.addToLog(SextanteLog.LOG_WARNING, "Geometry exception while computing difference")
        if not FEATURE_EXCEPT:
            SextanteLog.addToLog(SextanteLog.LOG_WARNING, "Feature exception while computing difference")
Exemplo n.º 34
0
 def processAlgorithm(self, progress):
     inField = self.getParameterValue(SumLines.FIELD)
     lineLayer = QGisLayers.getObjectFromUri(self.getParameterValue(SumLines.LINES))
     polyLayer = QGisLayers.getObjectFromUri(self.getParameterValue(SumLines.POLYGONS))
     polyProvider = polyLayer.dataProvider()
     lineProvider = lineLayer.dataProvider()
     if polyProvider.crs() <> lineProvider.crs():
         SextanteLog.addToLog(SextanteLog.LOG_WARNING,
                              "CRS warning!Warning: Input layers have non-matching CRS.\nThis may cause unexpected results.")
     allAttrs = polyProvider.attributeIndexes()
     polyProvider.select(allAttrs)
     allAttrs = lineProvider.attributeIndexes()
     lineProvider.select(allAttrs)
     fieldList = ftools_utils.getFieldList(polyLayer)
     index = polyProvider.fieldNameIndex(unicode(inField))
     if index == -1:
         index = polyProvider.fieldCount()
         field = QgsField(unicode(inField), QVariant.Double, "real", 24, 15, self.tr("length field"))
         fieldList[index] = field
     sRs = polyProvider.crs()
     inFeat = QgsFeature()
     inFeatB = QgsFeature()
     outFeat = QgsFeature()
     inGeom = QgsGeometry()
     outGeom = QgsGeometry()
     distArea = QgsDistanceArea()
     lineProvider.rewind()
     start = 15.00
     add = 85.00 / polyProvider.featureCount()
     writer = self.getOutputFromName(SumLines.OUTPUT).getVectorWriter(fieldList, polyProvider.geometryType(), sRs)
     spatialIndex = ftools_utils.createIndex( lineProvider )
     while polyProvider.nextFeature(inFeat):
         inGeom = QgsGeometry(inFeat.geometry())
         atMap = inFeat.attributeMap()
         lineList = []
         length = 0
         #(check, lineList) = lineLayer.featuresInRectangle(inGeom.boundingBox(), True, False)
         #lineLayer.select(inGeom.boundingBox(), False)
         #lineList = lineLayer.selectedFeatures()
         lineList = spatialIndex.intersects(inGeom.boundingBox())
         if len(lineList) > 0: check = 0
         else: check = 1
         if check == 0:
             for i in lineList:
                 lineProvider.featureAtId( int( i ), inFeatB , True, allAttrs )
                 tmpGeom = QgsGeometry( inFeatB.geometry() )
                 if inGeom.intersects(tmpGeom):
                     outGeom = inGeom.intersection(tmpGeom)
                     length = length + distArea.measure(outGeom)
         outFeat.setGeometry(inGeom)
         outFeat.setAttributeMap(atMap)
         outFeat.addAttribute(index, QVariant(length))
         writer.addFeature(outFeat)
         start = start + add
         progress.setPercentage(start)
     del writer
 def _loadAlgorithms(self):
     folder = self.scriptsFolder()
     for descriptionFile in os.listdir(folder):
         if descriptionFile.endswith("py"):
             try:
                 fullpath = os.path.join(ScriptUtils.scriptsFolder(), descriptionFile)
                 alg = ScriptAlgorithm(fullpath)
                 if alg.name.strip() != "":
                     self.algs.append(alg)
             except WrongScriptException,e:
                 SextanteLog.addToLog(SextanteLog.LOG_ERROR,e.msg)
 def loadFromFolder(self, folder):
     if not os.path.exists(folder):
         return
     for descriptionFile in os.listdir(folder):
         if descriptionFile.endswith("py"):
             try:
                 fullpath = os.path.join(folder, descriptionFile)
                 alg = ScriptAlgorithm(fullpath)
                 if alg.name.strip() != "":
                     self.algs.append(alg)
             except WrongScriptException,e:
                 SextanteLog.addToLog(SextanteLog.LOG_ERROR,e.msg)
    def processAlgorithm(self, progress):
        # get the lib
        liblwgeom = self.getLwgeomLibrary()

        # retrieve the values of the parameters entered by the user
        inputFilename = self.getParameterValue(self.INPUT_LAYER)
        output = self.getOutputValue(self.OUTPUT_LAYER)

        # input layers vales are always a string with its location.
        # That string can be converted into a QGIS object (a QgsVectorLayer in this case))
        # using the Sextante.getObject() method
        inputLayer = Sextante.getObject(inputFilename)

        # create the output layer
        provider = inputLayer.dataProvider()
        encoding = provider.encoding()
        geomType = self.inputToOutputGeomType(inputLayer)
        writer = QgsVectorFileWriter( output, encoding, provider.fields(), geomType, provider.crs() )

        # Now we take the features and add them to the output layer, 
        # first check for selected features
        selection = inputLayer.selectedFeatures()
        if len(selection) > 0:
            count = len(selection)
            idx = 0

            for feat in selection:
                # run lwgeom algorithm on the feature geometry
                if not self.runLwgeom( feat.geometry(), lib=liblwgeom ):
                    SextanteLog.addToLog( SextanteLog.LOG_ERROR, u"FAILURE: previous failure info: layer %s, feature #%s" % (inputLayer.source(), feat.id()) )
                writer.addFeature(feat)

                progress.setPercentage( idx*100/count )
                idx += 1

        else:
            count = inputLayer.featureCount()
            idx = 0

            # no features selected on the layer, process all the features
            inputLayer.select( inputLayer.pendingAllAttributesList(), QgsRectangle(), True )
            feat = QgsFeature()
            while inputLayer.nextFeature( feat ):
                # run lwgeom algorithm on the feature geometry
                if not self.runLwgeom( feat.geometry(), lib=liblwgeom ):
                    SextanteLog.addToLog( SextanteLog.LOG_ERROR, u"FAILURE: previous failure info: layer %s, feature #%s" % (inputLayer.source(), feat.id()) )
                writer.addFeature(feat)

                progress.setPercentage( idx*100/count )
                idx += 1

        del writer
        progress.setPercentage( 100 )
 def runFusion(commands, progress):
     loglines = []
     loglines.append("Fusion execution console output")
     proc = subprocess.Popen(commands,
                             shell=True,
                             stdout=subprocess.PIPE,
                             stdin=subprocess.PIPE,
                             stderr=subprocess.STDOUT,
                             universal_newlines=False).stdout
     for line in iter(proc.readline, ""):
         loglines.append(line)
     SextanteLog.addToLog(SextanteLog.LOG_INFO, loglines)
Exemplo n.º 39
0
 def processAlgorithm(self, progress):
     inField = self.getParameterValue(PointsInPolygon.FIELD)
     polyLayer = QGisLayers.getObjectFromUri(self.getParameterValue(PointsInPolygon.POLYGONS))
     pointLayer = QGisLayers.getObjectFromUri(self.getParameterValue(PointsInPolygon.POINTS))
     polyProvider = polyLayer.dataProvider()
     pointProvider = pointLayer.dataProvider()
     if polyProvider.crs() <> pointProvider.crs():
         SextanteLog.addToLog(SextanteLog.LOG_WARNING,
                              "CRS warning!Warning: Input layers have non-matching CRS.\nThis may cause unexpected results.")
     allAttrs = polyProvider.attributeIndexes()
     polyProvider.select(allAttrs)
     allAttrs = pointProvider.attributeIndexes()
     pointProvider.select(allAttrs)
     fieldList = ftools_utils.getFieldList(polyLayer)
     index = polyProvider.fieldNameIndex(unicode(inField))
     if index == -1:
         index = polyProvider.fieldCount()
         field = QgsField(unicode(inField), QVariant.Double, "real", 24, 15, "point count field")
         fieldList[index] = field
     sRs = polyProvider.crs()
     writer = self.getOutputFromName(PointsInPolygon.OUTPUT).getVectorWriter(fieldList, polyProvider.geometryType(), sRs)
     inFeat = QgsFeature()
     inFeatB = QgsFeature()
     outFeat = QgsFeature()
     inGeom = QgsGeometry()
     start = 15.00
     add = 85.00 / polyProvider.featureCount()
     spatialIndex = ftools_utils.createIndex( pointProvider )
     while polyProvider.nextFeature(inFeat):
         inGeom = inFeat.geometry()
         atMap = inFeat.attributeMap()
         outFeat.setAttributeMap(atMap)
         outFeat.setGeometry(inGeom)
         pointList = []
         count = 0
         #(check, pointList) = pointLayer.featuresInRectangle(inGeom.boundingBox(), True, True)
         #pointLayer.select(inGeom.boundingBox(), False)
         #pointList = pointLayer.selectedFeatures()
         pointList = spatialIndex.intersects(inGeom.boundingBox())
         if len(pointList) > 0: check = 0
         else: check = 1
         if check == 0:
             for i in pointList:
                 pointProvider.featureAtId( int( i ), inFeatB , True, allAttrs )
                 tmpGeom = QgsGeometry( inFeatB.geometry() )
                 if inGeom.contains(tmpGeom):
                     count = count + 1
         outFeat.setAttributeMap(atMap)
         outFeat.addAttribute(index, QVariant(count))
         writer.addFeature(outFeat)
         start = start + add
         progress.setPercentage(start)
     del writer
 def createAlgsList(self):
     self.preloadedAlgs = []
     folder = self.scriptsFolder()
     for descriptionFile in os.listdir(folder):
         if descriptionFile.endswith("py"):
             try:
                 fullpath = os.path.join(self.scriptsFolder(), descriptionFile)
                 alg = PymorphAlgorithm(fullpath)
                 alg.group = "Algorithms"
                 self.preloadedAlgs.append(alg)
             except WrongScriptException,e:
                 SextanteLog.addToLog(SextanteLog.LOG_ERROR,e.msg)
 def loadFromFolder(self, folder):
     if not os.path.exists(folder):
         return
     for descriptionFile in os.listdir(folder):
         if descriptionFile.endswith("py"):
             try:
                 fullpath = os.path.join(folder, descriptionFile)
                 alg = ScriptAlgorithm(fullpath)
                 if alg.name.strip() != "":
                     self.algs.append(alg)
             except WrongScriptException, e:
                 SextanteLog.addToLog(SextanteLog.LOG_ERROR, e.msg)
Exemplo n.º 42
0
 def _loadAlgorithms(self):
     folder = self.scriptsFolder()
     for descriptionFile in os.listdir(folder):
         if descriptionFile.endswith("py"):
             try:
                 fullpath = os.path.join(ScriptUtils.scriptsFolder(),
                                         descriptionFile)
                 alg = ScriptAlgorithm(fullpath)
                 if alg.name.strip() != "":
                     self.algs.append(alg)
             except WrongScriptException, e:
                 SextanteLog.addToLog(SextanteLog.LOG_ERROR, e.msg)
Exemplo n.º 43
0
    def generalize( self, inputLayer, useSelection, tolerance):
        self.inputLayer = inputLayer
        self.useSelection = useSelection
        self.tolerance = tolerance
        self.pointsBefore = 0
        self.pointsAfter = 0
        vProvider = self.inputLayer.dataProvider()
        allAttrs = vProvider.attributeIndexes()
        vProvider.select( allAttrs )
        shapeFields = vProvider.fields()
        crs = vProvider.crs()
        wkbType = self.inputLayer.wkbType()
        if not crs.isValid():
            crs = None
        shapeFileWriter = self.getOutputFromName(SimplifyGeometries.OUTPUT).getVectorWriter(shapeFields, wkbType, crs )
        featureId = 0
        if self.useSelection:
            selection = self.inputLayer.selectedFeatures()
            self.maxRange = len( selection )
            for f in selection:
              featGeometry = QgsGeometry( f.geometry() )
              attrMap = f.attributeMap()
              self.pointsBefore += geomVertexCount( featGeometry )
              newGeometry = featGeometry.simplify( self.tolerance )
              self.pointsAfter += geomVertexCount( newGeometry )
              feature = QgsFeature()
              feature.setGeometry( newGeometry )
              feature.setAttributeMap( attrMap )
              shapeFileWriter.addFeature( feature )
              featureId += 1
              self.emit( SIGNAL( "featureProcessed()" ) )
        else:
            self.maxRange =  vProvider.featureCount()
            f = QgsFeature()
            while vProvider.nextFeature( f ):
              featGeometry = QgsGeometry( f.geometry() )
              attrMap = f.attributeMap()
              self.pointsBefore += self.geomVertexCount( featGeometry )
              newGeometry = featGeometry.simplify( self.tolerance )
              self.pointsAfter += self.geomVertexCount( newGeometry )
              feature = QgsFeature()
              feature.setGeometry( newGeometry )
              feature.setAttributeMap( attrMap )
              shapeFileWriter.addFeature( feature )
              featureId += 1
              self.progress.setPercentage(self.processedFeatures/self.maxRange * 100)

        if shapeFileWriter != None:
            del shapeFileWriter

        SextanteLog.addToLog(SextanteLog.LOG_INFO, "Simplify: Input geometries have been simplified from"
                             + str(self.pointsBefore) + " to "  + str(self.pointsAfter) + " points.")
 def _loadAlgorithms(self):
     folder = ModelerUtils.modelsFolder()
     for descriptionFile in os.listdir(folder):
         if descriptionFile.endswith("model"):
             try:
                 alg = ModelerAlgorithm()
                 fullpath = os.path.join(ModelerUtils.modelsFolder(),descriptionFile)
                 alg.openModel(fullpath)
                 if alg.name.strip() != "":
                     alg.provider = self
                     self.algs.append(alg)
             except WrongModelException,e:
                 SextanteLog.addToLog(SextanteLog.LOG_ERROR,"Could not load model " + descriptionFile + "\n" + e.msg)
Exemplo n.º 45
0
 def _loadAlgorithms(self):
     folder = ModelerUtils.modelsFolder()
     for descriptionFile in os.listdir(folder):
         if descriptionFile.endswith("model"):
             try:
                 alg = ModelerAlgorithm()
                 fullpath = os.path.join(ModelerUtils.modelsFolder(),descriptionFile)
                 alg.openModel(fullpath)
                 if alg.name.strip() != "":
                     alg.provider = self
                     self.algs.append(alg)
             except WrongModelException,e:
                 SextanteLog.addToLog(SextanteLog.LOG_ERROR,"Could not load model " + descriptionFile + "\n" + e.msg)
Exemplo n.º 46
0
 def createAlgsList(self):
     self.preloadedAlgs = []
     folder = OTBUtils.otbDescriptionPath()
     for descriptionFile in os.listdir(folder):
         if descriptionFile.endswith("txt"):
             try:
                 alg = OTBAlgorithm(os.path.join(folder, descriptionFile))
                 if alg.name.strip() != "":
                     self.preloadedAlgs.append(alg)
                 else:
                     SextanteLog.addToLog(SextanteLog.LOG_ERROR, "Could not open OTB algorithm: " + descriptionFile)
             except Exception,e:
                 SextanteLog.addToLog(SextanteLog.LOG_ERROR, "Could not open OTB algorithm: " + descriptionFile)
Exemplo n.º 47
0
 def createAlgsList(self):
     self.preloadedAlgs = []
     folder = SagaUtils.sagaDescriptionPath()
     for descriptionFile in os.listdir(folder):
         if descriptionFile.endswith("txt"):
             try:
                 alg = SagaAlgorithm(os.path.join(folder, descriptionFile))
                 if alg.name.strip() != "":
                     self.preloadedAlgs.append(alg)
                 else:
                     SextanteLog.addToLog(SextanteLog.LOG_ERROR, "Could not open SAGA algorithm: " + descriptionFile)
             except Exception,e:
                 SextanteLog.addToLog(SextanteLog.LOG_ERROR, "Could not open SAGA algorithm: " + descriptionFile)
Exemplo n.º 48
0
 def executeTauDEM(command, progress):
     loglines = []
     loglines.append("TauDEM execution console output")
     fused_command = ''.join(['"%s" ' % c for c in command])
     proc = subprocess.Popen(fused_command,
                             shell=True,
                             stdout=subprocess.PIPE,
                             stdin=subprocess.PIPE,
                             stderr=subprocess.STDOUT,
                             universal_newlines=True).stdout
     for line in iter(proc.readline, ""):
         loglines.append(line)
     SextanteLog.addToLog(SextanteLog.LOG_INFO, loglines)
Exemplo n.º 49
0
 def createAlgsList(self):
     self.preloadedAlgs = []
     folder = self.scriptsFolder()
     for descriptionFile in os.listdir(folder):
         if descriptionFile.endswith("py"):
             try:
                 fullpath = os.path.join(self.scriptsFolder(),
                                         descriptionFile)
                 alg = PymorphAlgorithm(fullpath)
                 alg.group = "Algorithms"
                 self.preloadedAlgs.append(alg)
             except WrongScriptException, e:
                 SextanteLog.addToLog(SextanteLog.LOG_ERROR, e.msg)
Exemplo n.º 50
0
 def execute(self, progress):
     '''The method to use to call a SEXTANTE algorithm.
     Although the body of the algorithm is in processAlgorithm(),
     it should be called using this method, since it performs
     some additional operations.
     Raises a GeoAlgorithmExecutionException in case anything goes wrong.'''
     self.setOutputCRSFromInputLayers()
     self.resolveTemporaryOutputs()
     self.checkOutputFileExtensions()
     try:
         self.processAlgorithm(progress)
     except GeoAlgorithmExecutionException, gaee:
         SextanteLog.addToLog(SextanteLog.LOG_ERROR, gaee.msg)
         raise gaee
Exemplo n.º 51
0
 def execute(self, progress):
     '''The method to use to call a SEXTANTE algorithm.
     Although the body of the algorithm is in processAlgorithm(),
     it should be called using this method, since it performs
     some additional operations.
     Raises a GeoAlgorithmExecutionException in case anything goes wrong.'''
     self.setOutputCRSFromInputLayers()
     self.resolveTemporaryOutputs()
     self.checkOutputFileExtensions()
     try:
         self.processAlgorithm(progress)
     except GeoAlgorithmExecutionException, gaee:
         SextanteLog.addToLog(SextanteLog.LOG_ERROR, gaee.msg)
         raise gaee
Exemplo n.º 52
0
 def runGdal(commands, progress):
     settings = QSettings()
     path = unicode(settings.value( "/GdalTools/gdalPath", ""))
     envval = unicode(os.getenv("PATH"))
     if not path.lower() in envval.lower().split(os.pathsep):
         envval += "%s%s" % (os.pathsep, path)
         os.putenv( "PATH", envval )
     loglines = []
     loglines.append("GDAL execution console output")
     fused_command = ''.join(['%s ' % c for c in commands])
     proc = subprocess.Popen(fused_command, shell=True, stdout=subprocess.PIPE, stdin=subprocess.PIPE,stderr=subprocess.STDOUT, universal_newlines=False).stdout
     for line in iter(proc.readline, ""):
         loglines.append(line)
     SextanteLog.addToLog(SextanteLog.LOG_INFO, loglines)
     GdalUtils.consoleOutput = loglines
 def loadFromFolder(self, folder):
     if not os.path.exists(folder):
         return
     for descriptionFile in os.listdir(folder):
         if descriptionFile.endswith("model"):
             try:
                 alg = ModelerAlgorithm()
                 fullpath = os.path.join(folder, descriptionFile)
                 alg.openModel(fullpath)
                 if alg.name.strip() != "":
                     alg.provider = self
                     self.algs.append(alg)
             except WrongModelException, e:
                 SextanteLog.addToLog(
                     SextanteLog.LOG_ERROR, "Could not load model " +
                     descriptionFile + "\n" + e.msg)
Exemplo n.º 54
0
    def addRecentAlgorithms(self, updating):
        showRecent = SextanteConfig.getSetting(
            SextanteConfig.SHOW_RECENT_ALGORITHMS)
        if showRecent:
            recent = SextanteLog.getRecentAlgorithms()
            if len(recent) != 0:
                found = False
                if updating:
                    recentItem = self.algorithmTree.topLevelItem(0)
                    treeWidget = recentItem.treeWidget()
                    treeWidget.takeTopLevelItem(
                        treeWidget.indexOfTopLevelItem(recentItem))
                    #self.algorithmTree.removeItemWidget(first, 0)

                recentItem = QTreeWidgetItem()
                recentItem.setText(0, self.tr("Recently used algorithms"))
                for algname in recent:
                    alg = Sextante.getAlgorithm(algname)
                    if alg is not None:
                        algItem = TreeAlgorithmItem(alg)
                        recentItem.addChild(algItem)
                        found = True
                if found:
                    self.algorithmTree.insertTopLevelItem(0, recentItem)
                    recentItem.setExpanded(True)

            self.algorithmTree.setWordWrap(True)
    def runLwgeom(self, geom, lib, **kwargs):
        # create a LWGEOM geometry parsing the WKB
        wkb_in = ctypes.create_string_buffer(geom.asWkb())
        wkb_size_in = ctypes.c_int(geom.wkbSize())
        LW_PARSER_CHECK_NONE = ctypes.c_char(chr(0))    #define LW_PARSER_CHECK_NONE   0
        try:
            lwgeom_in = lib.lwgeom_from_wkb( wkb_in, wkb_size_in, LW_PARSER_CHECK_NONE )
        finally:
            del wkb_in

        if not lwgeom_in:
            SextanteLog.addToLog(SextanteLog.LOG_ERROR, "FAILURE: liblwgeom wasn't able to parse the WKB!")
            return False

        # execute the liblwgeom function on the LWGEOM geometry
        try:
            lwgeom_out = self.runLwgeomFunc(lwgeom_in, lib=lib, **kwargs)
        finally:
            lib.lwgeom_free( lwgeom_in )
            del lwgeom_in

        if not lwgeom_out:
            return

        # convert the LWGEOM geometry back to WKB
        wkb_size_out = ctypes.c_size_t()
        WKB_ISO = ctypes.c_uint8(1)    #define WKB_ISO   0x01
        try:
            wkb_out = lib.lwgeom_to_wkb( lwgeom_out, WKB_ISO, ctypes.byref(wkb_size_out) )
        finally:
            lib.lwgeom_free( lwgeom_out )
            del lwgeom_out

        if not wkb_out or wkb_size_out <= 0:
            SextanteLog.addToLog(SextanteLog.LOG_ERROR, "FAILURE: liblwgeom wasn't able to convert the geometry back to WKB!")
            return False

        # update the QgsGeometry through the WKB
        wkb_geom = ctypes.string_at(wkb_out, wkb_size_out.value)
        lib.lwfree( wkb_out )
        del wkb_out

        geom.fromWkb( wkb_geom )
        return True