4

I know I can pull a vector file into my current project using:

vlayer = iface.addVectorLayer(r"S:/Data/Natural_Earth_quick_start/110m_cultural/ne_110m_admin_0_countries.shp", "admin", "ogr")

But if I create my layer using something like:

vlayer = QgsVectorLayer(r"S:/Data/Natural_Earth_quick_start/110m_cultural/ne_110m_admin_0_countries.shp", "admin", "ogr")

and then I do some processing on vlayer how do I then add it to the current project or map layers list?

5
  • 1
    Can you expand on what you mean by "processing"? Editing the layer such as modifying features, running a tool from the processing toolbox etc? Or do you mean something like: QgsProject.instance().addMapLayer(vlayer)?
    – Joseph
    Commented Mar 6, 2019 at 13:00
  • that is the correct answer
    – Ian Turton
    Commented Mar 6, 2019 at 13:05
  • Any sort of process that changes the layer features
    – Ian Turton
    Commented Mar 6, 2019 at 13:05
  • You can see in the PyQGIS cookbook how to add a QgsVectorLayer to the project docs.qgis.org/testing/en/docs/pyqgis_developer_cookbook/…
    – etrimaille
    Commented Mar 6, 2019 at 15:33
  • @etrimaille - thanks, I spotted that soon after I got the answer here :-)
    – Ian Turton
    Commented Mar 7, 2019 at 8:02

1 Answer 1

4
  • To add an existing single layer:

    vlayer = QgsVectorLayer("some/path/to/shapefile.shp", "admin", "ogr")
    QgsProject.instance().addMapLayer(vlayer)
    

  • To add existing multiple layers:

    vlayer1 = QgsVectorLayer("some/path/to/shapefile1.shp", "admin1", "ogr")
    vlayer2 = QgsVectorLayer("some/path/to/shapefile2.shp", "admin2", "ogr")
    layers = [vlayer1 , vlayer2]
    QgsProject.instance().addMapLayers(layers)
    

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.