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

Matt
  • 115
  • 6
Steven Lutz
  • 467
  • 1
  • 6
  • 16

3 Answers3

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

Matt
  • 115
  • 6
PCamargo
  • 584
  • 6
  • 26
  • 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? – banbar Jul 17 '17 at 11:57
  • 1
    These are correct. The complete list of existing types is here: https://qgis.org/api/qgswkbtypes_8h_source.html (starting on line 68). – PCamargo Jul 17 '17 at 21:32
4

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
Mustafa Uçar
  • 442
  • 1
  • 6
  • 18
gui3
  • 1,711
  • 14
  • 30
1

QgsGeometry has the method wkbType that returns what you want.

lcoandrade
  • 151
  • 5