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_commandand 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?
2017-02-08T17:01:23 2 Could not load provider: GRASS commands [...] NameError: global name 'Grass7Utils' is not definedSince in
GrassUtils.pyGRASS7 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 08 '17 at 16:31
– Sophie Crommelinck Feb 08 '17 at 16:34Failed 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 forgrassprovider6.dllandgrassrasterprovider6.dllNameError: global name 'QUrl' is not defined– Sophie Crommelinck Feb 08 '17 at 16:34