I am running a .py file from terminal in Ubuntu 18.04.
python3 script.py
I am running the given below algorithm of QGIS Desktop 3.8.3 in script.py.
processing.run("gdal:cliprasterbymasklayer", {'INPUT':'/home/sreeraj/Desktop/Program/IMAGE.tif','MASK':'/home/sreeraj/Desktop/Program/VECTOR.geojson','SOURCE_CRS':QgsCoordinateReferenceSystem('EPSG:32643'),'TARGET_CRS':QgsCoordinateReferenceSystem('EPSG:32643'),'NODATA':None,'ALPHA_BAND':False,'CROP_TO_CUTLINE':True,'KEEP_RESOLUTION':False,'SET_RESOLUTION':False,'X_RESOLUTION':None,'Y_RESOLUTION':None,'MULTITHREADING':False,'OPTIONS':'','DATA_TYPE':0,'OUTPUT':'/home/sreeraj/Desktop/Program/OUTPUT.tif'})
I am getting the following errror :
Could not load source layer for INPUT: /home/sreeraj/Desktop/Program/IMAGE.tif not found
At the same time, when I open QGIS 3.8.3 in Ubuntu 18.04 and execute this same code in Plugins -> Python Console, it is working perfectly without any errors and is creating the output.
I am wondering what is happening here !!! It is the same code I am executing inside .py file and in Python Console of QGIS, but in the case of .py file, I am getting this error.
This error means that the algorithm is not able to find this .tif file. But, then how this same code is working in QGIS Python Console ? How can I solve this error ?
The exact code in script.py is as given below.
import sys
from osgeo import ogr, gdal
from gdalconst import *
from qgis.core import *
import qgis.utils
from qgis.gui import *
from PyQt5.QtGui import *
from PyQt5.QtCore import Qt
from PyQt5.QtCore import QFileInfo
from PyQt5.QtWidgets import QApplication
app = QApplication(sys.argv)
QgsApplication.setPrefixPath("/usr/share/qgis", True)
qgs = QgsApplication([], False)
sys.path.append('/usr/share/qgis/python/plugins')
from qgis.analysis import QgsNativeAlgorithms
import processing
from processing.core.Processing import Processing
qgs.initQgis()
Processing.initialize()
QgsApplication.processingRegistry().addProvider(QgsNativeAlgorithms())
from processing.tools import *
processing.run("gdal:cliprasterbymasklayer", {'INPUT':'/home/sreeraj/Desktop/Program/IMAGE.tif','MASK':'/home/sreeraj/Desktop/Program/VECTOR.geojson','SOURCE_CRS':QgsCoordinateReferenceSystem('EPSG:32643'),'TARGET_CRS':QgsCoordinateReferenceSystem('EPSG:32643'),'NODATA':None,'ALPHA_BAND':False,'CROP_TO_CUTLINE':True,'KEEP_RESOLUTION':False,'SET_RESOLUTION':False,'X_RESOLUTION':None,'Y_RESOLUTION':None,'MULTITHREADING':False,'OPTIONS':'','DATA_TYPE':0,'OUTPUT':'/home/sreeraj/Desktop/Program/OUTPUT.tif'})
qgs.exitQgis()
print('Done')
PLEASE NOTE THAT THIS IS NOT AN ISSUE WITH THIS ALGORITHM. WHEN I AM TRYING WITH OTHER ALGORITHMS ALSO, THIS SAME ISSUE IS COMING UP.
I doubt that there is a problem with the path convention starting with '/home/' when using these QGIS Algorithms in TERMINAL. Otherwise, it may be a issue with the initialization of processing and/or importing. So, please check in detail the headers used in this .py file.