1

Trying to set the power coefficient for the IDW interpolation in QGIS 3.10 in pyqgis.

Whatever number I set it has no effect on the interpolation output - always the same output. The default is 2 but I tried 10, 90, 0.5 and all the same result:

enter image description here

Here's the code:

## Get the selected features by ID first and create a clone layer with that selection

it = [f for f in layObj.getFeatures()]
ids = [5,6,7,8,9,10,12,21,24,26,28]  # example of ids only- in reality it's hundreds                                  
layObj.selectByIds(ids) 
clone = processing.run("native:saveselectedfeatures", {'INPUT': layObj, 'OUTPUT': memory:{}'.format(layObj.name())})['OUTPUT'] 

layer_data = QgsInterpolator.LayerData()
layer_data.source = clone
layer_data.zCoordInterpolation=False
layer_data.interpolationAttribute = 'PM10_Emissions_tpa'

# I've tried all of these
layer_data.distanceCoefficient = 10  # https://qgis.org/pyqgis/3.10/analysis/QgsIDWInterpolator.html?highlight=idw#
layer_data.setDistanceCoefficient = 10  
layer_data.coefficient = 10  

layer_data.mInputType = 1 # I've tried 0 and 1- same exact output
idw_interpolator = QgsIDWInterpolator([layer_data])

export_path = rasOut 
rect = clone.extent()
res = pixSizeP
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()

Perhaps doing something stupid but can't seem to find a good example that works for me- seen other posts but didn't solve it for me. Can anyone help?

1
  • No results by using different 'Distance Coefficients' is because layer_data has not 'setDistanceCoefficient' method. Desired method is in idw_interpolator. Please, see my answer.
    – xunilk
    Commented May 7, 2020 at 20:48

1 Answer 1

2

No results by using different 'Distance Coefficients' is because layer_data has not 'setDistanceCoefficient' method. Desired method is in idw_interpolator.

So, by using same code in your previous question about QgsIDWInterpolator, I had to add only one code line:

.
.
.
idw_interpolator.setDistanceCoefficient(10)
.
.
.

in following code:

layer = iface.activeLayer() 
layer_data = QgsInterpolator.LayerData()
layer_data.source = layer
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])
idw_interpolator.setDistanceCoefficient(10) #added line

rasOut = r"/home/zeito/pyqgis_data/BiomassNOx_Emissions_tpa2.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()

for producing visible changes in interpolated raster layer.

After running above code with same point layer in your previous question, I got result observed in following image (different of my answer in your previous question):

enter image description here

So, you should modify your code as follows:

.
.
.
idw_interpolator = QgsIDWInterpolator([layer_data])
idw_interpolator.setDistanceCoefficient(10)
.
.
.

for obtaining an equivalent result.

1
  • Works, Grand! Thanks again. I just can't seem to really understand the API without a full example somewhere
    – GIS_py
    Commented May 8, 2020 at 14:36

Your Answer

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

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