2

I'm fairly new to GIS.

I'm using QGIS to overlay a vector grid on my map and getting data on different attributes within each box of the grid. I need to do this for different grid sizes hence there will be some repetition. So to make this efficient I wanted to write a PyQGIS script to run a loop to do this. However, I'm new PyQGIS too and am not familiar with the commands (or the library itself).

Is there a way to get the PyQGIS code when I implement something from the QGIS GUI?

In case someone is familiar with STATA, I'm looking for something analogous to getting the STATA code when you use the point-and-click method to run a command.

3

When you are fairly new to both GIS and Python it might be a good idea to use the graphical modeler(menue "processing") first to get a rough idea. Here you can easily combine tasks together to get a running model which can be used like a normal tool.

The biggest differences to a direct python-script are the missing base-python functions and other imported modules as the modeler connects only existing QGIS tools. And second that you can´t run the model stand-alone without having qgis running and starting it manually. But for most use cases it is enough to automate work flows.

If you then want to get on further you can export the model as a python script to get the syntax of the used tools. But for a working stand-alone script there is much more to do, which can be found on this site in many posts.

3

Great answer by @Matte! Just to add that if you use the tools from the Processing Toolbox, you can see the PyQGIS equivalent from the menubar:

Processing > History

I just tested the Vector grid algorithm, filled with some parameters such as extent, grid sizes and set the output as a temporary file. The following is what is shown in the history:

processing.runalg("qgis:vectorgrid","-1.93529228872,1.41133755225,-1.13153276241,1.64027369706",100,100,0,None)

Just remember that you will need to add import processing when running your script from the Python Console. If you create and run your script from the Processing Toolbox, you will not need to import this module.


Tested on QGIS 2.16.1-Nødebo for Windows 7 64-bit.

  • @Sam - Most welcome, glad it helped =) – Joseph Nov 10 '16 at 10:09

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.