كتاب دليل المبرمجين لتعلم بايثون للمبتدئين The PyQGIS Programmers Guide




 book is not a Python tutorial—we’ll give you a short introduction to concepts needed to get started with PyQGIS. As you get into it, you’ll likely want to dig a little deeper into Python to improve your skills. We won’t talk much about flow control, variables, operators, and all the other things you need to know to use the language. For that, see the list of additional resources at the end of the chapter.

Getting Help

We already saw an example of using the help function to produce a nicely formatted description of the QgisInterface class. This works both from the command line and in the QGIS Python Console. Another helpful function is dir, which provides a list of all the methods and functions for an object. Although terse, this can be helpful if you have forgotten the name of a function.

Navigating the QGIS API

Before we can really get into PyQGIS programming, we need a basic understanding of how to use both the QGIS and Qt API documentation. Since there are over 500 QGIS classes listed in the documentation, we obviously won’t be going through them all. In this chapter we’ll try to give you a broad overview, as well as specifics on how to decipher the documentation. 

As we get into it, you’ll notice the documentation is not Pythonic—it’s C++. Return values, arguments, and definitions are based on C/C++ types and terminology. Sometimes this presents a bit of a problem when trying to interpret a particular class or method, but we’ll work to make sense of it.

Finding the Documentation

The QGIS API documentation can be found at: http://loc8.cc/ppg/ api. The documentation for Qt can be found at either http://qt-project. org or or http://loc8.cc/ppg/pyqt_classes. We’ll primarily use the latter since it is specific to classes and methods supported by PyQt, however the official Qt documentation can also be quite useful as it often contains expanded explanations and examples.
The QGIS API documentation is C++ true centric, meaning all the types you’ll see are C++ types. Don’t worry, there are direct equivalents in Python that we’ll point out.

First you’ll notice the types specified are QString and bool. QString is a Qt class and bool is a C++ boolean type. Since C++ is statically typed, each argument, variable, and return value must have a type specified.

The QGIS/Python Ecosystem

This chapter explains a bit of history and how Python and QGIS work together. You’ll also learn how to manage Python plugins in QGIS.
  • History
Early in the development of QGIS, a plugin architecture was designed to provide a way to dynamically add new features and capabilities. This architecture was created using C++. Since writing a QGIS plugin in C++ is not a trivial task, the number of potential contributors was limited.

In 2007 work began to add Python as a scripting language. Python was hosen because it has become the lingua franca of the GIS scripting world and would integrate nicely with the QGIS API. When version 0.9 of QGIS was released in late 2007, it included support for writing scripts and plugins in Python.

With the first release of Python support, the number of contributed plugins grew at an exceptional rate, contributing to the overall growth of QGIS as a first-class desktop GIS.

How Python and QGIS work together

QGIS is written in C++ and is comprised of over 400 core classes that make up the application. Of these, roughly 75% are Python enabled through the use of SIP13, the extension module generator that outputs C++ code. These “sip” files are compiled as part of QGIS to provide the Python interface to the classes. The Python support is divided 13 http://loc8.cc/ppg/py_sip into several modules:
  • qgis.core - core classes
  • qgis.gui - graphical user interface classes
  • qgis.analysis - analysis related classes
  • qgis.networkanalysis - classes for network analysis

تعليقات
ليست هناك تعليقات
إرسال تعليق



    وضع القراءة :
    حجم الخط
    +
    16
    -
    تباعد السطور
    +
    2
    -