4

I'm trying to add some layers created during a process to a project.

There seems nothing to be added (TOC empty) but when I close QGIS it says:

This project includes one or more layers as temporary draft. This layers are not saved at the disc and...

Or something like that (just my own translation)

I defined the layer as:

vl_layerPG = QgsVectorLayer("Polygon", "vl_layerPG", "memory")

Filled with some data and then:

QgsProject.instance().addMapLayer(vl_layerPG, True)

But this is not added to the TOC, almost visible.

I tried without True first as shown at the PyQGIS cookbook, but It was the same result.

def processAlgorithm(self, parameters, context, feedback):
    dxf_file = self.parameterAsString(parameters, self.INPUT_FILE, context)

    current = 0       
    #self.generaCapaPlantaGeneral(datos)
    
    driverIn = ogr.GetDriverByName('DXF')
    dataSource = driverIn.Open(dxf_file, 0)
    dataSource.ExecuteSQL("SELECT * FROM entities WHERE Layer = 'PG-LP'")
    layerIn = dataSource.GetLayer()        
    n_records = layerIn.GetFeatureCount()    
    inFeature = layerIn.GetNextFeature()
    
    geometryPoly = ogr.BuildPolygonFromEdges(ogr.ForceToMultiLineString(inFeature.GetGeometryRef()), dfTolerance = 10)
    geometryPolyWkt = geometryPoly.ExportToWkt()
                   
    ml = QgsVectorLayer("Polygon", "PG_layer", "memory")
    pr = ml.dataProvider()
    pr.addAttributes([
        QgsField("ref",QVariant.String),
    ])
    
    attrib = []
    attrib.append(46)
    feat = QgsFeature()
    feat.setAttributes(attrib)
    feat.setGeometry(QgsGeometry.fromWkt(geometryPolyWkt))
    
    pr.addFeatures([feat])
    ml.updateFields()
    
    QgsProject.instance().addMapLayer(ml)
    ml.updateExtents()        
            
    return {self.MEM_OUTPUT:ml}
5
  • Can you share more detail from your code?
    – pnz1337
    Sep 12, 2019 at 18:32
  • Maybe try without 'True': QgsProject.instance().addMapLayer(vl_layerPG)
    – pnz1337
    Sep 12, 2019 at 18:32
  • @pnz I edited the question adding the code. I'm a little stuck on that, because if I tried the command QgsProject.instance().addMapLayer(ml) at the python terminal It works as expected.
    – Javier G
    Sep 20, 2019 at 9:27
  • Hi, did you find a way to import your layers in processing?
    – aurel_nc
    Nov 25, 2020 at 5:21
  • No yet, there's the same behaviour.
    – Javier G
    Nov 26, 2020 at 11:40

1 Answer 1

0

Finally I founded a solution:

I read that is not possible to add a new layer from processAlgorithm anymore.

But you can add then after, at the postProcessAlgorithm

def postProcessAlgorithm(self, context, feedback):
    QgsProject.instance().addMapLayer(self.ml)
2
  • I didn't test, but I think that ml in processAlgorithm have to be self.ml (I mean self.ml = QgsVecto....) for this to work in postProcessAlgorithm. Oct 27, 2021 at 8:13
  • Yes, that ml must be self.ml
    – Javier G
    Oct 29, 2021 at 12:12

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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