[QGIS-Developer] gdal processing gives no output in standalone script

Bourdon, Jean-François (DIF) Jean-Francois.Bourdon at mffp.gouv.qc.ca
Fri Jul 31 06:20:50 PDT 2020


Hi all,

I'm unable to use any GDAL algorithm in a standalone script (QGIS 3.12.1 on Windows 10). All processing.run(native: ) and processing.run(saga: ) algorithms work without any problem, but when using a processing.run(gdal: ) algorithms, I only get an empty result (no error). However, the same processing(gdal: ) algorithm works when I'm using the python console within QGIS.

Thinking that it may be related to environment variables, I used os.environ inside QGIS' python console to see what variables were loaded, then loaded the exact same ones in my standalone script but it didn't work. I asked on StackOverflow but I’m still looking for a solution. I tried on couple QGIS’ version and different computers with always the same result. I suppose I must be missing something in my imports.

https://gis.stackexchange.com/questions/359589/no-result-with-processing-rungdal-in-standalone-qgis-script
https://github.com/qgis/QGIS/issues/38036

Thanks for any help!


How to Reproduce
Create a py file, change the inpath and outpath variables and run it with QGIS' python (note that the paths are with an OSGeo4W64 installation at C:/OSGeo4W64):

import os

os.environ['PATH'] = ";".join([
    "C:/OSGeo4W64/apps/qgis/bin",
    "C:/OSGeo4W64/apps/Python37",
    "C:/OSGeo4W64/apps/Python37/Scripts",
    "C:/OSGeo4W64/apps/qt5/bin",
    "C:/OSGeo4W64/apps/Python27/Scripts",
    "C:/OSGeo4W64/bin",
    "C:/WINDOWS/system32",
    "C:/WINDOWS",
    "C:/WINDOWS/system32/WBem",
    "C:/OSGeo4W64/apps/msys/bin",
    "C:/OSGeo4W64/apps/Python37/lib/site-packages/pywin32_system32",
    "C:/OSGeo4W64/apps/Python37/lib/site-packages/numpy/.libs",
    "C:/OSGeo4W64/apps/Python37/lib/site-packages/scipy/.libs"
])
os.environ['PROJ_LIB'] = 'C:/OSGeo4W64/share/proj'
os.environ['PYTHONHOME'] = 'C:/OSGeo4W64/apps/Python37'
os.environ['PYTHONPATH'] = 'C:/OSGeo4W64/apps/Python37;C:/OSGeo4W64/apps/Python37/Scripts'
os.environ['GDAL_DATA'] = 'C:/OSGeo4W64/share/gdal'
os.environ['GDAL_DRIVER_PATH'] = 'C:/OSGeo4W64/bin/gdalplugins'
os.environ['GDAL_FILENAME_IS_UTF8'] = 'YES'
os.environ['QGIS_PREFIX_PATH'] = 'C:/OSGeo4W64/apps/qgis'
os.environ['HOME']= 'C:/Users/bouj1a'
os.environ['GEOTIFF_CSV'] = 'C:/OSGeo4W64/share/epsg_csv'
os.environ['JPEGMEM']= '1000000'
os.environ['O4W_QT_BINARIES']= 'C:/OSGeo4W64/apps/Qt5/bin'
os.environ['O4W_QT_HEADERS']= 'C:/OSGeo4W64/apps/Qt5/include'
os.environ['O4W_QT_LIBRARIES']= 'C:/OSGeo4W64/apps/Qt5/lib'
os.environ['O4W_QT_PLUGINS']= 'C:/OSGeo4W64/apps/Qt5/plugins'
os.environ['O4W_QT_PREFIX']= 'C:/OSGeo4W64/apps/Qt5'
os.environ['O4W_QT_TRANSLATIONS']= 'C:/OSGeo4W64/apps/Qt5/translations'
os.environ['OSGEO4W_ROOT']= 'C:/OSGeo4W64'
os.environ['QT_PLUGIN_PATH']= 'C:/OSGeo4W64/apps/qgis/qtplugins;C:/OSGeo4W64/apps/qt5/plugins'
os.environ['VSI_CACHE']= 'TRUE'
os.environ['VSI_CACHE_SIZE']= '1000000'

from qgis.core import *
QgsApplication.setPrefixPath("C:/OSGeo4W64/bin", True)

import processing
from processing.core.Processing import Processing
from qgis.analysis import QgsNativeAlgorithms
from PyQt5.QtCore import *

qgs = QgsApplication([], False)
qgs.initQgis()
Processing.initialize()
QgsApplication.processingRegistry().addProvider(QgsNativeAlgorithms())

inpath = 'E:/raster.tif'
outpath = 'E:/polygon.shp'

params = {
    'INPUT':inpath,
    'BAND':1,
    'FIELD':'VALUE',
    'EIGHT_CONNECTEDNESS':False,
    'OUTPUT':outpath
}
processing.run("gdal:polygonize", params)

# Result (but no file created)
#> {'OUTPUT': 'E:/polygon.shp'}


QGIS and OS versions
QGIS 3.12.2-București
QGIS code revision 8a1fb33
Compiled against Qt 5.11.2
Windows 10 (10.0)

Jean-François Bourdon, ing.f.
Analyste en télédétection
Direction des inventaires forestiers
Ministère des Forêts, de la Faune et des Parcs
5700, 4e Avenue Ouest, local A-108
Québec (Québec) G1H 6R1
Téléphone : 418 627-8669, poste 4304
jean-francois.bourdon at mffp.gouv.qc.ca<mailto:jean-francois.bourdon at mffp.gouv.qc.ca>
mffp.gouv.qc.ca<http://www.mffp.gouv.qc.ca/accueil.jsp>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.osgeo.org/pipermail/qgis-developer/attachments/20200731/505dba13/attachment-0001.html>


More information about the QGIS-Developer mailing list