Take the 2-minute tour ×
Geographic Information Systems Stack Exchange is a question and answer site for cartographers, geographers and GIS professionals. It's 100% free, no registration required.

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()
type(cLayer)

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

1 Answer 1

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 http://www.qgis.org/api/classQgsVectorLayer.html 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 http://www.qgis.org/api/classQgsMapLayer.html there you can see QgsMapLayer::LayerType

You can also see that QgsMapLayer has a type method QgsMapLayer::type() (http://www.qgis.org/api/classQgsMapLayer.html#a209afd81e5d57a7d909acab01ed608e0) 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

 
discard

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.