5

I am trying to edit a plugin to work in QGIS 3.0 and want to check for layer is polygon.

for layer in layers:  
    if layer.type() == QgsMapLayer.VectorLayer and layer.wkbType() == Qgis.Polygon:  
        self.dlg.comboBox.addItem( layer.name(), layer )

Its throwing error

"if layer.type() == QgsMapLayer.VectorLayer and layer.wkbType() == Qgis.Polygon : AttributeError: type object 'Qgis' has no attribute 'Polygon'"

How can I use WKBType in QGIS 3.0?

  • 1
    I suggest you to use layer.geometryType() == QgsWkbTypes.PolygonGeometry, which is a short way to handle all possible polygon geometries (e.g., Polygon, PolygonZ, PolygonM, PolygonZM, MultiPolygon, MultiPolygonZ, and so on). – Germán Carrillo Sep 22 '17 at 12:42
  • @GermánCarrillo - I think you should post your comment as an answer :) – Joseph Sep 22 '17 at 13:09
  • 1
    I think that would depend on what @sachin-kumar actually needs, but Ok, I'll do it :) – Germán Carrillo Sep 22 '17 at 13:15
6

As you can see here, the enum QGis.WkbType becomes QgsWkbTypes.Type in QGIS v3.

So, a valid statement would be:

layer.wkbType() == QgsWkbTypes.Polygon

However, that would not be useful if you have, for instance, multi polygons.

Therefore, I suggest you to use:

layer.geometryType() == QgsWkbTypes.PolygonGeometry

which is a short way to handle all possible polygon geometries (e.g., Polygon, PolygonZ, PolygonM, PolygonZM, MultiPolygon, MultiPolygonZ, and so on).

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.