9

I have written a python class which reads properties of map layers from an XML file and creates appropriate memory layers. I have developed and tested this code with QGIS 2.8.2 on a Windows 7 machine (importing the class at the python console and calling the class methods). As the module should be used in a Linux environment I did the same successfully with QGIS 2.8.2 on CentOs 7 - all map layers were successfully created and symbolized.

Then I refactored the code to fit in the framework of a QGIS standalone application. There are major problems with threads and such stuff, but the app starts and comes up with all layers created, and working methods.

Now comes the strange part. In the standalone CentOs version the creation of memory layers doesn't work - layers are not valid. Even a simplified test without field definitions, no crs doesn't work.

layer = QgsVectorLayer(type + '?crs=EPSG:' + str(epsg) + fields, layer_name, 'memory')

Hundreds of lines of code before and after this line are the same in all versions. Has anybody an idea how to handle this problem? Maybe a bug?

4
  • Please check this couple of answers: gis.stackexchange.com/a/142548/4972 and gis.stackexchange.com/a/132338/4972, and let me know if they solve your problem. Commented Jul 24, 2015 at 20:47
  • setPrefixPath() was set to /usr/share/qgis, where the installation went to. But it doesn't matter what value actually is, so it must be wrong The example in Erik Westras book points to the build directory of the sources. On Linux is it necessary to install Qgis sources too?
    – Detlev
    Commented Jul 24, 2015 at 21:50
  • 1
    On GNU/Linux the prefix path is usually /usr, could you please try with it? Commented Jul 24, 2015 at 22:20
  • Great! That solves this problem. I am not sure which files or directories Qgis apps are searching for at location QGIS_PREFIX. Could you elaborate this as an answer or point to some documentation. This seems to be very different between operating systems.
    – Detlev
    Commented Jul 25, 2015 at 6:29

1 Answer 1

14

To avoid "layer is not valid" errors you should carefully set your QGIS prefix path. The prefix path must be set to let QGIS know where to find resources such as data providers and the reference system database. If you look at your QGIS installation, you should be able to access lib/qgis/plugins on Unix (or just plugins on Windows), relative to the prefix path. For instance:

  • On my Ubuntu Linux the prefix path is: /usr, because providers are at /usr/lib/qgis/plugins.
  • On Windows I can access C:\\Program Files\\QGIS 3.x\\apps\\qgis-ltr\\plugins\\, so the prefix path should be C:\\Program Files\\QGIS 3.x\\apps\\qgis-ltr\\.

For reference, have a look at Viewer not displaying the data in my standalone PyQGIS Application and Getting random values from geometryType() in a standalone PyQGIS script if you want more information on this matter.

1
  • Great answer. As a related note if you want to run it in a bat file you have to add the code from python-qgis.bat to a bat file and then call the python file as python3 yourfile.py - details in gis.stackexchange.com/questions/436603/…
    – GeorgeC
    Commented Jul 25, 2022 at 23:26

Not the answer you're looking for? Browse other questions tagged or ask your own question.