QGIShas a powerful programming interface that allows you to extend the corefunctionality of the software as well as write scripts to automate your tasks.QGIS supports the popular Python scripting language. Even if you are abeginner, learning a little bit of Python and QGIS programming interface willallow you to be much more productive in your work. This tutorial assumes noprior programming knowledge and is intended to give an introduction topython scripting in QGIS (PyQGIS).
We will load a vector point layer representing all major airports and usepython scripting to create a text file with the airport name, airport code,latitude and longitude for each of the airport in the layer.
You will see a new panel open at the bottom of QGIS canvas. You will see aprompt like >>> at the bottom where you can type commands. Forinteracting with the QGIS environment, we must use the iface variable.To access the currently active layer in QGIS, you can type the following andpress Enter. This command fetches the reference to the currentlyloaded layer and stores it in the layer variable.
There is a handy function called dir() in python that shows you allavailable methods for any object. This is useful when you are not surewhat functions are available for the object. Run the following command tosee what operations we can do on the layer variable.
You will see a long list of available functions. For now, we will use afunction called getFeatures() which will gets you the reference to allfeatures of a layer. In our case, each feature will be a point representingan airport. You can type the following command to iterate through each of thefeatures in the current layer. Make sure to add 2 spaces before typing thesecond line.
As you will see in the output, each line contains a reference to a featurewithin the layer. The reference to the feature is stored in the fvariable. We can use the f variable to access the attributes of eachfeature. Type the following to print the name and iata_code for eachairport feature.
Now we have all the pieces that we can stitch together to generate ourdesired output. Type the following code to print the name, iata_code,latitude and longitude of each of the airport features. The %s and%f notations are ways to format a string and number variables.
I would like to run a few small and simple Python scripts for QGIS (Mac) from "outside" of QGIS (e.g. Sublime Text). With outside I mean in this context, either the normal os command line (
terminal.app) or even better, directly out of Sublime Text (text-editor), but definitely not via the inbuilt QGIS Python Console.
I've read through various tutorial e.g. -cookbook/intro.html#python-applications and I am able to get a reference to the QGIS app, but unfortunately not to qgis.utils.iface or something else deeper. This little code snippet should for instance print out the name of the active layer ... here is what I have:
You can't get a reference to the iface object here because it doesn't exist in this context. The iface (QgisInterface) object is a convenience object for plugins, or scripts running inside QGIS, to access the main objects e.g. the map canvas, legend, composer, etc, and only exists when the main application is running.
If you installed QGIS using thestandalone installer, the easiest option is to remove it and install from OSGeo4W. You can run both the standalone and OSGeo4W versions on the samemachine, but you need to be extra careful not to mix up the environment.
To continue with the setup, we need to set the environment by creating a .cmdscript. The following is adapted from several sources, and trimmed down to theminimum. Copy and paste it into afile named pyqgis.cmd and save it to a convenient location (like your HOMEdirectory).
Just double-click on your pyqgis.cmd script from the Explorer or a desktopshortcut to start a cmd shell. From here you can use Python interactively andalso use pb_tool to compile and deploy your plugin for testing.
We added the start statement with the path to theIDE (in this case PyCharm). If you save this to something like pycharm.cmd, you candouble-click on it to start PyCharm. The same method works for other IDEs, suchas PyDev.
Within your IDE settings, point it to use the Python interpreter included withOSGeo4W---typically at: %OSGEO4W_ROOT%\bin\python3.exe. This will make it pick upall the QGIS goodies needed for development, completion, and debugging. In mycase OSGEO4W_ROOT is C:\OSGeo4W3, so in the IDE, the path to the correctPython interpreter would be: C:\OSGeo4W3\bin\python3.exe.
This tutorial aims to help GIS users to get started with Python programming for QGIS 3. In contrast to many tutorials out there, the idea is to not assume any previous programming knowledge.
Unlike many introductions to Python, this one does not start with an installation and setup guide. Why? Because if you have installed QGIS, you already have a working installation of Python on your system. All you need to get started is to launch QGIS and open the Python console (Plugins Python console).
And then maybe write one explaining commonly used structures/logic of scripts, such as looping through features in a layer, creating new calculated columns. This enables learners to apply pyqgis to numerous problems.
Thanks for great and friendly explanation. I have been studying GIS for quite sometime. i would like to see GIS project from scratch that shows how to create shapefiles from other Shapefiles and how to to collect GPS data and digitize it. Also vector and raster combined.
Thanks for your great turtorial ,following your steps,I have done a IDW process with a cvslayer programmaticly ,but it is gray. I can colorize the IDW result layer by specifying classified value ranges and corrsponding color in the symbology tab of Qgis layer properties form.Hower I am pretty new with both GIS and Python , I do not know how to colorize the result raster layer by python program. Can you do me a favor ,thanks.(set point color by its value,for example: 0
Hi underdark, Thank you very much for the wonderful tutorial! I am from Japan and first I looked for some tutorials like this in Japanese but I could not find any. So lucky to get to know this tutorial. I love it!
Looks and sound good, i cant wait to start the course to learn about pyQGIS. My question is are operations preformed on python console of qgis, are the codes and syntaxes the same as ArcMap python console?!
Standalone installers include everything QGIS needs in a single download. Once you have the installer, no internet is required to complete the installation. When a new release is available, you need to download the complete installer again in order to upgrade. For beginners, the standlone installer is probably the easiest way to install QGIS:
The weekly snapshots of the nightly qgis-dev package of OSGeo4W are for users that cannot use OSGeo4W (see below) for some reason or just prefer standalone installers. In the feature freeze phase, theset also act as release candidate installers.
More advanced QGIS users should use OSGeo4W packages. This installer makes it possible to install several versions of QGIS in parallel and also to do much more efficient updates as only changed components are downloaded and installed with each new release.
The OSGeo4W repository contains a lot of software from OSGeo projects. QGIS and all dependencies are included, along with Python, GRASS, GDAL, etc. The installer is able to install from internet or just download all needed packages beforehand. The downloaded files are kept in a local directory for future installations and could also be used to install offline.
The packages listed in the above table only install the necessary dependencies needed to run QGIS. Corresponding to those packages there are also meta packages with the suffix -full-free and -full. The -full-free contains additional optional dependencies that some popular (not included in the default QGIS install) plugins use. The -full includes everything from -full-free and also adds proprietary extensions like Oracle drivers, ECW and MrSID.
In the section following this one, you will find ALL possible options to install different versions of QGIS in different versions of Debian/Ubuntu. If you have problems, verify whether your distribution is still supported as the repositories also contain older unsupported distributions with the last QGIS version that was supported. Be aware that those might have meanwhile ceased to work.
For Ubuntu we also used to have extra packages in a separate repository that are based on ubuntugis, which held more up-to-date versions of other GIS packages than Ubuntu itself for LTS versions. If you want those you also need to include ubuntugis-unstable ppa in your /etc/apt/sources.list.d/qgis.list file (see ubuntugis documentation).
The release packages are only produced once, shortly after a new version has been released. As unstable, not yet released debian versions (testing) and ubuntugis-unstable can have library changes the packages might sooner or later be broken for these targets, when the development in debian, ubuntu or ubuntugis-unstable moves on and their packages used as dependencies in QGIS change. In that event you can either:
switch to a nightly repository (available for the two release branches and master) whose packages are rebuild on regular basis and will also pickup the updated dependencies automatically or
Because of the atomic nature of NixOS packages, you need to override the package if you want extra python packages to be available to QGIS. For example to run QGIS with numpy, geopandas and rasterio python libraries you can do:
The package management system MacPorts offers both the latest release version (port qgis3) and the long term version (port qgis3-ltr). This will install QGIS with native architecture, Intel x86_64 or Apple ARM. Main software dependencies such as GDAL, PDAL and GRASS GIS are usually the latest version available.
3a8082e126