How to get shapefile geometry type in PyQGIS?
Asked Answered
G

3

7

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

Gabrila answered 12/8, 2014 at 21:3 Comment(0)
A
10

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

Aminoplast answered 14/8, 2014 at 17:29 Comment(2)
I have some shapefiles that are obtained from ArcMap. When I print their wkbType(), they are all numbers like 3001, 3002, 3003 and even -2147483645, -2147483646 and -2147483647 but some are indeed 1 (corresponding to point) and 3 (corresponding to a polygon). How come such an outcome had happened? Is it because of the conversion or something else?Transit
These are correct. The complete list of existing types is here: qgis.org/api/qgswkbtypes_8h_source.html (starting on line 68).Aminoplast
A
5

Looking for a way to have the geometry type in string and after a lot LOT of searching, finally found a clean method in the docs :

geomTypeString=qgis.core.QgsWkbTypes.displayString(int(layer.wkbType()))

that will give 'Point','LineString','Polygon','MultiPoint'.... and it "knows" all of the geometry types in Qgis.

For my purpose I still had some trouble with the 'Point25D' and other strange types so added this to restrict it to the flat ones (Point,Line,Poly)

geomFlatTypeString=qgis.core.QgsWkbTypes.displayString(int(
    qgis.core.QgsWkbTypes.flatType(int(in_layer.wkbType()))))

For Info, the aim was a method that creates a memory layer duplicate of a layer whatever the type is, here is the full code:

def copyLayer(in_layer,condition=None):
    #condition=function to test features and return True or False______
    if condition==None:
        def condition(f):
            return True
    typeGeom=qgis.core.QgsWkbTypes.displayString(int(
        qgis.core.QgsWkbTypes.flatType(int(in_layer.wkbType()))))
    crsId=in_layer.crs().authid()
    out_layer=QgsVectorLayer(typeGeom+"?crs="+crsId,
                             in_layer.name()+"_copie",
                             "memory")
    fields=in_layer.dataProvider().fields().toList()
    out_layer.dataProvider().addAttributes(fields)
    out_layer.updateFields()
    features=[f for f in in_layer.getFeatures() if condition(f)]
    out_layer.dataProvider().addFeatures(features)
    return out_layer
Apart answered 22/8, 2017 at 12:47 Comment(1)
in QGIS 3 seems to be QgsWkbTypes (case slightly differs).Serial
S
1

QgsGeometry has the method wkbType that returns what you want.

Stipitate answered 12/4, 2015 at 2:47 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.