Wednesday 21 December 2016

pyqgis 3 - QGIS 3.4/3.6 standalone script


So, I have this little script (which I would like to build more upon).


I was able to make it work in QGIS3.2 (just change all paths and environments to 3.2) but now, after deleting QGIS 3.2 and installing 3.4 or 3.6 it does not work anymore.



I am able to start QgsApplication. But after testing, isValidLayer(), the result is false. So any processing creates empty output.


Environments and python is set up only for QGIS3.6 (Python37). If I install QGIS 3.2 and set it up back, all works just fine. But i am not able to change it to version 3.6. It is driving me crazy!


Any ideas?


import os, sys, datetime, glob, re, pprint
from qgis.core import *

from qgis.analysis import QgsNativeAlgorithms

sys.path.append(r'c:/SW/QGIS3.6/apps/qgis/python/plugins')


#-------------------------------------------------------------
# start QGIS processing application

QgsApplication.setPrefixPath(r'C:/SW/QGIS3.6/apps/qgis', True)
print(QgsApplication.prefixPath())
qgs = QgsApplication([], False)
qgs.initQgis()

from processing.core.Processing import Processing
Processing.initialize()

QgsApplication.processingRegistry().addProvider(QgsNativeAlgorithms())
#-------------------------------------------------------------


# folder = r'd:\01_Projects'
# files = glob.glob(folder + '/**/BUDOVY_P.shp', recursive=True)

out = r''
file = r'd:\01_Projects\BUDOVY_P.shp'



layer = QgsVectorLayer(file, 'tmp', 'ogr')
print(layer.isValid())
qgs.exitQgis()

Results is:


C:/SW/QGIS3.6/apps/qgis
False

pth file in site-packages for setting up PyCharm:



C:\SW\QGIS3.6\apps\qgis\python
C:\SW\QGIS3.6\apps\Python37\
C:\SW\QGIS3.6\apps\Python37\Lib\
C:\SW\QGIS3.6\apps\Python37\Lib\site-packages
C:\SW\QGIS3.6\bin
C:\SW\QGIS3.6\include
C:\SW\QGIS3.6\apps\qgis\bin
C:\SW\QGIS3.6\apps\qgis\python\plugins
C:\SW\QGIS3.6\apps\grass\grass-7.6.0
C:\SW\QGIS3.6\apps\Qt5\bin


env variables:


C:\SW\QGIS3.6\apps\qgis\bin
C:\SW\QGIS3.6\bin
C:\SW\QGIS3.6\apps\Python37\Scripts

The same part run in QGIS returns:


Python Console
Use iface to access QGIS API interface or Type help(iface) for more info
Security warning: typing commands from an untrusted source can lead to data loss and/or leak

file = r'd:/01_Projects/BUDOVY_P.shp'
layer = QgsVectorLayer(file, 'tmp_lyr', 'ogr')
print(layer.isValid())
True

Answer



After many hours of trial and error I found a solution (sort of). I hat to manually set environment variable for QGIS_PREFIX_PATH. But after this, I got a warning, there is a problem with gdal_data, and it can not find the gcv.csv file or something like that. Which is kind of funny, because I have it set up:


GDAL_DATA
C:\SW\QGIS3.6\share\gdal

But nevermind, QGIS can't see it even though printing it with os.environ['GDAL_DATA'] works just fine.



And with setting up the QGIS_PREFIX_PATH, you do not have to:


QgsApplication.setPrefixPath()

As you can see it is very funny problem, so the final code is:


import os, sys, datetime, glob, re, pprint
from qgis.core import *

from qgis.analysis import QgsNativeAlgorithms
sys.path.append(r'c:\SW\QGIS3.6\apps\qgis\python\plugins')
#-------------------------------------------------------------

# start QGIS processing application
os.environ['QGIS_PREFIX_PATH'] = r'C:\SW\QGIS3.6\apps\qgis'
os.environ['GDAL_DATA'] = r'C:\SW\QGIS3.6\share\gdal'

qgs = QgsApplication([], False)
QgsApplication.initQgis()

import processing
from processing.core.Processing import Processing
Processing.initialize()

QgsApplication.processingRegistry().addProvider(QgsNativeAlgorithms())
# -------------------------------------------------------------
file = r'd:\01_Projects\BUDOVY_P.shp'


layer = QgsVectorLayer(file, 'tmp', 'ogr')
print(layer.isValid())

>>True


If I add QGIS_PREFIX_PATH to env variables in windows so it is permanently there. it does not work. I have to set it manually in script. Yeah well...


I also tried calling QApplication instead of QgsApplication and I managed it to work but without additional processing. If I try to call processing module, Python crashes completely. So there was a no go.


No comments:

Post a Comment

arcpy - Changing output name when exporting data driven pages to JPG?

Is there a way to save the output JPG, changing the output file name to the page name, instead of page number? I mean changing the script fo...