How to get shapefile geometry type in PyQGIS?

Steven Lutz picture Steven Lutz · Aug 12, 2014 · Viewed 7.8k times · Source

I'm writing a script that is dependent on knowing the geometry type of the loaded shapefile. but I've looked in the pyqgis cookbook and API and can't figure out how to call it.

infact, I have trouble interpreting the API, so any light shed on that subject would be appreciated.

Thank you

Answer

PCamargo picture PCamargo · Aug 14, 2014

The command is simple:

layer=qgis.utils.iface.mapCanvas().currentLayer()

if layer.wkbType()==QGis.WKBPoint:
    print 'Layer is a point layer'

if layer.wkbType()==QGis.WKBLineString:
    print 'Layer is a line layer'

if layer.wkbType()==QGis.WKBPolygon:
    print 'Layer is a polygon layer'

if layer.wkbType()==QGis.WKBMultiPolygon:
    print 'Layer is a multi-polygon layer'

if layer.wkbType()==100:
    print 'Layer is a data-only layer'

You can use numbers (1,2,3,4) instead of the QGis.WKB***** syntax, but the way described above yields a more readable code.

The actual reference in the cookbook is here: http://docs.qgis.org/testing/en/docs/pyqgis_developer_cookbook/geometry.html