3

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.

7
  • Post the rest of the script as well. Commented Oct 24, 2019 at 10:12
  • @HeikkiVesanto Posted.
    – Sreeraj
    Commented Oct 24, 2019 at 10:38
  • I think the file not found is a symptom of a different issue. Can you try using the format of the script found in this question, I think you don't have the correct environmental paths called: gis.stackexchange.com/questions/311957/… Commented Oct 24, 2019 at 10:44
  • @HeikkiVesanto I tried using the format of the script found in gis.stackexchange.com/questions/311957/… (including using the environmental paths called in this question), but I am getting the same error.
    – Sreeraj
    Commented Oct 24, 2019 at 11:17
  • Could be a permissions issue? sudo chmod -R 777 /home/sreeraj/Desktop/Program/ Commented Oct 24, 2019 at 11:26

0

Your Answer

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

Browse other questions tagged or ask your own question.