7

I'm making a plugin that converts vector .shp to raster .tif. This is the UI of the plugin. The ComboBox is the custom mMapLayerComboBox widget.

UI of the Plugin

  1. After selecting a loaded shapefile in QGIS, what is the type of the layer object that I'll use in the python code as a shapefile? This is the code:

enter image description here

Also, OSGEO/OGR/GDAL needs a string as an argument (the pathfile of the shapefile to be exact) as input. So ogr.Open(layer) gives an error. This is the error:

Not a String error

But running ogr.Open(layer) in Jupyter, where layer = 'datasets/input/Agriculture.shp' gives no error and runs the code properly.

So, 2. How do I get the pathfile of the shapefile from the ComboBox? (if not ComboBox, what other widgets can I use)?

1
  • Could you add the code and the error message as text instead of image? Jan 31, 2020 at 23:17

1 Answer 1

7

In this case, layer is an instance of QgsVectorLayer, because currentLayer gives you that. But ogr.Open() needs a path. So you need to get layer source to pass to ogr.Open(). QgsVectorLayer class has source method which gives you full path of the layer source.

Use in that way:

layer = self.dlg.mMapLayerComboBox.currentLayer()
layer_path = layer.source()

dataSource = ogr.Open(layer_path)
ogr_layer = dataSource.GetLayer()
2
  • Yes, this works, but mMapLayerComboBox doesn't have source as a method, an error came when I used. used normal comboBox with which it now works. Thanks a lot! Feb 1, 2020 at 17:41
  • 1
    QgsMapLayerComboBox doesn't have source method. It's QgsMapLayer's method. mMapLayerComboBox.currentLayer() returns a QgsMapLayer instance (means layer). Feb 1, 2020 at 19:40

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.