Book Image

Mastering QGIS

Book Image

Mastering QGIS

Overview of this book

Table of Contents (18 chapters)
Mastering QGIS
Credits
Foreword
About the Authors
About the Reviewers
www.PacktPub.com
Preface
Index

Running an external algorithm or command


Often there are a bunch of legacy programs or scripts for which there are no resources to port them into another language or framework. Thanks to Python and PyQGIS, it's simple to integrate your existing programs into QGIS.

Running a simple command

We can run an external command in different ways, but we will explore how to do it with the processing toolbox that supports the progress bar, which is often useful to log algorithm steps.

To execute an external command, we will follow these steps:

  1. Create a processing toolbox script called runping.

  2. Code the script.

  3. Test the script.

Step one is similar to that described in the Creating a test processing toolbox script section.

The code of the script is in the following code snippet:

import subprocess
import time

proc = subprocess.Popen(
    ["ping", "-c", "10", "localhost"],
    stdout=subprocess.PIPE,
    stdin=subprocess.PIPE,
    stderr=subprocess.PIPE)

counter = 0
for line in iter(proc.stdout.readline, '')...