1

The following script works fine from python or ipython at the command line:

from qgis.core import *
from qgis.gui import *
import sys
sys.path.append("/usr/share/qgis/python/plugins")
from PyQt4.QtCore import *
from PyQt4.QtGui import *
app = QgsApplication([], True)
QgsApplication.initQgis()
from processing.core.Processing import Processing
Processing.initialize()
import processing

layer = QgsVectorLayer('ger.shp','ger','ogr')

Where ger.shp is a shapefile in the current folder. After this, layer.isValid() is True. No warnings or messages.

But if I try and run it from the rPython package, I get an error message and the layer is not valid:

library(rPython)

qtest <- function(){
    code = "from qgis.core import *
from qgis.gui import *
import sys
sys.path.append('/usr/share/qgis/python/plugins')
from PyQt4.QtCore import *
from PyQt4.QtGui import *
app = QgsApplication([], True)
QgsApplication.initQgis()
from processing.core.Processing import Processing
Processing.initialize()
import processing

layer = QgsVectorLayer('ger.shp','ger','ogr')
"
    rPython::python.exec(code)
}

qtest()
QObject::connect: Cannot connect (null)::raiseError( QString ) to QgsVectorLayer::raiseError( QString )

and then:

python.get("layer.isValid()")
[1] FALSE 

It is identical code and identical python version, so I think it can only be in the way the code is run - perhaps the python interpreter is initialised differently from the way embedded python is run in rPython? I'm stuck.

1

I'd assumed the prefix path was correct because I didn't need to set it in the console version. Using print app.showSettings() showed me this was a false assumption.

When running from R, it printed as:

Application state:
QGIS_PREFIX_PATH env var:       
Prefix:     /usr/lib/R/bin
Plugin Path:        /usr/lib/R/bin/lib/qgis/plugins
Package Data Path:  /usr/lib/R/bin/share/qgis

which is clearly wrong. Tweaking the script to set it correctly to /usr in my case:

app = QgsApplication([], True)
app.setPrefixPath('/usr', True)
app.initQgis()
print app.showSettings()

and now the layer loads Truely.

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.