1

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.

Edited 2019/09/16

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}
  • Can you share more detail from your code? – pnz Sep 12 at 18:32
  • Maybe try without 'True': QgsProject.instance().addMapLayer(vl_layerPG) – pnz Sep 12 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 at 9:27

Your Answer

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

Browse other questions tagged or ask your own question.