Sign In Start Free Trial
Account

Add to playlist

Create a Playlist

Modal Close icon
You need to login to use this feature.
  • Book Overview & Buying QGIS Python Programming Cookbook
  • Table Of Contents Toc
QGIS Python Programming Cookbook

QGIS Python Programming Cookbook

By : Joel Lawhead
3.8 (6)
close
close
QGIS Python Programming Cookbook

QGIS Python Programming Cookbook

3.8 (6)
By: Joel Lawhead

Overview of this book

If you are a geospatial analyst who wants to learn more about automating everyday GIS tasks or a programmer who is responsible for building GIS applications,this book is for you. The short, reusable recipes make concepts easy to understand. You can build larger applications that are easy to maintain when they are put together.
Table of Contents (11 chapters)
close
close
10
Index

Installing QGIS for development

QGIS has a set of Python modules and libraries that can be accessed from the Python console within QGIS. However, they can also be accessed from outside QGIS to write standalone applications. First, you must make sure that PyQGIS is installed for your platform, and then set up some required system environment variables.

In this recipe, we will walk you through the additional steps required beyond the normal QGIS installation to prepare your system for development. The steps for each platform are provided, which also include the different styles of Linux package managers.

Getting ready

QGIS uses slightly different installation methods for Windows, GNU/Linux, and Mac OS X. The Windows installers install everything you need for Python development, including Python itself.

However, on Linux distributions and Mac OS X, you may need to manually install the Python modules for the system installation of Python. On Mac OS X, you can download installers for some of the commonly used Python modules with QGIS from http://www.kyngchaos.com/software/python.

How to do it

On Linux, you have the option to compile from the source or you can just specify the Python QGIS interface to be installed through your package manager.

Installing PyQGIS using the Debian package manager

  1. For Linux distributions based on the Debian Linux package manager, which includes Ubuntu and Debian, use the following command in a shell:
    sudo apt-get update
    
  2. Next, install the QGIS, PyQGIS, and QGIS GRASS plugins:
    sudo apt-get install qgis python-qgis qgis-plugin-grass
    

Installing PyQGIS using the RPM package manager

  1. For Linux distributions based on the Red Hat Package Manager (RPM), first update the package manager, as follows:
    sudo yum update
    
  2. Then, install the packages for the QGIS, PyQGIS, and QGIS GRASS plugins:
    sudo yum install qgis qgis-python qgis-grass
    

Setting the environment variables

Now, we must set the PYTHONPATH to the PyQGIS directory. At the same time, append the path to this directory to the PATH variable so that you can use the PyQGIS modules with an external IDE.

Setting the environment variables on Windows

  1. Set the PYTHONPATH variable in a command prompt to the bin directory of the QGIS installation:
    set PYTHONPATH="C:\Program Files\QGIS Brighton\bin"
    
  2. Next, append QGIS's bin directories to the system's PATH variable:
    set PATH="C:\Program Files\QGIS Brighton\bin";"C:\Program Files\QGIS Brighton\bin\apps\qgis\bin";%PATH%
    

Setting the environment variables on Linux

  1. Set the PYTHONPATH variable in a command prompt to the bin directory of the QGIS installation:
    export PYTHONPATH=/usr/share/qgis/python
    
  2. Now, append the QGIS shared library directory to the runtime search path. Note that this location can vary depending on your particular system configuration:
    export LD_LIBRARY_PATH=/usr/share/qgis/python
    

How it works…

The QGIS installation process and package managers set up the Python module's configuration internal to QGIS. When you use the Python console inside QGIS, it knows where all the PyQGIS modules are. However, if you want to use the PyQGIS API outside QGIS, using a system Python installation on Windows or Linux, it is necessary to set some system variables so that Python can find the required PyQGIS modules.

There's more…

This recipe uses the default QGIS paths on each platform. If you aren't sure which PyQGIS path is for your system, you can figure this out from the Python console in QGIS.

Finding the PyQGIS path on Windows

The libraries on Windows are stored in a different location than in the case of other platforms. To locate the path, you can check the current working directory of the Python console:

  1. Start QGIS.
  2. Select Python Console from the Plugins menu, which appears in the lower-right corner of the QGIS application window, as shown in the following screenshot:
    Finding the PyQGIS path on Windows
  3. Use the os module to get the current working directory:
    import os
    os.getcwd()
    
  4. Verify that the current working directory of the Python console is returned.

Finding the location of the QGIS Python installation on other platforms

Perform the following steps to find the path needed for this recipe on all the platforms besides Windows:

  1. Start QGIS.
  2. Start the QGIS Python Console.
  3. Use the sys module to locate the PyQGIS path:
    import sys
    sys.path
    
  4. Python will return a list of paths.
  5. Find the path that ends in /python, which is the location of the Python installation used by QGIS
CONTINUE READING
83
Tech Concepts
36
Programming languages
73
Tech Tools
Icon Unlimited access to the largest independent learning library in tech of over 8,000 expert-authored tech books and videos.
Icon Innovative learning tools, including AI book assistants, code context explainers, and text-to-speech.
Icon 50+ new titles added per month and exclusive early access to books as they are being written.
QGIS Python Programming Cookbook
notes
bookmark Notes and Bookmarks search Search in title playlist Add to playlist font-size Font size

Change the font size

margin-width Margin width

Change margin width

day-mode Day/Sepia/Night Modes

Change background colour

Close icon Search
Country selected

Close icon Your notes and bookmarks

Confirmation

Modal Close icon
claim successful

Buy this book with your credits?

Modal Close icon
Are you sure you want to buy this book with one of your credits?
Close
YES, BUY

Submit Your Feedback

Modal Close icon
Modal Close icon
Modal Close icon