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?

4

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")
|improve this answer|||||
  • 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? – Lasse Jan 13 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 Jan 14 at 10:15

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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