I've created a simple tool using the code from save model to automate processing. It runs as expected in the QGIS python console but does not work from osgeo4w shell.
I have tried the process in https://towardsdatascience.com/how-to-use-qgis-spatial-algorithms-with-python-scripts-4bf980e39898 and https://www.qgistutorials.com/en/docs/running_qgis_jobs.html
I have also tried Spyder in Conda but I keep getting ModuleNotFoundError: No module named 'qgis'
Even looking at similar questions here, I see it's been an issue for different versions and so they don't work either.
--- UPDATE
It ended up being some install error and uninstalling and re-installing QGIS worked. To run the python to allow for import qgis to work you have to open the bat file - C:\OSGeo4W\bin\python-qgis.bat and then you can import qgis however processing.run is not part of it so I still need to know how to run algs as below.
I need to run algs like
from qgis.core import *
# Points to path
alg_params = {
'CLOSE_PATH': False,
'GROUP_EXPRESSION': '',
'INPUT': outputs['CreatePointsLayerFromTable']['OUTPUT'],
'NATURAL_SORT': False,
'ORDER_EXPRESSION': '',
'OUTPUT': QgsProcessing.TEMPORARY_OUTPUT
}
outputs['PointsToPath'] = processing.run('native:pointstopath', alg_params)
print ('3_PointsToPath')
Is there a guide like the examples above?
I want to automate the running of this py file to process data weekly. So alternatively is there a way to launch qgis with this py file to process and close?
The whole code that I want to run in qgis_processing is below.
import os, glob, re
from qgis.core import *
in_dir=r'Z:/Mowing_Automated_Reporting/Input'
out_dir=r'Z:/Mowing_Automated_Reporting/Working/Draft_Output'
os.chdir(in_dir)
def Clean_Mower_Data(in_file,out_file, max_speed, max_length):
# Extract by expression
outputs = {}
max_speed_alg=''"Speed KmH" < '+ str(max_speed)+'''
print (max_speed_alg)
alg_params = {
'EXPRESSION': '"Speed KmH" < '+ str(max_speed) ,
'INPUT': in_file,
'OUTPUT': QgsProcessing.TEMPORARY_OUTPUT #'Z:/My Drive/Mangoesmapping/Spatial Projects/2022/DSC/015_DSC_Mowing_Automated_Reporting/working/1.tab',
}
outputs['ExtractByExpression']=processing.run('native:extractbyexpression', alg_params)
print ('1_ExtractByExpression')
# Create points layer from table
alg_params = {
'INPUT': outputs['ExtractByExpression']['OUTPUT'],
'MFIELD': '',
'TARGET_CRS': QgsCoordinateReferenceSystem('EPSG:4326'),
'XFIELD': 'Longitude',
'YFIELD': 'Latitude',
'ZFIELD': '',
'OUTPUT': QgsProcessing.TEMPORARY_OUTPUT
}
outputs['CreatePointsLayerFromTable'] = processing.run('native:createpointslayerfromtable', alg_params)
print ('2_CreatePointsLayerFromTable')
# Points to path
alg_params = {
'CLOSE_PATH': False,
'GROUP_EXPRESSION': '',
'INPUT': outputs['CreatePointsLayerFromTable']['OUTPUT'],
'NATURAL_SORT': False,
'ORDER_EXPRESSION': '',
'OUTPUT': QgsProcessing.TEMPORARY_OUTPUT
}
outputs['PointsToPath'] = processing.run('native:pointstopath', alg_params)
print ('3_PointsToPath')
# Explode lines
alg_params = {
'INPUT': outputs['PointsToPath']['OUTPUT'],
'OUTPUT': QgsProcessing.TEMPORARY_OUTPUT
}
outputs['ExplodeLines'] = processing.run('native:explodelines', alg_params)
print ('4_ExplodeLines')
# Extract by expression
alg_params = {
'EXPRESSION': '$length <'+str(max_length),
'INPUT': outputs['ExplodeLines']['OUTPUT'],
'OUTPUT': out_file
}
processing.run('native:extractbyexpression', alg_params)
print ('5_Extracted')
# Fix geometries
alg_params = {
'INPUT': outputs['ExtractByExpression2']['OUTPUT'],
'OUTPUT': out_file
}
processing.run('native:fixgeometries', alg_params)
print ('6_Fixed')
filelist=glob.glob('*.csv')
for file in filelist:
#Process with speed/length limits
fn=os.path.splitext(file)[0]
max_speed=30
max_length =150
fn=s=re.findall("^DeviceDataExport_(.*)_\d*-\d*-\d*.*",fn)
fn=re.sub('[^A-Za-z0-9]+_', '', fn[0])+'__s'+str(max_speed)+'_l'+str(max_length)
in_file=os.path.join(in_dir,file)
out_file=out_dir+"//"+fn+".tab"
Clean_Mower_Data(in_file,out_file, max_speed, max_length)
print ('processed: %s --to--> %s' % (file, fn))
#Process without speed/length limits
fn=os.path.splitext(file)[0]
max_speed=999
max_length =99999
fn=re.findall("^DeviceDataExport\s(.*)\s\d*-\d*-\d*.*",fn)
fn=re.sub('[^A-Za-z0-9]+', '', fn[0])
in_file=os.path.join(in_dir,file)
out_file=out_dir+"//"+fn+".tab"
Clean_Mower_Data(in_file,out_file, max_speed, max_length)
print ('processed: %s --to--> %s' % (file, fn))
qgis_process? https://docs.qgis.org/3.22/en/docs/user_manual/processing/standalone.html?#using-processing-from-the-command-line – J. Monticolo Jul 22 '22 at 04:51python-qgis-dev.batfile I get the following errors>>> import qgis File "<stdin>", line 1, in <module> File "C:\OSGeo4W\apps\qgis-dev\python\qgis\__init__.py", line 78, in <module> from qgis.PyQt import QtCore File "C:\OSGeo4W\apps\qgis-dev\python\qgis\PyQt\QtCore.py", line 24, in <module> from PyQt5.QtCore import * ImportError: DLL load failed while importing QtCore: The specified module could not be found.– GeorgeC Jul 22 '22 at 08:26import qgisbut doesn't allowprocessing.runas it says'processing' is not defined– GeorgeC Jul 23 '22 at 04:18qgis/python/plugins/directory would be the cleanest way. – bugmenot123 Jul 25 '22 at 17:36