2

I have a problem with IDW interpolation. I need to use canvas extent and other attributes (I created an option with only attribute 0, other attributes aren't working). The documentation is not very helpful.

My code:

layer = iface.activeLayer() 

layer_data = QgsInterpolator.LayerData()
layer_data.source = xlayer
layer_data.zCoordInterpolation=False
layer_data.interpolationAttribute =0
layer_data.SourceType =SourcePoints
layer_data.distanceCoefficient=2


tin_interpolator = QgsIDWInterpolator([layer_data])

export_path = r"C:\Users\Maria\Documents\miau.tif"

cos= canvas.extent()

output = QgsGridFileWriter(tin_interpolator,export_path,cos,2000,2000)
output.writeFile()

rlayer=iface.addRasterLayer(export_path, "interpolation_output")
1

Not sure why you called it tin_interpolator when you want to use QgsIDWInterpolator :) But you need to replace the following lines:

  • layer_data.SourceType =SourcePoints to

    layer_data.sourceType = QgsInterpolator.SourcePoints


  • layer_data.distanceCoefficient=2 to

    tin_interpolator.setDistanceCoefficient(2)


  • cos= canvas.extent() to

    cos = iface.mapCanvas().extent()


So your final code should look something like:

layer = iface.activeLayer() 
layer_data = QgsInterpolator.LayerData()
layer_data.source = layer
layer_data.zCoordInterpolation = False
layer_data.interpolationAttribute = 0
layer_data.sourceType = QgsInterpolator.SourcePoints

tin_interpolator = QgsIDWInterpolator([layer_data])
tin_interpolator.setDistanceCoefficient(2)

export_path = r"C:\Users\Maria\Documents\miau.tif"
cos = iface.mapCanvas().extent()
output = QgsGridFileWriter(tin_interpolator,export_path,cos,2000,2000)
output.writeFile()

rlayer = iface.addRasterLayer(export_path, "interpolation_output")

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.