9

I'm looking for some resources for learning PyQGIS.

It would be interesting having a collection of books or websites that provide some practical examples for learning the syntax or accomplishing specific tasks.

Ideally, these resources should give a general guidance for both beginner and experienced users.

Where to find QGIS tutorials and web resources? is a very similar question, but it gives help for learning QGIS, and not specifically PyQGIS (in fact, it doesn't have the PyQGIS tag).

Any help?

closed as too broad by PolyGeo Jul 26 '17 at 23:04

Please edit the question to limit it to a specific problem with enough detail to identify an adequate answer. Avoid asking multiple distinct questions at once. See the How to Ask page for help clarifying this question. If this question can be reworded to fit the rules in the help center, please edit the question.

  • Can you post some code to illustrate your problem. It indeed take some effort to get into pyqgis. Your Best options are the cookbook and the c++ api. – Peter Peterson Sep 18 '14 at 17:58
  • 1
  • 1
    To me this question seems a duplicate of that one. I think you could post your answer there :) – Germán Carrillo Jul 26 '17 at 15:52
  • 1
    Normally, I would say that the newer question (which is too broad for focussed Q&A) should be made a duplicate of the older one to discourage such questions being asked. However, in this case it will take much less of our volunteered effort to merge the older one into this one as a locked wiki so that your effort writing it is not lost. – PolyGeo Jul 26 '17 at 22:56
  • 1
    Focused questions by beginners should provide focused answers for them. – PolyGeo Jul 26 '17 at 23:24
8

The following documentation resources should ease your PyQGIS development experience:

A diagram of the main PyQGIS relationships (by Thomas Gratier): https://raw.githubusercontent.com/webgeodatavore/qgis-class-diagram/master/diagramme_principal.png

Searchable documentation of PyQGIS classes (by SourcePole): http://geoapis.sourcepole.com

For example, the addFeature method you had problems with is described here: http://geoapis.sourcepole.com/qgispyapi/qgsvectorlayer#QgsVectorLayer.addFeature

enter image description here

As a second example, the setAttribute method is described here: http://geoapis.sourcepole.com/qgispyapi/qgsfeature#QgsFeature.setAttribute

enter image description here

  • geoapis.sourcepole.com has amazing usability, however it was not updated since QGIS 2.8. Is there effort to maintain this, or documentation how this website was built? – Norwin Nov 11 '18 at 13:57
  • I really don't know, but now you have the official PyQGIS API docs: qgis.org/pyqgis/master So, I think SourcePole wouldn't spend time updating their PyQGIS website. That would be duplication of efforts. – Germán Carrillo Nov 11 '18 at 19:37
7

The following resources give a general guidance for learning or using PyQGIS and generally assume a minimum proficiency of working with Python.


QGIS 3.x versions

  • PyQGIS 3 API Documentation: official documentation of the Python API. Documentation for each major release since v3.0 as well as the nightly version is provided;

  • PyQGIS Developer Cookbook: written for QGIS 2.x it is gradually updated to 3.x. It still may be helpful as a tutorial and a reference guide and gives a good overview of the principal functionalities.


QGIS 2.x versions

PyQGIS Documentation:

  • PyQGIS Developer Cookbook: official introduction to PyQGIS programming. It is intended to work both as a tutorial and a reference guide and gives a good overview of the principal functionalities;

  • PyQGIS API Documentation: inofficial documentation of the Python API by SourcePole. It provides a searchable interface, but was not updated since QGIS 2.8;

  • QGIS C++ API Documentation: official C++ API documentation. While describing the C++ API, it can be useful for pyqgis development.

Online books:

Tutorials / Blogs / Web resources:

  • Nathan Woodrow: a blog mostly about QGIS stuff that also treats specific topics about the using of PyQGIS. The author is one of the most active QGIS developers;

  • nyalldawson.net: a blog with several posts about the using of PyQGIS. The author is one of the most active QGIS developers;

  • "How To" in QGIS: the site provides some suggestions for solving problems using PyQGIS. When possible, these tips are offered through simple code samples. I'm the author of this blog;

  • QGIS Tutorials and Tips: a section of this blog provides a series of tutorials for learning PyQGIS scripting. The author is a very experienced GIS specialist;

  • Lutra Consulting: a list of posts, having the PyQGIS tag, that cover some topics about PyQGIS.

  • 2
    ANy updates here? QGIS3 is close to release I believe. – MartinT Dec 12 '17 at 23:40
  • Does anyone happen to have an idea, when/if any of the above mentioned books (2.x) might be updated to 3.x? – Ville Koivisto Feb 19 at 12:45
  • Sherman's title has been updated for PyQGIS3 (The PyQGIS Programmer's Guide: Extending QGIS 3 with Python 3). – Ville Koivisto Aug 8 at 7:18
1

Not sure what you mean in Getting into Python API of QGIS?, but there is an addFeatures() function of course. This works in QGIS 2.4:

mem_layer = QgsVectorLayer("Polygon?crs=epsg:4326&field=MYNUM:integer&field=MYTXT:string", "temp_layer", "memory")

if not mem_layer.isValid(): raise Exception("Failed to create memory layer")

mem_layer_provider = mem_layer.dataProvider()

my_polygon = QgsFeature()
my_polygon.setGeometry(QgsGeometry.fromRect(QgsRectangle(16,48,17,49)))
my_polygon.setAttributes([10,"hello world"])

mem_layer_provider.addFeatures([my_polygon])

mem_layer.updateExtents()
QgsMapLayerRegistry.instance().addMapLayer(mem_layer)

If you are unhappy with the API docs and the PyQGIS Cookbook, your last chance is to look into working plugins - they are open source after all - and you can easily see if they work in 2.4 ...

To improve documentation, the project happily accepts resources of any kind.

  • 1
    even layer.addFeature() if you want. – Nathan W Sep 18 '14 at 22:42
  • My bad. It was the setAttribute-function that has thrown the error- I guess, I wasn't too mindful after searching. – Florian R. Klein Sep 19 '14 at 7:00
  • Looking at the code of existing plugins, is how I learned pyqgis. – warrieka Sep 19 '14 at 7:52

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