5

I have two layers(layer1,layer2) in postgis database where I have connect in QGIS project.

my database have specific precision.

now I want to use from QGIS processing some algorithms like GISDIFFERENCE(for example I know tew postgis have query for this).

but I don't know how to import the algorithm output in postgis database automatic to keep my database precision.

for example if I try to run graphical this tool show me choose to save as to postgis database.

but how to do this using pyqgis ?because I want to create a QGIS plugin

some way is to run algorithms and I save manual the output to shapefile and after to import this shapefile to postgis database like this :

outputs_QGISDIFFERENCE_1=processing.runalg('qgis:difference', layer1,layer2,False,None)
load= QgsVectorLayer(outputs_QGISDIFFERENCE_1['OUTPUT'],'myname', 'ogr')
 QgsMapLayerRegistry.instance().addMapLayer(load)

and save to postgis database.

but that way create it me topology error because I loose the database precision if I use the graphical way to save as to postgis database then that layer is correct.

but how to do this using pyqgis ?

0

Consider using something like this:

def updateOriginalLayer(self, pgInputLayer, qgisOutputVector):
        """
        Updates the original posts layer using the processing output layer
        pgInputLyr: postgis input layer
        qgisOutputVector: qgis processing output layer
        """
        provider = pgInputLayer.dataProvider()
        # getting keyColumn because we want to be generic
        uri = QgsDataSourceURI(pgInputLayer.dataProvider().dataSourceUri())
        keyColumn = uri.keyColumn()
        # starting edition mode
        pgInputLayer.startEditing()
        addList = []
        idsToRemove = []
        #making the changes and inserts
        for feature in pgInputLayer.getFeatures():
            id = feature.id()
            outFeats = []
            #getting the output features with the specific id
            if qgisOutputVector:
                for gf in qgisOutputVector.dataProvider().getFeatures(QgsFeatureRequest(QgsExpression("{0}={1}".format(keyColumn, id)))):
                    outFeats.append(gf)
            #starting to make changes
            for i in range(len(outFeats)):
                if i == 0:
                    #let's update this feature
                    newGeom = outFeats[i].geometry()
                    newGeom.convertToMultiType()
                    feature.setGeometry(newGeom)
                    pgInputLayer.updateFeature(feature)
                else:
                    #for the rest, let's add them
                    newFeat = QgsFeature(feature)
                    newGeom = outFeats[i].geometry()
                    newFeat.setGeometry(newGeom)
                    idx = newFeat.fieldNameIndex(keyColumn)
                    newFeat.setAttribute(idx, provider.defaultValue(idx))
                    addList.append(newFeat)
            #in the case we don't find features in the output we should mark them to be removed
            if len(outFeats) == 0:
                idsToRemove.append(id)
        #pushing the changes into the edit buffer
        pgInputLayer.addFeatures(addList, True)
        #removing features from the layer
        pgInputLayer.deleteFeatures(idsToRemove)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.