2

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?

PolyGeo
65.5k29 gold badges115 silver badges350 bronze badges
asked Jan 13, 2020 at 11:27

1 Answer 1

5

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)
output.writeFile()
iface.addRasterLayer(export_path, "output")
answered Jan 13, 2020 at 12:23
3
  • 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? 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. 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 Commented Apr 13, 2021 at 22:57

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.