0

I'm trying to use the gdal:proximity function from pyQGIS algorithms.

below is my code for trying to run the algorithms:

def publicgreen_qgis(self, in_fn):
        params = {'INPUT':in_fn,
       'FIELD':'1','BURN':1,'UNITS':1,'WIDTH':10,'HEIGHT':10,
       'EXTENT':'2667.53800002985,56396.4399999964,15748.72099999,50256.3342999838 [EPSG:3414]','NODATA':0,
       'OPTIONS':'','DATA_TYPE':5,'INIT':None,
       'INVERT':False,'EXTRA':'','OUTPUT':'TEMPORARY_OUTPUT'}

        feedback = QgsProcessingFeedback()

        res = processing.run("gdal:rasterize", params, feedback=feedback)

        params = {'INPUT':res['OUTPUT'],'BAND':1,
        'VALUES':'','UNITS':0,'MAX_DISTANCE':0,'REPLACE':0,
        'NODATA':0,'OPTIONS':'','EXTRA':'','DATA_TYPE':5,'OUTPUT':"test/file/proximity.tif"}

        res2 = processing.run("gdal:proximity", params, feedback=feedback)

Have tried running on QGIS python plugin with 'OUTPUT':'TEMPORARY_OUTPUT' but still doesn't output anything.

The code runs without error in my application, however, the output file I want is not created.

EDIT1: running from QGIS python plugin works, was not paying attention to my folder, however, still no output from the application.

EDIT2: have tried changing directories, running without GUI, only gdal:rasterized would give me an output, gdal:proximity still not giving me any output. Not sure how to check if the process is running as well. Program just runs without giving me an output or exception.

EDIT3: Getting results from processing.run GRASS r.resample in PyQGIS seems to have the same problem.

Sean Ang
  • 143
  • 9

2 Answers2

2

The problem was because I didn't add Python37/Script to PYTHONPATH environment (C:\OSGeo4W64\apps\Python37\Scripts) for most people.

Not too sure why rasterize works while proximity don't.

Sean Ang
  • 143
  • 9
0

Here is my results with UI and standalone script (perhaps something wrong with your input parameters):

enter image description here

import os
import sys
import qgis.core

OSGeo_folder = r'C:\OSGeo4W64'

qgis.core.QgsApplication.setPrefixPath(os.path.join(OSGeo_folder, r'apps\qgis'), True)
qgs = qgis.core.QgsApplication([], False)
qgs.initQgis()
sys.path.append(os.path.join(OSGeo_folder, r'apps\qgis\python\plugins'))
import processing
processing.core.Processing.Processing.initialize()
qgis.core.QgsApplication.processingRegistry().addProvider(qgis.analysis.QgsNativeAlgorithms())


params = {'INPUT': r'C:\DELETE\GIS SE 3\test_UTM_42N.shp',
          'BURN': 1,
          'UNITS': 1,
          'WIDTH': 50,
          'HEIGHT': 50,
          'EXTENT': '501106.039172488,502781.3902660671,6760036.967377309,6763673.388672297 [EPSG:32642]',
          'NODATA': 0,
          'DATA_TYPE': 0,
          'INVERT': False,
          'OUTPUT': r'C:\DELETE\GIS SE 3\raster_StandAlone.tif'}

feedback = qgis.core.QgsProcessingFeedback()

res = processing.run("gdal:rasterize", params, feedback=feedback)
Comrade Che
  • 7,091
  • 27
  • 58
  • my rasterize process works fine but proximity doesn't, can't seem to find the problem though. My input parameters should be correct since it works in the python plug-in in qgis application. Thanks for the reply though! – Sean Ang May 08 '20 at 08:58
  • @SeanAng Please, edit the question topic "Using QGIS 3.12 gdal:rasterize doesn't output raster file in output location in PyQGIS standalone application". – Comrade Che May 08 '20 at 09:00