After unsuccessfully trying to use processing.run('qgis:idwinterpolation', ...)
using a layer object in memory as the source (seems to like full file paths to physically stored datasets), I tried to use QgsIDWInterpolator
instead but it crashes QGIS and creates an empty TIF file.
I'm using data from one dataset but iterating over the features to get a unique list of air quality categories and create one raster for each category. The code starts after I've already generated a temporary layer airQLayer
specific to a category. I want a tif interpolation per category. I'm not too familiar with the settings though, perhaps doing something wrong.
layer = airQLayer
layer_data = QgsInterpolator.LayerData()
layer_data.vectorLayer = layer
layer_data.zCoordInterpolation=False
layer_data.interpolationAttribute = 11 # the 12th field in the layer contains the data to interpolate
layer_data.mInputType = 1
idw_interpolator = QgsIDWInterpolator([layer_data])
export_path = rasOut # e.g. r"C:\Temp\AnalysisResults\BiomassNOx_Emissions_tpa.tif"
rect = layer.extent()
res = 10
ncols = int( ( rect.xMaximum() - rect.xMinimum() ) / res )
nrows = int( (rect.yMaximum() - rect.yMinimum() ) / res)
model_feedback.pushInfo(str(rect))
model_feedback.pushInfo("{} - {} - {}".format(res,ncols,nrows))
output = QgsGridFileWriter(idw_interpolator,export_path,rect,ncols,nrows)
output.writeFile()
The syntax for QgsGridFileWriter
seems to have fewer parameters compared to previous examples in other threads. I thought I'd sorted it that part when it started creating a tif but no, it crashes. Any ideas why? I'm on QGIS 3.10.2 and have tried with and without GRASS.
Error:
QgsInterpolator::cacheBaseData qgsinterpolator.cpp:42
QgsIDWInterpolator::interpolatePoint qgsidwinterpolator.cpp:37
sipQgsIDWInterpolator::interpolatePoint sip_analysispart1.cpp:9900
QgsGridFileWriter::writeFile qgsgridfilewriter.cpp:65
meth_QgsGridFileWriter_writeFile sip_analysispart2.cpp:54
PyMethodDef_RawFastCallKeywords :
PyMethodDef_RawFastCallKeywords :
PyEval_EvalFrameDefault :
PyEval_EvalCodeWithName :
PyFunction_FastCallDict :
PyMethodDef_RawFastCallDict :
PyObject_Call :
PyInit_sip :
sipVH__core_331 sip_corepart0.cpp:17375
sipQgsProcessingAlgorithm::processAlgorithm sip_corepart2.cpp:183909
QgsProcessingAlgorithm::runPrepared qgsprocessingalgorithm.cpp:502
QgsProcessingAlgRunnerTask::run qgsprocessingalgrunnertask.cpp:66
sipQgsProcessingAlgRunnerTask::run sip_corepart1.cpp:91963
QgsTask::start qgstaskmanager.cpp:77
QThreadPoolPrivate::reset :
QThread::start :
BaseThreadInitThunk :
RtlUserThreadStart :
layer_data.vectorLayer = layer
. It must be:layer_data.source = layer
Please, see my answer.