OSGeo.be GeoDevEvening

How to start a QGis plugin?

Please install

QGis 2.18 (Las Palmas)

Notepad++ (or your favorite editor)


QGis plugins

Plugin Builder
Plugin Reloader (experimental)
QOSM
QuickOSM

Demo plugin

Available on Github

https://github.com/Roel/qgis-geogame

QGis plugin platform

QGis & Qt versions

Python version


> import sys
> sys.version
'2.7.5 (default, May 15 2013, 22:44:16) [MSC v.1500 64 bit (AMD64)]'
        

API versions

QGis 2

Python 2
Qt 4

QGis 3

Python 3
Qt 5

Python

Python documentation

Python library reference

Numbers

                                                            
> a = 1
> type(a)
type 'int'

> b = 1.0
> type(b)
type 'float'

> 3/2
1

> 3.0/2.0
1.5

> int(3.0/2.0)
1
          

Text

                                                            
> c = 'a'
> type(c)
type 'str'
 
> d = "b"
> type(d)
type 'str'

> c+d
'ab'
          

Operators

                                                            
> a = 1

> a = a + 1
> a += 1
> a
3

> a == 4
False
          

Lists

                                                            
> l = [1, 2, 3]
> type(l)
type 'list'

> len(l)
3

> (l[0], l[-1])
(1, 3)

> l.append(4)
> l
[1, 2, 3, 4]
          

More

                                                            
> l = [1, 2, 3]

> [i*2 for i in l if i < 3]
[2, 4]

> max(-2, 0)
0

> 'bugs:bunny'.split(':')
['bugs', 'bunny']

> import random
> random.randint(0, 10)
4
          

Conditionals


if a:
    print('a')
elif b:
    print('b')
else:
    print('c')
          

Indentation


if a:
    print('a')
    if b:
      print('b')
else:
    print('c')

if a:
    print('a')
    if b:
      print('b')
    else:
        print('c')
          

Functions

                                                            
def longer(text, letterCount=3):
    return text + text[-1]*letterCount
    
> longer('YOLO')
'YOLOOOO'

> longer('YOLO', 6)
'YOLOOOOOOO'
          

Inner functions


def sumOfDoubles(list):
    def double(number):
        return number*2

    sum = 0
    for item in list:
      sum += double(item)
    return sum

sum([i*2 for i in list])
          

Object orientation


class Game(object):
    def __init__(self, name, maxPlayers):
        self.name = name
        self.maxPlayers = maxPlayers

    def play(self, players):
        if players > self.maxPlayers:
            return 'Too many players!'
        else:
            return 'Enjoy your game of ' + self.name + '!'

stratego = Game('Stratego', 2)
risk = Game('Risk', 6)

stratego.play(3)
# Too many players!

risk.play(3)
# Enjoy your game of Risk!
          

Qt

Framework for graphical user interfaces
(and other things)

Qt documentation

Qt API reference

Qt designer

Signals / Slots

QLabel


label = QLabel("Initial text.")

label.setText("Text changed to this text.")
label.setText("Or this.")
          

QLCDNumber


scorePanel = QLCDNumber()

scorePanel.display(42)
scorePanel.display(1337)
          

QPushButton


def start():
    print('The game is on!')

button = QPushButton("Start")
button.setEnabled(True)

button.clicked.connect(start)
          

QWebView


webView = QWebView()

webView.setUrl(QUrl("http://www.qgis.org"))
webView.setHtml("")
          

QGis API

PyQGis developer cookbook

QGis API reference

QgisInterface

                                                            
> iface
qgis._gui.QgisInterface object at 0x0000000006E7D0D07

> iface.actionAddWmsLayer()
PyQt4.QtGui.QAction object at 0x000000001B53FAE8

> iface.actionAddWmsLayer().trigger()
        

QgsPoint


> point1 = QgsPoint(100.0, 200.0)
> point2 = QgsPoint(100.0, 500.0)

> point1.x()
100.0

> point1.y()
200.0

> point1.distance(point2)
300.0

> point1.azimuth(QgsPoint(200.0,500.0))
18.43494882292201
        

QgsMapCanvas


def printPoint(self, point):
    print(point.x(), point.y())

canvas = iface.mapCanvas()
canvas.xyCoordinates.connect(printPoint)
        

QgsMapTool


def printPoint(self, point):
    print(point.x(), point.y())

canvas = iface.mapCanvas()

mapToolPoint = QgsMapToolEmitPoint(canvas)
mapToolPoint.canvasClicked.connect(printPoint)

canvas.setMapTool(mapToolPoint)
        

QgsCoordinateReferenceSystem


wgs84 = QgsCoordinateReferenceSystem(4326, QgsCoordinateReferenceSystem.EpsgCrsId)
lambert72 = QgsCoordinateReferenceSystem(31370, QgsCoordinateReferenceSystem.EpsgCrsId)
webmercator = QgsCoordinateReferenceSystem(3857, QgsCoordinateReferenceSystem.EpsgCrsId)

canvasCRS = canvas.mapRenderer().destinationCrs()
        

QgsCoordinateTransform


t = QgsCoordinateTransform(wgs84, canvasCRS)

transformedPoint = t.transform(QgsPoint(100.0, 200.0))
        

QgsVectorLayer


> vectorLayer = iface.activeLayer()
> vectorLayer
qgis._core.QgsVectorLayer object at 0x000000001B2720D0

> features = vectorLayer.getFeatures(QgsFeatureRequest(1))
> features.next()
qgis._core.QgsFeature object at 0x000000001B272158
        

QgsFeature


> feature = vectorLayer.getFeatures(QgsFeatureRequest(1)).next()

> feature.attribute('name')
u'Manneken Pis'

> feature.geometry().asPoint()
(4.34999,50.845)
        

Starting a QGis plugin

Plugin builder

Files

~/.qgis2/python/plugins/GeoGame

  • metadata.txt
    • Information about the plugin
  • __init__.py
    • Classfactory
  • geogame.py
    • Base plugin class
  • geogame_dockwidget.py
    • Class for dockwidget (user interface)
  • geogame_dockwidget_base
    • Dockwidget UI itself (xml)

Compile resources

Shift + right click in plugin directory, then
'Open command window here'


"C:\Program Files\QGIS 2.18\bin\pyrcc4.exe" resources.qrc > resources.py