I'm trying to automate idw interpolations from my database using PyQGIS in a standalone script. I copied the python command from the GUI which is how I got the fields I am using for the algorithm.
Below is my code:
from qgis.core import (
QgsApplication,
QgsProcessingFeedback,
QgsVectorLayer
)
import sys
QgsApplication.setPrefixPath('C:\Program Files\QGIS 3.32.0\apps\qgis', True)
qgs = QgsApplication([], False)
qgs.initQgis()
sys.path.append("C:\Program Files\QGIS 3.32.0\apps\qgis\qtplugins")
sys.path.append("C:\Program Files\QGIS 3.32.0\apps\Qt5\plugins")
sys.path.append("C:\Program Files\QGIS 3.32.0\apps\qgis\python")
sys.path.append("C:\Program Files\QGIS 3.32.0\apps\qgis\python\plugins")
sys.path.append("C:\Program Files\QGIS 3.32.0\apps\Python36\DLLs")
sys.path.append("C:\Program Files\QGIS 3.32.0\apps\Python36\lib")
sys.path.append("C:\Program Files\QGIS 3.32.0\apps\Python36\lib\site-packages\win32")
sys.path.append("C:\Program Files\QGIS 3.32.0\apps\Python36\lib\site-packages\win32\lib")
sys.path.append("C:\Program Files\QGIS 3.32.0\apps\Python36\lib\site-packages\Pythonwin")
sys.path.append("C:\Program Files\QGIS 3.32.0\apps\Python36\lib\site-packages")
sys.path.append("C:\PROGRA~1\QGIS33~1.0\apps\qgis\python\plugins")
import processing
from processing.core.Processing import Processing
Processing.initialize()
distance_coeffecient = 2
extent = '174.547209636,175.536797982,-41.691839082,-40.745909046 [EPSG:4326]'
pixel_size = 0.000946
Generate rasters from point data
def run_idw(dbname, host, port, user, password, table, output_path):
feedback = QgsProcessingFeedback()
command = {'INTERPOLATION_DATA':f'dbname='{dbname}' host={host} port={port} user='{user}' password='{password}' sslmode=disable key='id' srid=4326 type=Point checkPrimaryKeyUnicity='0' table="public"."{table}" (location)::~::0::~::2::~::2',
'DISTANCE_COEFFICIENT':distance_coeffecient,
'EXTENT':extent,
'PIXEL_SIZE':pixel_size,
'OUTPUT':output_path}
result = processing.run("qgis:idwinterpolation", command, feedback=feedback)
print("Completed interpolation...") # This never prints
return result
print("Starting interpolation...")
print(processing.algorithmHelp("qgis:idwinterpolation"))
output2 = processing.run("qgis:idwinterpolation", {'INTERPOLATION_DATA':'dbname='db' host=localhost port=5432 user='postgres' password='ps' sslmode=disable key='id' srid=4326 type=Point checkPrimaryKeyUnicity='0' table="public"."data" (location)::~::0::~::3::~::0','DISTANCE_COEFFICIENT':2,'EXTENT':'174.745782752,174.874293474,-41.363096743,-41.277173877 [EPSG:4326]','PIXEL_SIZE':9e-05,'OUTPUT':'TEMPORARY_OUTPUT'})
print(output2, "this should print") # but it doesnt
output = run_idw('db', 'localhost', '5432', 'postgres', 'ps', 'data', 'C:/Users/cucumber/Documents/graph-viz-qgis/testfile.tif')
print(output) # this also doesnt print
qgs.exitQgis()
A tif file is made when the program exits, but it's 1kb big and if I try to open it in QGIS it says its corrupt. I have no idea why the process is exiting earlier than it should. If I try the interpolation in QGIS's python console it also does not work.
The closest question I found related to my issue is this one Processing step is ignored in QGIS3 standalone python script but their solution didn't work for me.
Edit: After playing around with this some more, I found that the process exits with code 3221225477... so basically, it was crashing for some reason. I thought it could be because the raster output size is too large, so I reduced the pixel size to 0.1 but it still crashed.
Edit 2: Strangely enough the program doesn't crash when running other algorithms (I tried it with zonalstatisticsfb), it just crashes when I run the idwinterpolation algorithm.