Book Image

QGIS Python Programming Cookbook

Book Image

QGIS Python Programming Cookbook

Overview of this book

Table of Contents (16 chapters)
QGIS Python Programming Cookbook
About the Author
About the Reviewers

Generating points along a line

You can generate points within a polygon in a fairly simple way by using the point in polygon method. However, sometimes you may want to generate points along a line. You can randomly place points inside the polygon's extent — which is essentially just a rectangular polygon — or you can place points at random locations along the line at random distances. In this recipe, we'll demonstrate both of these methods.

Getting ready

You will need to download the zipped shapefile and place it in a directory named shapes in your qgis_data directory from the following:

How to do it...

First, we will generate random points along a line using a grass() function in the Processing Toolbox. Then, we'll generate points within the line's extent using a native QGIS processing function. To do this, we need to perform the following steps:

  1. First, we need to import the processing module:

    import processing
  2. Then, we'll load the line layer...