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?

  • 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 Mar 6 '19 at 13:00
  • that is the correct answer – Ian Turton Mar 6 '19 at 13:05
  • Any sort of process that changes the layer features – Ian Turton Mar 6 '19 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 Mar 6 '19 at 15:33
  • @etrimaille - thanks, I spotted that soon after I got the answer here :-) – Ian Turton Mar 7 '19 at 8:02
  • To add an existing single layer:

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

  • 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]

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.