Beispiel #1
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)
Beispiel #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 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)
Beispiel #4
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
    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
Beispiel #6
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)
Beispiel #7
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)
Beispiel #8
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)
Beispiel #9
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)
Beispiel #10
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.")
Beispiel #11
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
Beispiel #12
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)
Beispiel #13
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;
    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
Beispiel #15
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)
Beispiel #16
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
    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
Beispiel #19
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)
Beispiel #20
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")
Beispiel #21
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 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)
    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 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)
 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)
 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
Beispiel #31
0
    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
Beispiel #32
0
def buffering(progress, writer, distance, field, useSelection, useField, layer, dissolve, segments):
    GEOS_EXCEPT = True
    FEATURE_EXCEPT = True

    layer.select(layer.pendingAllAttributesList())

    if useField:
        field = layer.fieldNameIndex(field)

    outFeat = QgsFeature()
    inFeat = QgsFeature()
    inGeom = QgsGeometry()
    outGeom = QgsGeometry()

    current = 0

    # there is selection in input layer
    if useSelection:
        total = 100.0 / float(layer.selectedFeatureCount())
        selection = layer.selectedFeatures()

        # with dissolve
        if dissolve:
            first = True
            for inFeat in selection:
                atMap = inFeat.attributeMap()
                if useField:
                    value = atMap[field].doDouble()[0]
                else:
                    value = distance

                inGeom = QgsGeometry(inFeat.geometry())
                try:
                    outGeom = inGeom.buffer(float(value), segments)
                    if first:
                      tempGeom = QgsGeometry(outGeom)
                      first = False
                    else:
                      try:
                        tempGeom = tempGeom.combine( outGeom )
                      except:
                        GEOS_EXCEPT = False
                        continue
                except:
                    GEOS_EXCEPT = False
                    continue

                current += 1
                progress.setPercentage(int(current * total))
            try:
                outFeat.setGeometry(tempGeom)
                writer.addFeature(outFeat)
            except:
                FEATURE_EXCEPT = False
        # without dissolve
        else:
            for inFeat in selection:
                atMap = inFeat.attributeMap()
                if useField:
                    value = atMap[field].toDouble()[0]
                else:
                    value = distance

                inGeom = QgsGeometry(inFeat.geometry())
                try:
                    outGeom = inGeom.buffer(float(value), segments)
                    try:
                        outFeat.setGeometry(outGeom)
                        outFeat.setAttributeMap(atMap)
                        writer.addFeature(outFeat)
                    except:
                        FEATURE_EXCEPT = False
                        continue
                except:
                    GEOS_EXCEPT = False
                    continue

                current += 1
                progress.setPercentage(int(current * total))
    # there is no selection in input layer
    else:
      total = 100.0 / float(layer.featureCount())

      # with dissolve
      if dissolve:
          first = True
          while layer.nextFeature(inFeat):
              atMap = inFeat.attributeMap()
              if useField:
                  value = atMap[field].toDouble()[0]
              else:
                  value = distance

              inGeom = QgsGeometry(inFeat.geometry())
              try:
                  outGeom = inGeom.buffer(float(value), segments)
                  if first:
                      tempGeom = QgsGeometry(outGeom)
                      first = False
                  else:
                      try:
                        tempGeom = tempGeom.combine(outGeom)
                      except:
                        GEOS_EXCEPT = False
                        continue
              except:
                  GEOS_EXCEPT = False
                  continue

              current += 1
              progress.setPercentage(int(current * total))
          try:
              outFeat.setGeometry(tempGeom)
              writer.addFeature(outFeat)
          except:
              FEATURE_EXCEPT = False
      # without dissolve
      else:
          while layer.nextFeature(inFeat):
              atMap = inFeat.attributeMap()
              if useField:
                  value = atMap[field].toDouble()[0]
              else:
                  value = distance

              inGeom = QgsGeometry(inFeat.geometry())
              try:
                  outGeom = inGeom.buffer(float(value), segments)
                  try:
                      outFeat.setGeometry(outGeom)
                      outFeat.setAttributeMap(atMap)
                      writer.addFeature(outFeat)
                  except:
                      FEATURE_EXCEPT = False
                      continue
              except:
                  GEOS_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 buffer")
    if not FEATURE_EXCEPT:
        SextanteLog.addToLog(SextanteLog.LOG_WARNING, "Feature exception while computing buffer")
Beispiel #33
0
    def processAlgorithm(self, progress):
        if SextanteUtils.isWindows():
            path = SagaUtils.sagaPath()
            if path == "":
                raise GeoAlgorithmExecutionException("SAGA folder is not configured.\nPlease configure it before running SAGA algorithms.")
        commands = list()
        self.exportedLayers = {}

        #1: Export rasters to sgrd and vectors to shp
        #   Tables must be in dbf format. We check that.
        if self.resample:
            self.calculateResamplingExtent()
        for param in self.parameters:
            if isinstance(param, ParameterRaster):
                if param.value == None:
                    continue
                value = param.value
                if not value.endswith("sgrd"):
                    commands.append(self.exportRasterLayer(value))
                if self.resample:
                    commands.append(self.resampleRasterLayer(value));
            if isinstance(param, ParameterVector):
                if param.value == None:
                    continue
                layer = QGisLayers.getObjectFromUri(param.value, False)
                if layer:
                    filename = LayerExporter.exportVectorLayer(layer)
                    self.exportedLayers[param.value]=filename
                elif not param.value.endswith("shp"):
                        raise GeoAlgorithmExecutionException("Unsupported file format")
            if isinstance(param, ParameterTable):
                if param.value == None:
                    continue
                table = QGisLayers.getObjectFromUri(param.value, False)
                if table:
                    filename = LayerExporter.exportTable(table)
                    self.exportedLayers[param.value]=filename
                elif not param.value.endswith("shp"):
                        raise GeoAlgorithmExecutionException("Unsupported file format")
            if isinstance(param, ParameterMultipleInput):
                if param.value == None:
                    continue
                layers = param.value.split(";")
                if layers == None or len(layers) == 0:
                    continue
                if param.datatype == ParameterMultipleInput.TYPE_RASTER:
                    for layerfile in layers:
                        if not layerfile.endswith("sgrd"):
                            commands.append(self.exportRasterLayer(layerfile))
                        if self.resample:
                            commands.append(self.resampleRasterLayer(layerfile));
                elif param.datatype == ParameterMultipleInput.TYPE_VECTOR_ANY:
                    for layerfile in layers:
                        layer = QGisLayers.getObjectFromUri(layerfile, False)
                        if layer:
                            filename = LayerExporter.exportVectorLayer(layer)
                            self.exportedLayers[layerfile]=filename
                        elif (not layerfile.endswith("shp")):
                            raise GeoAlgorithmExecutionException("Unsupported file format")

        #2: set parameters and outputs
        if SextanteUtils.isWindows():
            command = self.undecoratedGroup  + " \"" + self.cmdname + "\""
        else:
            command = "lib" + self.undecoratedGroup  + " \"" + self.cmdname + "\""

        if self.hardcodedStrings:
            for s in self.hardcodedStrings:
                command += " " + s

        for param in self.parameters:
            if param.value is None:
                continue
            if isinstance(param, (ParameterRaster, ParameterVector, ParameterTable)):
                value = param.value
                if value in self.exportedLayers.keys():
                    command += (" -" + param.name + " \"" + self.exportedLayers[value] + "\"")
                else:
                    command += (" -" + param.name + " \"" + value + "\"")
            elif isinstance(param, ParameterMultipleInput):
                s = param.value
                for layer in self.exportedLayers.keys():
                    s = s.replace(layer, self.exportedLayers[layer])
                command += (" -" + param.name + " \"" + s + "\"");
            elif isinstance(param, ParameterBoolean):
                if param.value:
                    command+=(" -" + param.name);
            elif isinstance(param, ParameterFixedTable):
                tempTableFile  = SextanteUtils.getTempFilename("txt")
                f = open(tempTableFile, "w")
                f.write('\t'.join([col for col in param.cols]) + "\n")
                values = param.value.split(",")
                for i in range(0, len(values), 3):
                    s = values[i] + "\t" + values[i+1] + "\t" + values[i+2] + "\n"
                    f.write(s)
                f.close()
                command+=( " -" + param.name + " \"" + tempTableFile + "\"")
            elif isinstance(param, ParameterExtent):
                #'we have to substract/add half cell size, since saga is center based, not corner based
                halfcell = self.getOutputCellsize() / 2
                offset = [halfcell, -halfcell, halfcell, -halfcell]
                values = param.value.split(",")
                for i in range(4):
                    command+=(" -" + self.extentParamNames[i] + " " + str(float(values[i]) + offset[i]));
            elif isinstance(param, (ParameterNumber, ParameterSelection)):
                command+=(" -" + param.name + " " + str(param.value));
            else:
                command+=(" -" + param.name + " \"" + str(param.value) + "\"");

        for out in self.outputs:
            if isinstance(out, OutputRaster):
                filename = out.getCompatibleFileName(self)
                filename = SextanteUtils.tempFolder() + os.sep + os.path.basename(filename) + ".sgrd"
                command+=(" -" + out.name + " \"" + filename + "\"");
            if isinstance(out, OutputVector):
                filename = out.getCompatibleFileName(self)
                command+=(" -" + out.name + " \"" + filename + "\"");
            if isinstance(out, OutputTable):
                filename = out.getCompatibleFileName(self)
                command+=(" -" + out.name + " \"" + filename + "\"");

        commands.append(command)

        #3:Export resulting raster layers
        for out in self.outputs:
            if isinstance(out, OutputRaster):
                filename = out.getCompatibleFileName(self)
                filename2 = SextanteUtils.tempFolder() + os.sep + os.path.basename(filename) + ".sgrd"
                if SextanteUtils.isWindows():
                    commands.append("io_gdal 1 -GRIDS \"" + filename2 + "\" -FORMAT 1 -TYPE 0 -FILE \"" + filename + "\"");
                else:
                    commands.append("libio_gdal 1 -GRIDS \"" + filename2 + "\" -FORMAT 1 -TYPE 0 -FILE \"" + filename + "\"");

        #4 Run SAGA
        SagaUtils.createSagaBatchJobFileFromSagaCommands(commands)
        loglines = []
        loglines.append("SAGA execution commands")
        for line in commands:
            progress.setCommand(line)
            loglines.append(line)
        if SextanteConfig.getSetting(SagaUtils.SAGA_LOG_COMMANDS):
            SextanteLog.addToLog(SextanteLog.LOG_INFO, loglines)
        SagaUtils.executeSaga(progress);
Beispiel #34
0
    def ogrtransform(self,
                     poSrcDS,
                     poDstDS,
                     papszLayers=[],
                     papszLCO=[],
                     bTransform=False,
                     bAppend=False,
                     bUpdate=False,
                     bOverwrite=False,
                     poOutputSRS=None,
                     poSourceSRS=None,
                     pszNewLayerName=None,
                     pszWHERE=None,
                     papszSelFields=None,
                     eGType=-2,
                     eGeomOp=GeomOperation.NONE,
                     dfGeomOpParam=0,
                     papszFieldTypesToString=[],
                     pfnProgress=None,
                     pProgressData=None,
                     nCountLayerFeatures=0,
                     poClipSrc=None,
                     poClipDst=None,
                     bExplodeCollections=False,
                     pszZField=None):

        # Process each data source layer
        if len(papszLayers) == 0:
            nLayerCount = poSrcDS.GetLayerCount()
            papoLayers = [None for i in range(nLayerCount)]
            iLayer = 0

            for iLayer in range(nLayerCount):
                poLayer = poSrcDS.GetLayer(iLayer)

                if poLayer is None:
                    SextanteLog.addToLog(
                        SextanteLog.LOG_ERROR,
                        "FAILURE: Couldn't fetch advertised layer %d!" %
                        iLayer)
                    return False

                papoLayers[iLayer] = poLayer
                iLayer = iLayer + 1

        # Process specified data source layers
        else:
            nLayerCount = len(papszLayers)
            papoLayers = [None for i in range(nLayerCount)]
            iLayer = 0

            for layername in papszLayers:
                poLayer = poSrcDS.GetLayerByName(layername)

                if poLayer is None:
                    SextanteLog.addToLog(
                        SextanteLog.LOG_ERROR,
                        "FAILURE: Couldn't fetch advertised layer %s!" %
                        layername)
                    return False

                papoLayers[iLayer] = poLayer
                iLayer = iLayer + 1

        for poSrcLayer in papoLayers:
            qDebug(poSrcLayer.GetLayerDefn().GetName())
            #TODO: poDstDS.GetLayerByName for VRT layer fails if name is not lower case

            ok = TranslateLayer( poSrcDS, poSrcLayer, poDstDS, papszLCO, pszNewLayerName, \
                          bTransform, poOutputSRS, poSourceSRS, papszSelFields, \
                          bAppend, eGType, bOverwrite, eGeomOp, dfGeomOpParam, \
                          papszFieldTypesToString, nCountLayerFeatures, \
                          poClipSrc, poClipDst, bExplodeCollections, pszZField, pszWHERE, \
                          pfnProgress, pProgressData)
        return True
    def runAlgorithm(algOrName, onFinish, *args):
        if isinstance(algOrName, GeoAlgorithm):
            alg = algOrName
        else:
            alg = Sextante.getAlgorithm(algOrName)
        if alg == None:
            print("Error: Algorithm not found\n")
            return
        if len(args) != alg.getVisibleParametersCount() + alg.getVisibleOutputsCount():
            print ("Error: Wrong number of parameters")
            sextante.alghelp(algOrName)
            return

        alg = alg.getCopy()
        if isinstance(args, dict):
            # set params by name
            for name, value in args.items():
                if alg.getParameterFromName(name).setValue(value):
                    continue;
                if alg.getOutputFromName(name).setValue(value):
                    continue;
                print ("Error: Wrong parameter value %s for parameter %s." % (value, name))
                return
        else:
            i = 0
            for param in alg.parameters:
                if not param.hidden:
                    if not param.setValue(args[i]):
                        print ("Error: Wrong parameter value: " + unicode(args[i]))
                        return
                    i = i +1

            for output in alg.outputs:
                if not output.hidden:
                    if not output.setValue(args[i]):
                        print ("Error: Wrong output value: " + unicode(args[i]))
                        return
                    i = i +1

        msg = alg.checkParameterValuesBeforeExecuting()
        if msg:
            print ("Unable to execute algorithm\n" + msg)
            return

        if not alg.checkInputCRS():
            print ("Warning: Not all input layers use the same CRS.\n" +
                   "This can cause unexpected results.")

        SextanteLog.addToLog(SextanteLog.LOG_ALGORITHM, alg.getAsCommand())

        # don't set the wait cursor twice, because then when you restore it
        # it will still be a wait cursor
        cursor = QApplication.overrideCursor()
        if cursor == None or cursor == 0:
            QApplication.setOverrideCursor(QCursor(Qt.WaitCursor))
        elif cursor.shape() != Qt.WaitCursor:
            QApplication.setOverrideCursor(QCursor(Qt.WaitCursor))

        useThreads = SextanteConfig.getSetting(SextanteConfig.USE_THREADS)

        #this is doing strange things, so temporarily the thread execution is disabled from the console
        useThreads = False

        if useThreads:
            algEx = AlgorithmExecutor(alg)
            progress = QProgressDialog()
            progress.setWindowTitle(alg.name)
            progress.setLabelText("Executing %s..." % alg.name)
            def finish():
                QApplication.restoreOverrideCursor()
                if onFinish is not None:
                    onFinish(alg, SilentProgress())
                progress.close()
            def error(msg):
                QApplication.restoreOverrideCursor()
                print msg
                SextanteLog.addToLog(SextanteLog.LOG_ERROR, msg)
            def cancel():
                try:
                    algEx.finished.disconnect()
                    algEx.terminate()
                    QApplication.restoreOverrideCursor()
                    progress.close()
                except:
                    pass
            algEx.error.connect(error)
            algEx.finished.connect(finish)
            algEx.start()
            algEx.wait()
        else:
            progress = SilentProgress()
            ret = UnthreadedAlgorithmExecutor.runalg(alg, progress)
            if onFinish is not None and ret:
                onFinish(alg, progress)
            QApplication.restoreOverrideCursor()
        return alg
 def error(msg):
     QApplication.restoreOverrideCursor()
     print msg
     SextanteLog.addToLog(SextanteLog.LOG_ERROR, msg)
Beispiel #37
0
    def processAlgorithm(self, progress):
        useSelection = self.getParameterValue(ConvexHull.USE_SELECTED)
        useField = (self.getParameterValue(ConvexHull.METHOD) == 1)
        field = self.getParameterValue(ConvexHull.FIELD)
        vlayerA = QGisLayers.getObjectFromUri(self.getParameterValue(ConvexHull.INPUT))
        GEOS_EXCEPT = True
        FEATURE_EXCEPT = True
        vproviderA = vlayerA.dataProvider()
        allAttrsA = vproviderA.attributeIndexes()
        vproviderA.select(allAttrsA)
        fields = { 0 : QgsField("ID", QVariant.Int),
                    1 : QgsField("Area",  QVariant.Double),
                    2 : QgsField("Perim", QVariant.Double) }
        writer = self.getOutputFromName(ConvexHull.OUTPUT).getVectorWriter(fields, QGis.WKBPolygon, vproviderA.crs())
        inFeat = QgsFeature()
        outFeat = QgsFeature()
        inGeom = QgsGeometry()
        outGeom = QgsGeometry()
        nElement = 0
        index = vproviderA.fieldNameIndex(field)
        # there is selection in input layer
        if useSelection:
          nFeat = vlayerA.selectedFeatureCount()
          selectionA = vlayerA.selectedFeatures()
          if useField:
            unique = ftools_utils.getUniqueValues( vproviderA, index )
            nFeat = nFeat * len( unique )
            for i in unique:
              hull = []
              first = True
              outID = 0
              for inFeat in selectionA:
                atMap = inFeat.attributeMap()
                idVar = atMap[ index ]
                if idVar.toString().trimmed() == i.toString().trimmed():
                  if first:
                    outID = idVar
                    first = False
                  inGeom = QgsGeometry( inFeat.geometry() )
                  points = ftools_utils.extractPoints( inGeom )
                  hull.extend( points )
                nElement += 1
                progress.setPercentage(int(nElement/nFeat * 100))
              if len( hull ) >= 3:
                tmpGeom = QgsGeometry( outGeom.fromMultiPoint( hull ) )
                try:
                  outGeom = tmpGeom.convexHull()
                  outFeat.setGeometry( outGeom )
                  (area, perim) = self.simpleMeasure( outGeom )
                  outFeat.addAttribute( 0, QVariant( outID ) )
                  outFeat.addAttribute( 1, QVariant( area ) )
                  outFeat.addAttribute( 2, QVariant( perim ) )
                  writer.addFeature( outFeat )
                except:
                  GEOS_EXCEPT = False
                  continue
          else:
            hull = []
            for inFeat in selectionA:
              inGeom = QgsGeometry( inFeat.geometry() )
              points = ftools_utils.extractPoints( inGeom )
              hull.extend( points )
              nElement += 1
              progress.setPercentage(int(nElement/nFeat * 100))
            tmpGeom = QgsGeometry( outGeom.fromMultiPoint( hull ) )
            try:
              outGeom = tmpGeom.convexHull()
              outFeat.setGeometry( outGeom )
              writer.addFeature( outFeat )
            except:
              GEOS_EXCEPT = False
        # there is no selection in input layer
        else:
          rect = vlayerA.extent()
          nFeat = vproviderA.featureCount()
          if useField:
            unique = ftools_utils.getUniqueValues( vproviderA, index )
            nFeat = nFeat * len( unique )
            for i in unique:
              hull = []
              first = True
              outID = 0
              vproviderA.select( allAttrsA )#, rect )
              #vproviderA.rewind()
              while vproviderA.nextFeature( inFeat ):
                atMap = inFeat.attributeMap()
                idVar = atMap[ index ]
                if idVar.toString().trimmed() == i.toString().trimmed():
                  if first:
                    outID = idVar
                    first = False
                  inGeom = QgsGeometry( inFeat.geometry() )
                  points = ftools_utils.extractPoints( inGeom )
                  hull.extend( points )
                nElement += 1
                progress.setPercentage(int(nElement/nFeat * 100))
              if len( hull ) >= 3:
                tmpGeom = QgsGeometry( outGeom.fromMultiPoint( hull ) )
                try:
                  outGeom = tmpGeom.convexHull()
                  outFeat.setGeometry( outGeom )
                  (area, perim) = self.simpleMeasure( outGeom )
                  outFeat.addAttribute( 0, QVariant( outID ) )
                  outFeat.addAttribute( 1, QVariant( area ) )
                  outFeat.addAttribute( 2, QVariant( perim ) )
                  writer.addFeature( outFeat )
                except:
                  GEOS_EXCEPT = False
                  continue
          else:
            hull = []
            #vproviderA.rewind()
            vproviderA.select(allAttrsA)
            while vproviderA.nextFeature( inFeat ):
              inGeom = QgsGeometry( inFeat.geometry() )
              points = ftools_utils.extractPoints( inGeom )
              hull.extend( points )
              nElement += 1
              progress.setPercentage(int(nElement/nFeat * 100))
            tmpGeom = QgsGeometry( outGeom.fromMultiPoint( hull ) )
            try:
              outGeom = tmpGeom.convexHull()
              outFeat.setGeometry( outGeom )
              writer.addFeature( outFeat )
            except:
              GEOS_EXCEPT = False
        del writer

        if not GEOS_EXCEPT:
            SextanteLog.addToLog(SextanteLog.LOG_WARNING, "Geometry exception while computing convex hull")
        if not FEATURE_EXCEPT:
            SextanteLog.addToLog(SextanteLog.LOG_WARNING, "Feature exception while computing convex hull")
Beispiel #38
0
    def processAlgorithm(self, progress):
        if SextanteUtils.isWindows():
            path = GrassUtils.grassPath()
            if path == "":
                raise GeoAlgorithmExecutionException(
                    "GRASS folder is not configured.\nPlease configure it before running GRASS algorithms."
                )

        commands = []
        self.exportedLayers = {}
        outputCommands = []

        # if GRASS session has been created outside of this algorithm then get the list of layers loaded in GRASS
        # otherwise start a new session
        existingSession = GrassUtils.sessionRunning
        if existingSession:
            self.exportedLayers = GrassUtils.getSessionLayers()
        else:
            GrassUtils.startGrassSession()

        #1: Export layer to grass mapset
        for param in self.parameters:
            if isinstance(param, ParameterRaster):
                if param.value == None:
                    continue
                value = param.value
                # check if the layer hasn't already been exported in, for example, previous GRASS calls in this session
                if value in self.exportedLayers.keys():
                    continue
                else:
                    self.setSessionProjectionFromLayer(value, commands)
                    commands.append(self.exportRasterLayer(value))
            if isinstance(param, ParameterVector):
                if param.value == None:
                    continue
                value = param.value
                if value in self.exportedLayers.keys():
                    continue
                else:
                    self.setSessionProjectionFromLayer(value, commands)
                    commands.append(self.exportVectorLayer(value))
            if isinstance(param, ParameterTable):
                pass
            if isinstance(param, ParameterMultipleInput):
                if param.value == None:
                    continue
                layers = param.value.split(";")
                if layers == None or len(layers) == 0:
                    continue
                if param.datatype == ParameterMultipleInput.TYPE_RASTER:
                    for layer in layers:
                        if layer in self.exportedLayers.keys():
                            continue
                        else:
                            self.setSessionProjectionFromLayer(layer, commands)
                            commands.append(self.exportRasterLayer(layer))
                elif param.datatype == ParameterMultipleInput.TYPE_VECTOR_ANY:
                    for layer in layers:
                        if layer in self.exportedLayers.keys():
                            continue
                        else:
                            self.setSessionProjectionFromLayer(layer, commands)
                            commands.append(self.exportVectorLayer(layer))

        self.setSessionProjectionFromProject(commands)

        region = str(self.getParameterValue(
            self.GRASS_REGION_EXTENT_PARAMETER))
        regionCoords = region.split(",")
        command = "g.region"
        command += " n=" + str(regionCoords[3])
        command += " s=" + str(regionCoords[2])
        command += " e=" + str(regionCoords[1])
        command += " w=" + str(regionCoords[0])
        cellsize = self.getParameterValue(self.GRASS_REGION_CELLSIZE_PARAMETER)
        if cellsize:
            command += " res=" + str(cellsize)
        else:
            command += " res=" + str(self.getDefaultCellsize())

        commands.append(command)

        #2: set parameters and outputs
        command = self.grassName
        for param in self.parameters:
            if param.value == None or param.value == "":
                continue
            if (param.name == self.GRASS_REGION_CELLSIZE_PARAMETER
                    or param.name == self.GRASS_REGION_EXTENT_PARAMETER
                    or param.name == self.GRASS_MIN_AREA_PARAMETER
                    or param.name == self.GRASS_SNAP_TOLERANCE_PARAMETER):
                continue
            if isinstance(param, (ParameterRaster, ParameterVector)):
                value = param.value
                if value in self.exportedLayers.keys():
                    command += (" " + param.name + "=" +
                                self.exportedLayers[value])
                else:
                    command += (" " + param.name + "=" + value)
            elif isinstance(param, ParameterMultipleInput):
                s = param.value
                for layer in self.exportedLayers.keys():
                    s = s.replace(layer, self.exportedLayers[layer])
                s = s.replace(";", ",")
                command += (" " + param.name + "=" + s)
            elif isinstance(param, ParameterBoolean):
                if param.value:
                    command += (" " + param.name)
            elif isinstance(param, ParameterSelection):
                idx = int(param.value)
                command += (" " + param.name + "=" + str(param.options[idx]))
            elif isinstance(param, ParameterString):
                command += (" " + param.name + "=\"" + str(param.value) + "\"")
            else:
                command += (" " + param.name + "=" + str(param.value))

        uniqueSufix = str(uuid.uuid4()).replace("-", "")
        for out in self.outputs:
            if isinstance(out, OutputFile):
                if out.name == 'outputtext':
                    #the 'outputtext' file is generated by piping output from GRASS, is not an actual grass command
                    command += (" > " + out.value)
                else:
                    command += (" " + out.name + "=\"" + out.value + "\"")
            elif not isinstance(out, OutputHTML):
                #html files are not generated by GRASS, only by SEXTANTE to decorate grass output, so we skip them
                #An output name to make sure it is unique if the session uses this algorithm several times
                uniqueOutputName = out.name + uniqueSufix
                command += (" " + out.name + "=" + uniqueOutputName)
                # add output file to exported layers, to indicate that they are present in GRASS
                self.exportedLayers[out.value] = uniqueOutputName

        command += " --overwrite"
        commands.append(command)

        #3:Export resulting layers to a format that qgis can read
        for out in self.outputs:
            if isinstance(out, OutputRaster):
                filename = out.value
                #Raster layer output: adjust region to layer before exporting
                commands.append("g.region rast=" + out.name + uniqueSufix)
                outputCommands.append("g.region rast=" + out.name +
                                      uniqueSufix)
                command = "r.out.gdal -c createopt=\"TFW=YES,COMPRESS=LZW\""
                command += " input="
                command += out.name + uniqueSufix
                command += " output=\"" + filename + "\""
                commands.append(command)
                outputCommands.append(command)

            if isinstance(out, OutputVector):
                filename = out.value
                command = "v.out.ogr -ce input=" + out.name + uniqueSufix
                command += " dsn=\"" + os.path.dirname(out.value) + "\""
                command += " format=ESRI_Shapefile"
                command += " olayer=" + os.path.basename(out.value)[:-4]
                command += " type=auto"
                commands.append(command)
                outputCommands.append(command)

        #4 Run GRASS
        loglines = []
        loglines.append("GRASS execution commands")
        for line in commands:
            progress.setCommand(line)
            loglines.append(line)
        if SextanteConfig.getSetting(GrassUtils.GRASS_LOG_COMMANDS):
            SextanteLog.addToLog(SextanteLog.LOG_INFO, loglines)
        self.consoleOutput = GrassUtils.executeGrass(commands, progress,
                                                     outputCommands)
        self.postProcessResults()
        # if the session has been created outside of this algorithm, add the new GRASS layers to it
        # otherwise finish the session
        if existingSession:
            GrassUtils.addSessionLayers(self.exportedLayers)
        else:
            GrassUtils.endGrassSession()
 def error(self, msg):
     QApplication.restoreOverrideCursor()
     QMessageBox.critical(self, "Error", msg)
     SextanteLog.addToLog(SextanteLog.LOG_ERROR, msg)
     self.close()
    def processAlgorithm(self, progress):
        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:
            if param.value == None or param.value == "":
                continue
            if isinstance(param, ParameterVector):
                commands.append(param.name)
                if self.hasROI:
                    roiFile = SextanteUtils.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 = SextanteUtils.getTempFilename('tif')
                    commands.append(roiFile)
                    self.roiRasters[param.value] = roiFile
                else:
                    commands.append(param.value)
            elif isinstance(param, ParameterMultipleInput):
                commands.append(param.name)
                commands.append(str(param.value.replace(";"," ")))
            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)]
            SextanteLog.addToLog(SextanteLog.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()]
                SextanteLog.addToLog(SextanteLog.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)

        SextanteLog.addToLog(SextanteLog.LOG_INFO, loglines)

        OTBUtils.executeOtb(commands, progress)
Beispiel #41
0
 def processAlgorithm(self, progress):
     useField = not self.getParameterValue(Dissolve.DISSOLVE_ALL)
     fieldname = self.getParameterValue(Dissolve.FIELD)
     vlayerA = QGisLayers.getObjectFromUri(
         self.getParameterValue(Dissolve.INPUT))
     field = vlayerA.dataProvider().fieldNameIndex(fieldname)
     GEOS_EXCEPT = True
     vproviderA = vlayerA.dataProvider()
     allAttrsA = vproviderA.attributeIndexes()
     fields = vproviderA.fields()
     writer = self.getOutputFromName(Dissolve.OUTPUT).getVectorWriter(
         fields, vproviderA.geometryType(), vproviderA.crs())
     #inFeat = QgsFeature()
     outFeat = QgsFeature()
     vproviderA.rewind()
     nElement = 0
     nFeat = vproviderA.featureCount()
     if not useField:
         first = True
         features = QGisLayers.features(vlayerA)
         for inFeat in features:
             nElement += 1
             progress.setPercentage(int(nElement / nFeat * 100))
             if first:
                 attrs = inFeat.attributeMap()
                 tmpInGeom = QgsGeometry(inFeat.geometry())
                 outFeat.setGeometry(tmpInGeom)
                 first = False
             else:
                 tmpInGeom = QgsGeometry(inFeat.geometry())
                 tmpOutGeom = QgsGeometry(outFeat.geometry())
                 try:
                     tmpOutGeom = QgsGeometry(tmpOutGeom.combine(tmpInGeom))
                     outFeat.setGeometry(tmpOutGeom)
                 except:
                     GEOS_EXCEPT = False
                     continue
                 outFeat.setAttributeMap(attrs)
                 writer.addFeature(outFeat)
     else:
         unique = vproviderA.uniqueValues(int(field))
         nFeat = nFeat * len(unique)
         for item in unique:
             first = True
             add = True
             vproviderA.select(allAttrsA)
             vproviderA.rewind()
             features = QGisLayers.features(vlayerA)
             for inFeat in features:
                 nElement += 1
                 progress.setPercentage(int(nElement / nFeat * 100))
                 atMap = inFeat.attributeMap()
                 tempItem = atMap[field]
                 if tempItem.toString().trimmed() == item.toString(
                 ).trimmed():
                     if first:
                         QgsGeometry(inFeat.geometry())
                         tmpInGeom = QgsGeometry(inFeat.geometry())
                         outFeat.setGeometry(tmpInGeom)
                         first = False
                         attrs = inFeat.attributeMap()
                     else:
                         tmpInGeom = QgsGeometry(inFeat.geometry())
                         tmpOutGeom = QgsGeometry(outFeat.geometry())
                     try:
                         tmpOutGeom = QgsGeometry(
                             tmpOutGeom.combine(tmpInGeom))
                         outFeat.setGeometry(tmpOutGeom)
                     except:
                         GEOS_EXCEPT = False
                         add = False
             if add:
                 outFeat.setAttributeMap(attrs)
                 writer.addFeature(outFeat)
     del writer
     if not GEOS_EXCEPT:
         SextanteLog.addToLog(SextanteLog.LOG_WARNING,
                              "Geometry exception while dissolving")