I have tried to run different GRASS commands as part of a PyQGIS script in QGIS 2.18 and 2.14, without obtaining any result, e.g. for the following code:

# Open raster
fileInfo = QFileInfo(raster)
baseName = fileInfo.baseName()
filePath = str(os.path.abspath(raster))
rlayer = QgsRasterLayer(filePath, baseName)

# Define raster extent
extent = rlayer.extent()
xmin = extent.xMinimum()
xmax = extent.xMaximum()
ymin = extent.yMinimum()
ymax = extent.yMaximum()

# Open vector
vector = QgsVectorLayer(vector, vector, "ogr")

# Break each line at each point shared between 2 and more lines
cleaned = "cleaned.shp"
processing.runalg('grass7:v.clean', {"input": vector, "tool": 8,
    "GRASS_REGION_PARAMETER": "%f,%f,%f,%f" % (xmin, xmax, ymin, ymax),
    "output": cleaned})

This code doesn't throw any error (also not in the Log Message Panel > Processing). However, no output is generated and the file is not accessible with iface.addVectorLayer. I have tried different grass commands as well and running the script in the Python console of QGIS 2.14 and QGIS 2.16. A similar script works fine, if only qgis and no GRASS commands are used.

I am guessing that QGIS is not properly connected to the GRASS commands. I didn't have these problems with QGIS 2.16, which I don't have installed any more, so I didn't run this script in QGIS 2.16.

Here a related question on this question: Get results from using GRASS algorithms with the QGIS processing tool --> Running QGIS as an administrator didn't change anything

Secondly, I am wondering if it would be advisable to switch from

  • programming PyQGIS scripts, calling all processing modules via processing.runalg() and running these scripts in the QGIS Python shell to
  • programming Python scripts that call modules e.g grass.run_command and running these in the OsGeo4W shell.

I don't have much experience on that second option and find it hard to estimate additional required knowledge. But from the few examples I am finding on my first option, I get the feeling that I am not using the most common approach. Any advice on this?

  • I might have come a little closer to understanding the problem. The above problem still remains the same, but I thought this might help someone with more background knowledge on this to think of a solution: – Sophie Crommelinck Feb 8 '17 at 16:31
  • When I launch QGIS, the following error message occurs in the Log Message Panel in the Processing tab: 2017-02-08T17:01:23 2 Could not load provider: GRASS commands [...] NameError: global name 'Grass7Utils' is not defined Since in GrassUtils.py GRASS7 is not further mentionend, I removed the 7 in the script GrassUtils.py, I changed 'Grass7Utils' to 'GrassUtils': localPath = os.path.join(GrassUtils.grassPath(), 'docs/html') Now the error message above does not appear any more, when I launch QGIS. – Sophie Crommelinck Feb 8 '17 at 16:31
  • Secondly, when I launch QGIS, the following error message occurs in the Log Message Panel in the Plugins tab: Failed to load C:/OSGEO4~1/apps/qgis/plugins/grassplugin6.dll (Reason: Cannot load library C:/OSGEO4~1/apps/qgis/plugins/grassplugin6.dll: The specified module could not be found.) The same error appears for grassprovider6.dll and grassrasterprovider6.dll – Sophie Crommelinck Feb 8 '17 at 16:34
  • All these files are in the specified folder. I reinstalled all GRASS related parts and QGIS 2.18 with the OSGeo4W installer, before doing all this. When I run one of the GRASS commands (not one of the GRASS GIS 7 commands) from the Processing Toolbox, I get the following error: NameError: global name 'QUrl' is not defined – Sophie Crommelinck Feb 8 '17 at 16:34

Your Answer

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

Browse other questions tagged or ask your own question.