On the python console in QGIS what is the difference between the following two code snippets (activeLayer is a polygon layer):

# 1)
>>> 3

# 2)
f = iface.activeLayer().selectedFeatures()[0]
>>> 2

In case 1) the result is '3', in case 2) the result is '2', and moreover in QGIS 2.12 using 'asPolygon()' instead of 'type()' case 2) gives as list of qPoints as expected while in case 1) QGIS says goodbye with a 'Runtime Error! ... R6025 - pure virtual function call' (in QGIS 2.8 results in an empty list).

I expected the same results.


In my understanding, it's because although it seems to be pure Python, the PyQGIS API is backed with C++. Python is a wrapper on the top.

For this reason in case 1, the iface.activeLayer().selectedFeatures()[0].geometry() was already destroyed (C++ backed and already cleaned to avoid memory consumption) and you get an UnknownGeometry or a crash.

In case 2, assigning to a variable f, the feature avoids the cleaning and you get the expected result.

Weird but it's how it works.


First expression is not acceptable because 3 is the constant code for an 'UnknownGeometry'. At the Python Console you can corroborate it:


Complete list of the enumerate types can be seen here. For this reason, when you use the 'asPolygon' geometry method, in this case, it results in an empty list.

Second snipped code produces number 2 geometry type, that is the assigned integer number for a polygon geometry. In this case, when it is used the 'asPolygon' geometry method it works as expected.

  • 3
    I think he wants to know why the dot notation doesn't work here. For geometry() to return the correct result, the feature has to be assigned first to the variable f. – Detlev Dec 10 '15 at 17:42
  • 2
    That's right, as far as i'm experienced with python, both should deliver the same result. – Jochen Schwarze Dec 11 '15 at 8:02

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.