2

I am writing a standalone QGIS python application to run some unittests. I am getting an error when I try to use the QgsCoordinateReferenceSystem library.

ERROR 4: Unable to open EPSG support file gcs.csv. Try setting the GDAL_DATA 
environment variable to point to the directory containing EPSG csv files.

I am running QGIS 3.0.3 on Windows 7.

I have made sure my GDAL_DATA path points to the gdal folder containing the gcs.csv. I have even placed a print statement above and below the call to QgsCoordinateReferenceSystem to print the environment variable which is correct as C:\Program Files\QGIS 3.0\share\gdal.

Any insight into this would be most helpful.

my full code is:

import os
from qgis.core import QgsApplication, QgsCoordinateReferenceSystem

app = QgsApplication([], True)
app.setPrefixPath(r"C:\Program Files\QGIS 3.0\apps\qgis", True)
app.initQgis()

app.setPluginPath(app.pluginPath().replace("\n", ""))
app.setPkgDataPath(app.pkgDataPath().replace("\n", ""))

print(os.environ["GDAL_DATA"])
c = QgsCoordinateReferenceSystem(4326)
print(os.environ["GDAL_DATA"])

This Python script is called from the following batch file:

@ECHO OFF

call "C:\Program Files\QGIS 3.0\bin\o4w_env.bat"
call "C:\Program Files\QGIS 3.0\bin\qt5_env.bat"
call "C:\Program Files\QGIS 3.0\bin\py3_env.bat"

set PYTHONPATH=%OSGEO4W_ROOT%\apps\qgis\python;%OSGEO4W_ROOT%\apps\qgis\python\plugins;%USERPROFILE%\AppData\Roaming\QGIS\QGIS3\profiles\default\python\plugins%PYTHONPATH%

python test.py
  • What about GEOTIFF_CSV="C:\Program Files\QGIS 3.0\share\gdal"? There is more than one GDAL system variable...where/how have you set the GDAL_DATA path? What gets printed by print(os.environ["GDAL_DATA"])? In older versions of GDAL the space in the path was a definite no-no but I think that has been fixed now. – Michael Stimson May 31 '18 at 4:31
  • GEOTIFF_CSV is set to C:\Program Files\QGIS3.0\share\gdal\epsg_csv. The GDAL_DATA variable is set when running o4w_env.bat. My qgis-bin.exe works fine, no problems, it is only when i run it as a standalone application that it has a problem, so i believe my gdal installation is fine. – Kezah May 31 '18 at 4:38
  • 1
    And PROJ_LIB? That should then be C:\Program Files\QGIS3.0\share\gdal\proj. Are these being overwritten somehow... try for Env in ['GDAL_DATA' ,'GDAL_DRIVER_PATH','GEOTIFF_CSV','PROJ_LIB']: print ('{} : {}'.format(Env,os.environ.get(Env))) early in the program to check the values haven't been corrupted. – Michael Stimson May 31 '18 at 4:41
  • All the paths look good before an after. However it seems after running app.initQgis() all the paths are appended with a line break (\n). By forcefully setting os.environ["GDAL_DATA"] = os.environ["GDAL_PATH"].replace("\n", "") it removes the error. I dont know why initQgis() is adding line breaks. – Kezah May 31 '18 at 4:51
  • Thanks for you help, I did not notice the line breaks until I printed out all the variables in your loop. – Kezah May 31 '18 at 4:55
4

My GDAL_DATA environment variable was wrong, it had a line break (\n) at the end causing my error. I fixed it by forcefully changing the environment variable to remove the line break.

QgsApplication.instance().initQgis() appears to add line breaks to the end of all my environment paths. I have patched this by replacing the line break in each variable manually.

Please see complete code below:

import os
from qgis.core import QgsApplication, QgsCoordinateReferenceSystem

app = QgsApplication([], True)
app.setPrefixPath(r"C:\Program Files\QGIS 3.0\apps\qgis", True)
app.initQgis()

app.setPluginPath(app.pluginPath().replace("\n", ""))
app.setPkgDataPath(app.pkgDataPath().replace("\n", ""))

# With line break at the end
print(os.environ["GDAL_DATA"])

os.environ["GDAL_DATA"] = os.environ["GDAL_DATA"].replace("\n", "")
# Without line break
print(os.environ["GDAL_DATA"])

c = QgsCoordinateReferenceSystem(4326)

EDIT 6/6/2018 I found the problem to occur when after import qgis, this call seems to change the system variables. I have solved this by copying the environment variables before importing and then replacing them after.

import os

environment_variables = os.environ.copy()
import qgis
os.environ = environment_variables

from qgis.core import QgsApplication, QgsCoordinateReferenceSystem

app = QgsApplication([], True)
app.setPrefixPath(r"C:\Program Files\QGIS 3.0\apps\qgis", True)
app.initQgis()

c = QgsCoordinateReferenceSystem(4326)
  • That's great, I'm sure there will be many to come with the same problem. – Michael Stimson May 31 '18 at 5:21

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.