4

I'm trying to run a Python script from OSGeo4W Shell. Im doing this by typing "python-qgis C:\path\test.py"

Everything seems to go fine until the script launches the processing module. After that console gives me a lot of invalid escape sequence warnings.

Then when the script hits the SQL- algorithm it fails to find the referenced table. It still manages to load it.

ERROR: Referenced table file in query not found

Here is my script:

import sys

from qgis.core import (
QgsApplication, 
QgsProcessingFeedback, 
QgsVectorLayer,
QgsCoordinateReferenceSystem,
QgsProcessingParameterFeatureSource,
QgsProcessingFeedback
)

QGIS = 'C:\\Program Files\\QGIS 3.6\\apps\\qgis'


# Prefix
QgsApplication.setPrefixPath(('QGIS'),True)
qgs = QgsApplication([], False)
qgs.initQgis()

# Processing path
sys.path.append('C:\\Program Files\\QGIS 3.6\\apps\\qgis\\python\\')
sys.path.append('C:\\Program Files\\QGIS 3.6\\apps\\qgis\\python\\plugins')

path = 'O:\\Desktop\\Qgis\\File\\file.shp'

IN = QgsVectorLayer(path,'file','ogr')

if IN.isValid():
    print ('Success!')
else:
    print ('Failed!')

OUT = 'O:\\Desktop\\Area\\Leik\\A_Area.shp'

from qgis.analysis import QgsNativeAlgorithms
import processing
from processing.core.Processing import Processing
Processing.initialize()
QgsApplication.processingRegistry().addProvider(QgsNativeAlgorithms())

AO = {
            'INPUT_DATASOURCES': IN,
            'INPUT_GEOMETRY_CRS': QgsCoordinateReferenceSystem('EPSG:3883'),
            'INPUT_GEOMETRY_FIELD': '',
            'INPUT_GEOMETRY_TYPE': None,
            'INPUT_QUERY': 'SELECT \"file\".* \nFROM \"file\"\nWHERE \"file\".\"type\" like \"a%\"',
            'INPUT_UID_FIELD': '',
            'OUTPUT': OUT,
        }

feedback = QgsProcessingFeedback()

result = processing.run('qgis:executesql', AO,feedback=feedback)

print(result)

sys.exit()
1

Those warnings about invalid escape sequences are in \lib\site-packages\requests\status_codes.py and \lib\site-packages\_plotly_utils\basevalidators.py scripts and have nothing to do with your script. Upgrade QGIS to the latest stable (3.10) or LTR (3.4) release.

When running your code I didn't find invalid escape sequence warnings. The only strange path is that one ending with '...\\python\\'.
Also, you can declare raw strings (r'...') and don't escape the special characters.

All I found was an error in the AO dictionary:
In the query, the table must be named input1 (altought the name of the input layer is file).

This code run without errors here (I have changed the path to my files and proposed other way to declare the query):

import sys

from qgis.core import (
QgsApplication, 
QgsProcessingFeedback, 
QgsVectorLayer,
QgsCoordinateReferenceSystem,
QgsProcessingParameterFeatureSource,
QgsProcessingFeedback
)

QGIS = r'C:\Program Files\QGIS 3.6\apps\qgis'


# Prefix
QgsApplication.setPrefixPath(('QGIS'),True)
qgs = QgsApplication([], False)
qgs.initQgis()

# Processing path
sys.path.append(r'C:\Program Files\QGIS 3.6\apps\qgis\python')
sys.path.append(r'C:\Program Files\QGIS 3.6\apps\qgis\python\plugins')

path = r'C:\GA\GIS\test\file.shp'

IN = QgsVectorLayer(path,'file','ogr')

if IN.isValid():
    print ('Success!')
else:
    print ('Failed!')

OUT = r'C:\GA\GIS\test\A_Area.shp'

from qgis.analysis import QgsNativeAlgorithms
import processing
from processing.core.Processing import Processing
Processing.initialize()
QgsApplication.processingRegistry().addProvider(QgsNativeAlgorithms())


# Declare a query ready to be modified or include {variables}
import inspect
query = inspect.cleandoc(f'''
    SELECT "input1".*
    FROM "input1"
    WHERE "input1"."type" LIKE "a%";'''
)
print(query)


AO = {
            'INPUT_DATASOURCES': IN,
            'INPUT_GEOMETRY_CRS': QgsCoordinateReferenceSystem('EPSG:3883'),
            'INPUT_GEOMETRY_FIELD': '',
            'INPUT_GEOMETRY_TYPE': None,
            'INPUT_QUERY': query,
            'INPUT_UID_FIELD': '',
            'OUTPUT': OUT,
        }

feedback = QgsProcessingFeedback()

result = processing.run('qgis:executesql', AO,feedback=feedback)

print(result)

sys.exit()
8
  • I still get the errors and the escape warnings. Do you run the script with osgeow shell? Did you suggest that IN should be named "input1", – Henri Feb 5 '20 at 7:05
  • Yes, I run it with OSGeo4W Shell. I think that just PYTHONPATH variable may be different. No, the layer can have any name, but I need to refer to it as input1 in the algorithm. – Gabriel De Luca Feb 5 '20 at 7:08
  • Before running the script, try loading the python3 environment with py3_env command. – Gabriel De Luca Feb 5 '20 at 7:11
  • You mean. Osgeow shell -> py_env -> python-qgis C:path\test.py. Not working and I still get those errors.. what could be causing those? – Henri Feb 5 '20 at 7:24
  • Those are warnings, not errors. I am finding an installation that have them. Are warnings about invalid scape sequences in \lib\site-packages\requests\status_codes.py and \lib\site-packages\_plotly_utils\basevalidators.py scripts. I am searching if is safe to download those files fixed, but has nothing to do with your script. I mean running just py3_env command from the Shell, it loads the path for Python3. Then, run your script with python-qgis test.py. But that will not solve the status_codes.py and basevalidators.py deprecation warnings. – Gabriel De Luca Feb 5 '20 at 7:41

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.