Contour generator processing algorithm

Button image

The contour generator generates a layer of contour lines or polygons based on a values at a set of data points. The algorithm uses a set of parameters defining the source of the data for contouring, and the parameters used to define how the contour levels are calculated.

The algorithm requires the python libraries numpy and matplotlib python libaries to be installed.

As with all processing algorithms the parameters can be entered manually into the algorithm dialog box, or created programmatically. This allows the contour function to be used by other plugins and processing scripts.

The contouring function can also be accessed from the ContourDialog.

Contour generator algorithm

Parameters

The plugin uses the following parameters:

Input point layer (InputLayer)

The source of data points to contour

Value to contour (InputField)

A field or expression defining the data value at each point

Duplicate point tolerance (DuplicatePointTolerance)

If greater than zero then where points are closer than this to each other only one of the points will be used

Contour type (ContourType)

The type of layer to create. Can be contour lines, filled contour polygons each representing the area where the data lies between two contour levels, or layer polygons representing the area where the data is greater than the contour level

Filled contour options (ExtendOption)

If creating filled contours then select whether to create polygons where the data is less than the minimum contour level and/or greater than the maximum contour level. Options are:

Method used to calculate the contour levels (ContourMethod)

The contour levels can be calculated from the data values using one of a number of possible algorithms. Options are:

  • N equal intervals (0) the range from the minimum to the maximum data value is divided into the specified number of equal intervals
  • N quantiles (1) the distribution of data values is split into the specified number of quantile intervals such that approximately the same number of data points is in each interval
  • Logarithmic intervals (2) intervals are calculated at values 1,2, and 5 times a power of 10 spanning the range of data, up to a maximum number of contours
  • Fixed contour interval (3) Contours are calculated at multiples of a fixed interval, up to a maximum number of contours
  • User selected contour levels (4) the contour levels are defined by space separated values in the contour levels text field
  • Number (or max number) of contours (NContour)

    For the "N equal intervals" and "N quantiles" methods specifies the number of intervals to create. For the "Logarithmic intervals" and "Fixed contour interval" methods this is the maximum number of contour levels that will be created.

    Minimum contour level (MinContourValue)

    Specifies the minimum contour level. By default this comes from the minimum data value.

    Maximum contour level (MaxContourValue)

    Specifies the maximum contour level. By default this comes from the maximum data value.

    Contour interval (ContourInterval)

    For the "Fixed contour interval" method this specifies the interval between contour levels.

    Label decimal places (LabelDecimalPlaces)

    Specifies the decimal places used to represent the value in the label attribute of each contour feature. If -1 or not set then a value is calculated based on the contour levels.

    Trim trailing zeros from labels (LabelTrimZeros)

    If this is True then trailing zeros after the decimal point are removed from the value in the lavel attribute.

    Units to append to label values (LabelUnits)

    Defines a text string that is appended to the label attribute of each contour feature. Typically this might be units of measurement.

    Output Layer (OutputLayer)

    The destination layer for the contour features.

    Using the contour generator algorithm programmatically

    The following example shows the use of the algorithm in a python script. Most of the paramters are optional, but may be required depending on the contour method chosen.

    import processing
    layer=iface.mapCanvas().currentLayer()
    result=processing.run(
       "contourplugin:generatecontours",
          { 'ContourInterval' : 1, 
            'ContourLevels' : '', 
            'ContourMethod' : 1, 
            'ContourType' : 0, 
            'DuplicatePointTolerance' : 0, 
            'ExtendOption' : 0, 
            'InputField' : '"z"', 
            'InputLayer' : layer,
            'LabelDecimalPlaces' : -1, 
            'LabelTrimZeros' : False, 
            'LabelUnits' : '', 
            'MaxContourValue' : None, 
            'MinContourValue' : None, 
            'NContour' : 20, 
            'OutputLayer' : 'memory:' }
            )
    layer=result['OutputLayer']
    QgsProject.instance().addMapLayer(layer)