Using the QGIS Python console for interactive control

The Python console allows you to interactively control QGIS; you can test out ideas or just do some quick automation. The console is the simplest way to use the API.

How to do it...

In the following steps, we'll open the console, create a vector layer in the memory, and display it on the map:

  1. Start QGIS.
  2. From the Plugins menu, select Python Console.
  3. The following code will create a point on the map canvas:
            layer =  QgsVectorLayer('Point?crs=epsg:4326','MyPoint',"memory") 
            pr = layer.dataProvider() 
            pt = QgsFeature() 
            point1 = QgsPoint(20,20) 
            pt.setGeometry(QgsGeometry.fromPoint(point1)) 
            pr.addFeatures([pt]) 
            layer.updateExtents() 
            QgsMapLayerRegistry.instance().addMapLayers([layer]) 
    

How it works...

This ...

Get QGIS Python Programming Cookbook - Second Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.