def processAlgorithm(self, progress):
        '''Here is where the processing itself takes place'''

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

        #input layers values 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
        vectorLayer = Sextante.getObject(inputFilename)

        #And now we can process

        #First we create the output layer.
        #The output value entered by the user is a string containing a filename,
        #so we can use it directly
        settings = QSettings()
        systemEncoding = settings.value( "/UI/encoding", "System" ).toString()
        provider = vectorLayer.dataProvider()
        writer = QgsVectorFileWriter( output, systemEncoding, provider.fields(), provider.geometryType(), provider.crs() )

        #Now we take the selected features and add them to the output layer
        selection = vectorLayer.selectedFeatures()
        for feat in selection:
            writer.addFeature(feat)
        del writer
Beispiel #2
0
    def processAlgorithm(self, progress):
        '''Here is where the processing itself takes place'''

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

        #input layers values 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
        vectorLayer = Sextante.getObject(inputFilename)

        #And now we can process

        #First we create the output layer.
        #The output value entered by the user is a string containing a filename,
        #so we can use it directly
        settings = QSettings()
        systemEncoding = settings.value("/UI/encoding", "System").toString()
        provider = vectorLayer.dataProvider()
        writer = QgsVectorFileWriter(output, systemEncoding, provider.fields(),
                                     provider.geometryType(), provider.crs())

        #Now we take the selected features and add them to the output layer
        selection = vectorLayer.selectedFeatures()
        for feat in selection:
            writer.addFeature(feat)
        del writer
Beispiel #3
0
def createTest(text):
    s = ""
    tokens =  text[len("sextante.runalg("):-1].split(",")
    cmdname = tokens[0][1:-1];
    methodname = "test_" + cmdname.replace(":","")
    s += "def " + methodname + "(self):\n"
    alg = Sextante.getAlgorithm(cmdname)
    execcommand = "sextante.runalg("
    i = 0
    for token in tokens:
        if i < alg.getVisibleParametersCount() + 1:                            
            if os.path.exists(token[1:-1]):
                token = os.path.basename(token[1:-1])[:-4]           
            execcommand += token + "(),"
        else:
            execcommand += "None,"
        i+=1
    s += "\toutputs=" + execcommand[:-1] + ")\n"

    i = -1 * len(alg.outputs)
    for out in alg.outputs:        
        filename = tokens[i][1:-1]
        if (filename == str(None)):
            raise Exception("Cannot create unit test for that algorithm execution.\nThe output cannot be a temporary file")        
        s+="\toutput=outputs['" + out.name + "']\n"
        if isinstance(out, (OutputNumber, OutputString)):
            s+="self.assertTrue(" + str(out) + ", output)\n"
        if isinstance(out, OutputRaster):
            dataset = gdal.Open(filename, GA_ReadOnly)
            array = dataset.ReadAsArray(1)
            s+="\tself.assertTrue(os.path.isfile(output))\n"
            s+="\tself.assertEqual(hashraster(output)," + str(hash(array)) + ")\n"
        if isinstance(out, OutputVector):
            layer = Sextante.getObject(filename)
            fields = layer.pendingFields()
            s+="\tlayer=QGisLayers.getObjectFromUri(output, True)\n"
            s+="\tfields=layer.pendingFields()\n"
            s+="\texpectednames=[" + ",".join(["'" + str(f.name()) + "'" for f in fields]) + "]\n"
            s+="\texpectedtypes=[" + ",".join(["'" + str(f.typeName()) +"'" for f in fields]) + "]\n"
            s+="\tnames=[str(f.name()) for f in fields]\n"
            s+="\ttypes=[str(f.typeName()) for f in fields]\n"
            s+="\tself.assertEqual(expectednames, names)\n"
            s+="\tself.assertEqual(expectedtypes, types)\n"
            features = QGisLayers.features(layer)
            numfeat = len(features)
            s+="\tfeatures=sextante.getfeatures(layer)\n"
            s+="\tself.assertEqual(" + str(numfeat) + ", len(features))\n"
            if numfeat > 0:
                feature = features.next()
                attrs = feature.attributes()
                s+="\tfeature=features.next()\n"
                s+="\tattrs=feature.attributes()\n"
                s+="\texpectedvalues=[" + ",".join(['"' + str(attr.toString()) + '"' for attr in attrs]) + "]\n"
                s+="\tvalues=[str(attr.toString()) for attr in attrs]\n"
                s+="\tself.assertEqual(expectedvalues, values)\n"

    dlg = ShowTestDialog(s)
    dlg.exec_()
Beispiel #4
0
def createTest(item):            
    s = ""                
    tokens = item.entry.text[len("sextante.runalg("):-1].split(",")                
    cmdname = tokens[0][1:-1];
    methodname = "test_" + cmdname.replace(":","")
    s += "def " + methodname + "():\n"
    alg = Sextante.getAlgorithm(cmdname)
    execcommand = "sextante.runalg("
    i = 0
    for token in tokens:
        if i < alg.getVisibleParametersCount():
            execcommand+=token + ","
        else:
            execcommand+="None,"
        i+=1                
    s += "\toutputs=" + execcommand[:-1] + ")\n"
                
    i = -1 * len(alg.outputs)
    for out in alg.outputs:
        filename = tokens[i][1:-1]                    
        s+="\toutput=outputs['" + out.name + "']\n"
        if isinstance(out, (OutputNumber, OutputString)):
            s+="self.assertTrue(" + str(out) + ", output)\n"
        if isinstance(out, OutputRaster):                   
            dataset = gdal.Open(filename, GA_ReadOnly)
            array = dataset.ReadAsArray(1)
            s+="\tself.assertTrue(os.path.isfile(output))\n"
            s+="\tself.assertEqual(hashraster(output)," + str(hash(array)) + ")\n"
        if isinstance(out, OutputVector):
            layer = Sextante.getObject(filename)
            fields = layer.pendingFields()
            s+="\tlayer=sextante.getobject(output)\n"
            s+="\tfields=layer.pendingFields()\n"
            s+="\texpectednames=[" + ",".join([str(f.name()) for f in fields]) + "]\n"
            s+="\texpectedtypes=[" + ",".join([str(f.typeName()) for f in fields]) + "]\n"
            s+="\tnames=[str(f.name()) for f in fields]\n"
            s+="\ttypes=[str(f.typeName()) for f in fields]\n"
            s+="\tself.assertEqual(exceptednames, names)\n"
            s+="\tself.assertEqual(exceptedtypes, types)\n"
            features = QGisLayers.features(layer)
            numfeat = len(features)
            s+="\tfeatures=sextante.getfeatures(layer))\n"
            s+="\tself.assertEqual(" + str(numfeat) + ", len(features)\n"
            if numfeat > 0:
                feature = features.next()
                attrs = feature.attributes()
                s+="\tfeature=features.next()\n"
                s+="\tattrs=feature.attributes()\n"
                s+="\texpectedvalues=[" + ",".join([str(attr.toString()) for attr in attrs]) + "]\n"
                s+="\tvalues=[str(attr.toString()) for attr in attrs]\n"
                s+="\tself.assertEqual(exceptedtypes, types)\n"
    
    dlg = ShowTestDialog(s)
    dlg.exec_()
    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 )
Beispiel #6
0
def createTest(text):
    s = ""
    tokens = text[len("sextante.runalg("):-1].split(",")
    cmdname = tokens[0][1:-1]
    methodname = "test_" + cmdname.replace(":", "")
    s += "def " + methodname + "(self):\n"
    alg = Sextante.getAlgorithm(cmdname)
    execcommand = "sextante.runalg("
    i = 0
    for token in tokens:
        if i < alg.getVisibleParametersCount() + 1:
            if os.path.exists(token[1:-1]):
                token = os.path.basename(token[1:-1])[:-4] + "()"
            execcommand += token + ","
        else:
            execcommand += "None,"
        i += 1
    s += "\toutputs=" + execcommand[:-1] + ")\n"

    i = -1 * len(alg.outputs)
    for out in alg.outputs:
        filename = tokens[i][1:-1]
        if (tokens[i] == str(None)):
            QtGui.QMessageBox.critical(
                None, "Error",
                "Cannot create unit test for that algorithm execution.\nThe output cannot be a temporary file"
            )
            return
        s += "\toutput=outputs['" + out.name + "']\n"
        if isinstance(out, (OutputNumber, OutputString)):
            s += "self.assertTrue(" + str(out) + ", output.value)\n"
        if isinstance(out, OutputRaster):
            dataset = gdal.Open(filename, GA_ReadOnly)
            strhash = hash(str(dataset.ReadAsArray(0).tolist()))
            s += "\tself.assertTrue(os.path.isfile(output))\n"
            s += "\tdataset=gdal.Open(output, GA_ReadOnly)\n"
            s += "\tstrhash=hash(str(dataset.ReadAsArray(0).tolist()))\n"
            s += "\tself.assertEqual(strhash," + str(strhash) + ")\n"
        if isinstance(out, OutputVector):
            layer = Sextante.getObject(filename)
            fields = layer.pendingFields()
            s += "\tlayer=QGisLayers.getObjectFromUri(output, True)\n"
            s += "\tfields=layer.pendingFields()\n"
            s += "\texpectednames=[" + ",".join(
                ["'" + str(f.name()) + "'" for f in fields]) + "]\n"
            s += "\texpectedtypes=[" + ",".join(
                ["'" + str(f.typeName()) + "'" for f in fields]) + "]\n"
            s += "\tnames=[str(f.name()) for f in fields]\n"
            s += "\ttypes=[str(f.typeName()) for f in fields]\n"
            s += "\tself.assertEqual(expectednames, names)\n"
            s += "\tself.assertEqual(expectedtypes, types)\n"
            features = QGisLayers.features(layer)
            numfeat = len(features)
            s += "\tfeatures=sextante.getfeatures(layer)\n"
            s += "\tself.assertEqual(" + str(numfeat) + ", len(features))\n"
            if numfeat > 0:
                feature = features.next()
                attrs = feature.attributes()
                s += "\tfeature=features.next()\n"
                s += "\tattrs=feature.attributes()\n"
                s += "\texpectedvalues=[" + ",".join(
                    ['"' + str(attr) + '"' for attr in attrs]) + "]\n"
                s += "\tvalues=[str(attr) for attr in attrs]\n"
                s += "\tself.assertEqual(expectedvalues, values)\n"
                s += "\twkt='" + str(feature.geometry().exportToWkt()) + "'\n"
                s += "\tself.assertEqual(wkt, str(feature.geometry().exportToWkt()))"

    dlg = ShowTestDialog(s)
    dlg.exec_()