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,136
  • 29
  • 109
  • 338
Lutz
  • 393
  • 3
  • 15

1 Answers1

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")
Joseph
  • 75,746
  • 7
  • 171
  • 282
  • 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 Jan 13 '20 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 '20 at 10:15
  • Made a related answer if people prefer using Processing Toolbox algorithm to do the same e.g https://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 Apr 13 '21 at 22:57