I am referring to the most voted answer by root676 in Calling interpolation plugin from Python console of QGIS.

root676 ends his/her answer with:

Keep in mind that the QGIS-API is currently rewritten to version 3.0 and the used interpolation-classes are moved from qgis.analysis to qgis.core! This will have a huge impact on the functionality of this script so that it must be rewritten for version 3.0!

Now I want to use this code to make a TIN interpolation in QGIS 3. However, I could not find any post with code that is adapted to the QGIS 3 version.

Can anyone help me out?

1 Answer 1


The following minimal example is in a somewhat similar format to the post you linked to which should work for QGIS 3:

# Interpolate points using QgsTinInterpolator

pathToFile = "path/to/input.shp"
layer = QgsVectorLayer(pathToFile, 'input','ogr')
layer_data = QgsInterpolator.LayerData()
layer_data.source = layer
layer_data.zCoordInterpolation = False
index = layer.fields().indexFromName("fieldName")
layer_data.interpolationAttribute = index
layer_data.sourceType = QgsInterpolator.SourcePoints
ncol = 30
nrows = 30
interpolation_method = QgsTinInterpolator.Linear
#interpolation_method = QgsTinInterpolator.CloughTocher

tin_interpolator = QgsTinInterpolator([layer_data], interpolation_method)
export_path = "path/to/output.tif"
rect = layer.extent()

output = QgsGridFileWriter(tin_interpolator, export_path, rect, ncol, nrows)
iface.addRasterLayer(export_path, "output")
  • thank you! and one additional question if you don't mind: how can I replace the interpolation attribute with a column of my attribute table, which has different values?
    – Lutz
    Commented Jan 13, 2020 at 17:15
  • 1
    @Lasse - Most welcome! The interpolation attribute is just the field index of column. So the first field in your table has the index of 0, then 1 and so on. I'll edit the post to allow you to simply enter the name of the column and the index will be used automatically.
    – Joseph
    Commented Jan 14, 2020 at 10:15
  • Made a related answer if people prefer using Processing Toolbox algorithm to do the same e.g gis.stackexchange.com/a/393745/638 Some parts in the processing algorithm may dynamically retrieve from above code like position of the field index using name
    – ThomasG77
    Commented Apr 13, 2021 at 22:57

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.