1

I want to run this code outside QGIS as a standalone Python script.

import os
import processing

os.chdir('C:/Users/User/test/25')
alg_params = { '-c' : False, 
'-f' : False, 
'-i' : False, 
'-n' : False, 
'-r' : False, 
'GRASS_RASTER_FORMAT_META' : '', 
'GRASS_RASTER_FORMAT_OPT' : '', 
'GRASS_REGION_CELLSIZE_PARAMETER' : 0, 
'GRASS_REGION_PARAMETER' : None, 
'map' : 'test.tif', 
'null' : None, 
'output' : 'Null/out.tif', 
'setnull' : '0' }
processing.run('grass7:r.null', alg_params)

I searched for some solutions but got for Linux. I have QGIS 3.16. Please guide how to run it on Windows?

1
1

This answer assumes that you have an OSGeo4W64 installation of QGIS. If you have a standalone install, you will need to alter some of the files in the batch file and Python script.

First, create and save a batch file with the following content. As I said, make sure all the paths are correct for your system and installation. This batch file will set up the required environment to run QGIS processing algorithms including Grass algs with Python outside of the QGIS Python console.

@echo off
SET OSGEO4W_ROOT=C:\OSGeo4W64
call "%OSGEO4W_ROOT%"\bin\o4w_env.bat
call "%OSGEO4W_ROOT%"\bin\qt5_env.bat
call "%OSGEO4W_ROOT%"\bin\py3_env.bat
call "%OSGEO4W_ROOT%"\apps\grass\grass78\etc\env.bat
@echo off
path %PATH%;%OSGEO4W_ROOT%\apps\qgis\bin
path %PATH%;C:\OSGeo4W64\apps\Qt5\bin
path %PATH%;C:\OSGeo4W64\apps\Python37\Scripts
set QGIS_PREFIX_PATH=%OSGEO4W_ROOT:\=/%/apps/qgis
set GDAL_FILENAME_IS_UTF8=YES
rem Set VSI cache to be used as buffer, see #6448
set VSI_CACHE=TRUE
set VSI_CACHE_SIZE=1000000
set PYTHONPATH=%PYTHONPATH%;%OSGEO4W_ROOT%\apps\qgis\python
set PYTHONHOME=%OSGEO4W_ROOT%\apps\Python37
set QT_PLUGIN_PATH=%OSGEO4W_ROOT%\apps\qgis\qtplugins;%OSGEO4W_ROOT%\apps\qt5\plugins

cmd.exe

This line call "%OSGEO4W_ROOT%"\apps\grass\grass78\etc\env.bat is required to run Grass algorithms from a standalone script. Give the batch file a logical name (e.g. qgis-python-grass.bat).

Save the following script as a .py file. Again, it is up to you change all paths in this script to match your system and installation including the paths to your input and output .tif files.

import sys

from qgis.core import QgsApplication

from qgis.analysis import QgsNativeAlgorithms

QgsApplication.setPrefixPath('C:/OSGeo4W64/apps/qgis', True)
qgs = QgsApplication([], False)
qgs.initQgis()

sys.path.append('C:\\OSGeo4W64\\apps\\qgis\\python\\plugins')

import processing
from processing.core.Processing import Processing
Processing.initialize()
QgsApplication.processingRegistry().addProvider(QgsNativeAlgorithms())

alg_params = {
'map':'C:/Users/Path/To/Input.tif',
'setnull':'',
'null':0,
'-f':False,
'-i':False,
'-n':False,
'-c':False,
'-r':False,
'output':'C:/Users/Path/To/Output.tif',
'GRASS_REGION_PARAMETER':None,
'GRASS_REGION_CELLSIZE_PARAMETER':0,
'GRASS_RASTER_FORMAT_OPT':'',
'GRASS_RASTER_FORMAT_META':''
}

processing.run("grass7:r.null", alg_params)

For argument's sake, let's say that we will call this script r-null_standalone.py. Place both this script and the batch file in the same folder. You can then simply double click the batch file to launch a cmd window. At the command prompt just type python3 r-null_standalone.py and hit Enter to run your Python script.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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