3

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 :
1
  • Issue is in layer_data.vectorLayer = layer. It must be: layer_data.source = layer Please, see my answer.
    – xunilk
    Mar 11, 2020 at 14:44

1 Answer 1

4

Issue is in:

.
.
.
layer_data.vectorLayer = layer
.
.
.

as is referred in this link. So, I created an arbitrary point layer and named it as airQLayer.shp; where interpolation attribute was assigned to field with index equal 1. Afterward, I ran following script (by using my own paths) without crashes QGIS.

layer = iface.activeLayer() 
layer_data = QgsInterpolator.LayerData()
layer_data.source = layer  #former line with issue
layer_data.zCoordInterpolation = False
layer_data.interpolationAttribute = 1  # the 2nd field in the layer contains the data to interpolate
layer_data.mInputType = 1


idw_interpolator = QgsIDWInterpolator([layer_data])

rasOut = r"/home/zeito/pyqgis_data/BiomassNOx_Emissions_tpa.tif"

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)

output = QgsGridFileWriter(idw_interpolator,export_path,rect,ncols,nrows)
output.writeFile()

Result can be observed in following image:

enter image description here

1
  • Thanks for the reference. For those finding this, some posts I read had 'layer_data.vectorLayer = layer' but it should be 'layer_data.source = layer'. I can report no further crashes!
    – GIS_py
    Mar 12, 2020 at 16:18

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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