4

I want to check which type of geometry the current layer has. Therefore I use the PyQGIS documentation on Access to Geometry and my code looks like:

if iface.mapCanvas().currentLayer().wkbType() == QGis.WKBPoint:
    print True

If I run this code I get following error:

Traceback (most recent call last):
File "<input>", line 1, in <module>
NameError: name 'Qgis' is not defined

I can't import a QGis class because I get an error that it does not exist. Are there steps missing or how can I use this QGis class?

4

I think you need to add qgis.core module as the QGis.WkbType belongs to it:

from qgis.core import *

I tested this on a script and it worked for me:

##Test=name

from qgis.core import *
from qgis.utils import *

layer = iface.mapCanvas().currentLayer().wkbType() == QGis.WKBPoint
print layer

The script prints a True/False statement in the Python Console.

Hope this helps!


Edit:

From the Python Console, you can type the following to see some help descriptions:

>>> help (QGis.WkbType)
Help on class WkbType in module qgis._core:
  • 1
    I noticed that you're using QGIS 2.8, I tested this on 2.6.1 so not completely sure if my post will work for you. – Joseph Mar 17 '15 at 15:56
  • 4
    Just wanted to add that if one only needs to access to a few classes/enums, the recommended way of importing them is, e.g., from qgis.core import QGis. When you import everything (i.e., using the asterisk), you are importing lots of classes you don't really need, cluttering your environment. See docs.python.org/2/faq/… – Germán Carrillo Mar 17 '15 at 17:36
  • 1
    Thank you @Joseph. I didn't know the help funktion ;). But now it works for me. It is a bit strange that I have to import it even in the QGIS Python console... – Martin Mar 18 '15 at 7:58
  • 1
    @gcarrillo - Thank you, that is a very good tip. One which I also need to do more often (I have a bad habit of being lazy!) – Joseph Mar 19 '15 at 10:11
  • 1
    @Martin - Most welcome buddy! That is indeed strange because in QGIS 2.6.1, there's no need to import that class at all! But glad it's working for you with help from gcarrillo. – Joseph Mar 19 '15 at 10:15

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.