TransWikia.com

Generating an invalid vector layer in QGIS

Geographic Information Systems Asked on June 8, 2021

I need an invalid layer to test a plugin in QGIS. In the plugin window, I select a layer from combobox. So, the invalid layer should be in Layers panel. I google for a while but I cannot find a way to generate an invalid vector layer.

How can I generate one so that layer.isValid() returns False?

One Answer

  • Providing an invalid path makes it invalid: layer = QgsVectorLayer("zxcxzcxc/home/bera/GIS/Data/testdata/ak_riks.shp", "testlayer_shp", "ogr")
  • Or a providerlib that doesnt exist: layer = QgsVectorLayer("/home/bera/GIS/Data/testdata/ak_riks.shp", "testlayer_shp", "O G R"), it should be "ogr"

Then add layer to the project:

QgsProject.instance().addMapLayer(layer)

enter image description here

Correct answer by BERA on June 8, 2021

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP