1

I'm creating a memory layer in PyQGIS and I need to match the geometry of the memory layer that I am building to the geometry of another layer.

After some investigation it seems to me that, as per QGIS 2.2 Documentation and the QGIS 2.2 API Documentation, the URI defining the geometry type approximately matches the QGis::WkbType member enumeration text.

I know I can easily query the WKBType of my input layer with

myWkbType = mylayer.dataProvider().geometryType()

However, this returns an integer, whereas the constructor requires text. Is there a simple built-in PyQGIS way to turn these constants into text, or do I need to build a hacky little reverse mapping function for it?

3 Answers 3

2

Since QGIS 3.0 you can use the displayString and geometryDisplayString functions:

>>> QgsWkbTypes.displayString(layer.wkbType())
'MultiSurface'

>>> QgsWkbTypes.geometryDisplayString(layer.geometryType())
'Polygon'
1

QGis.WkbType is a sip wrapped enum class which doesn't have a reverse mapping function like it would if it were a native python enum.

So the answer i guess would be either to wrap the class in another class with the desired python enum attributes or to write a hacky little lookup function.

Rather than all the drama of writing a wrapping class - here's a stand alone dictionary and reverse map:

my_WkbType = { 'WKBUnknown': 0, 'WKBPoint':1, 'WKBLineString':2, 'WKBPolygon':3, 'WKBMultiPoint':4, 'WKBMultiLineString':5, 'WKBMultiPolygon':6, 'WKBNoGeometry':7, 'WKBPoint25D':8, 'WKBLineString25D':9, 'WKBPolygon25D':10, 'WKBMultiPoint25D':11, 'WKBMultiLineString25D':12, 'WKBMultiPolygon25D':13 }

my_rev_WkbType = {v:k for k, v in my_WkbType.items()}

To test it you can try:

my_rev_WkbType[QGis.WKBPolygon]

Or in the context of the question:

my_rev_WkbType[mylayer.dataProvider().geometryType()]
1
  • qgis 3 has the following function: QgsWkbTypes.geometryDisplayString(int)
    – Mr Purple
    Jun 8, 2019 at 5:04
0

this post is quite old, just stumbled upon it. To get a text representation to all available WkbTypes, i ended up using this:

dict([(v, k) for k, v in QgsWkbTypes.__dict__.items() if isinstance(v, int)])
1
  • Nice: much less hacky. Any chance of adding the line which would return the text for a memory layer based on the input layer as in my answer. If so I'll change to your answer as accepted.
    – Mr Purple
    Oct 28, 2018 at 0:26

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.