Geographic Information Systems Stack Exchange is a question and answer site for cartographers, geographers and GIS professionals. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I am not sure how to use the API documentation to code in Python. For example, I want to determine whether a layer is a point, line or polygon layer and found the following in the API documentation:

enter image description here

I have selected the lakes shapefile from the QGIS alaska dataset. In the QGis Python console, I have typed:

from PyQt4.QtCore import *
from PyQt4.QtGui import *
from qgis.core import *
canvas = self.iface.mapCanvas()
cLayer = canvas.currentLayer()

The returned value is: <class 'qgis.core.QgsVectorLayer'>.

How do I use the above information in the API documentation to (1) determine the type of the selected layer and (2) return the value of the listed enumerate types (e.g. polygon)? Whilst I am sure someone can tell me the answer, I would really like to know how to work out the answer using the API documentation and any other available documentation.

share|improve this question

QgsLayerItem isn't what you want for this. That is the item that is shown in the QGIS browser widget.

What you want to do is look up QgsVectorLayer which lands you here that is all the methods on the vector layer, however QgsVectorLayer is inherited from QgsMapLayer. You can see this by expanding the Inheritance diagram for QgsVectorLayer: section. Clicking on QgsMapLayer takes you too there you can see QgsMapLayer::LayerType

You can also see that QgsMapLayer has a type method QgsMapLayer::type() ( you can see this returns QgsMapLayer::LayerType. Because QgsVectorLayer and QgsRasterLayer inherit from QgsMapLayer they both have the type() method.

So now we can do:

layer.type() == QgsMapLayer.VectorLayer

to check if it's a vector layer.

If you click on the return type it will also take you to the docs for that type

enter image description here

share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

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