Book Image

QGIS Python Programming Cookbook, Second Edition - Second Edition

By : Joel Lawhead
Book Image

QGIS Python Programming Cookbook, Second Edition - Second Edition

By: Joel Lawhead

Overview of this book

QGIS is a desktop geographic information system that facilitates data viewing, editing, and analysis. Paired with the most efficient scripting language—Python, we can write effective scripts that extend the core functionality of QGIS. Based on version QGIS 2.18, this book will teach you how to write Python code that works with spatial data to automate geoprocessing tasks in QGIS. It will cover topics such as querying and editing vector data and using raster data. You will also learn to create, edit, and optimize a vector layer for faster queries, reproject a vector layer, reduce the number of vertices in a vector layer without losing critical data, and convert a raster to a vector. Following this, you will work through recipes that will help you compose static maps, create heavily customized maps, and add specialized labels and annotations. As well as this, we’ll also share a few tips and tricks based on different aspects of QGIS.
Table of Contents (16 chapters)
QGIS Python Programming Cookbook - Second Edition
Credits
About the Author
About the Reviewer
www.PacktPub.com
Customer Feedback
Preface

Building a standalone application


QGIS is a complete desktop GIS application. However, with PyQGIS, it can also be a comprehensive geospatial Python library to build standalone applications. In this recipe, we will build a simple standalone script that will create a map with a line on it.

Getting ready

All you need to do to get ready is ensure that you have configured Eclipse and PyDev for PyQGIS development, as described in the Setting up your QGIS IDE recipe of this chapter.

How to do it...

In PyDev, create a new project called MyMap with a Python script called MyMap.py, as follows:

  1. In the Eclipse File menu, select New and then click on PyDev Project.

  2. In the PyDev project's Name field, enter MyMap.

  3. Next, select the Python radio button from the Project Type list.

  4. From the Interpreter pull-down menu, select PyQGIS.

  5. Leave the radio button checked for Add project directory to the PYTHONPATH.

  6. Click on the Finish button.

  7. Now select the project in the PyDev package explorer.

  8. From the File menu, select New and then click on File.

  9. Name the file myMap.py.

  10. Click on the Finish button.

  11. Add the following code to the file that is open in the editor:

            from qgis.core import * 
            from qgis.gui import * 
            from qgis.utils import * 
            from PyQt4.QtCore import * 
            from PyQt4.QtGui import * 
     
            app = QgsApplication([], True) 
            path = "C:/Program Files/QGIS2.18/apps/qgis" 
            app.setPrefixPath(path, True) 
            app.initQgis() 
            canvas = QgsMapCanvas() 
            title = "PyQGIS Standalone Application Example" 
            canvas.setWindowTitle(title) 
            canvas.setCanvasColor(Qt.white) 
            layer_info = 'LineString?crs=epsg:4326' 
            layer =  QgsVectorLayer(layer_info, 'MyLine' , "memory") 
            pr = layer.dataProvider() 
            linstr = QgsFeature() 
            wkt = "LINESTRING (1 1, 10 15, 40 35)" 
            geom = QgsGeometry.fromWkt(wkt) 
            linstr.setGeometry(geom) 
            pr.addFeatures([linstr]) 
            layer.updateExtents() 
            QgsMapLayerRegistry.instance().addMapLayer(layer) 
            canvas.setExtent(layer.extent()) 
            canvas.setLayerSet([QgsMapCanvasLayer(layer)]) 
            canvas.zoomToFullExtent() 
            canvas.show() 
            exitcode = app.exec_() 
            QgsApplication.exitQgis() 
            sys.exit(exitcode) 
    
  12. From the Run menu, select Run.

  13. Verify that the standalone QGIS map appears in a new window, as shown here:

How it works...

This recipe uses as little code as possible to create a map canvas and draw a line to demonstrate the skeleton of a standalone application, which you can build up further to add more functionality, as we will see in later recipes.

To create line geometry, we use Well-Known Text (WKT), which provides a simple way to define the line vertices without creating a bunch of objects. The map does not appear until you call the canvas.show() method. This allows you to set up the map behind the scenes and then display it when it is complete.

There's more...

The standalone application can be compiled into an executable that can be distributed without installing QGIS using either py2exe or PyInstaller.

You can find out more about py2exe at http://www.py2exe.org.

You can learn more about PyInstaller at https://github.com/pyinstaller/pyinstaller/wiki.