Using the python console in QGis, I would like to interpolate several point files (txt x,y,z) to make rasters.

I have been trying this but it's not working and I don't know enough about python to make it work.

import qgis.core
import qgis.analysis

layer = QgsVectorLayer(uri, 'test', "delimitedtext")
layer_data = qgis.analysis.QgsInterpolator.LayerData()
layer_data.vectorLayer = layer
layer_data.interpolationAttribute =2
layer_data.mInputType = 0

tin_interpolator = QgsTINInterpolator([layer_data])

export_path = "C:/test.asc"

rect = layer.extent()
res = 10
ncol = int( ( rect.xMaximum() - rect.xMinimum() ) / res )
nrows = int( (rect.yMaximum() - rect.yMinimum() ) / res)
output = QgsGridFileWriter(tin_interpolator,export_path,rect,ncol,nrows,res,res)

I keep getting this error: Traceback (most recent call last): File "", line 1, in File "C:/test.py", line 13, in tin_interpolator = QgsTINInterpolator([layer_data]) NameError: name 'QgsTINInterpolator' is not defined

Can anyone explain what this variable means layer_data.zCoordInterpolation=False, and whether I should put this to True?


The error is due to the import statement, replace:

tin_interpolator = QgsTINInterpolator([layer_data])


tin_interpolator = qgis.analysis.QgsTINInterpolator([layer_data])
  • 1
    Thanks, that did work. I also had to add qgis.analysis to QgsGridFileWriter – Nuttieke Mar 14 '18 at 10:44
  • Yep. Alternatively you could use from qgis.analysis import QgsTINInterpolator, QgsGridFileWriter – Oscar Campo Mar 14 '18 at 10:50

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.