Last active
July 13, 2021 13:32
-
-
Save mdouchin/3ff5d70b5e7223bdfb78dfbeb7f872a3 to your computer and use it in GitHub Desktop.
Allows to run a QGIS processing algorithm from the CLI. Only usefull when qgis_process is not installed
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python3 | |
import os | |
import sys | |
import json | |
# variables | |
pg_service_file = '/etc/postgresql-common/pg_service.conf' | |
# Initialize needed paths | |
# to be able to load processing.core.Processing | |
TEMP_DIR = '/tmp/standalone_processing_runner/' | |
QGIS_HOME = TEMP_DIR | |
QGIS_CUSTOM_CONFIG_PATH = TEMP_DIR | |
qgisPrefixPath = os.environ.get('QGIS_PREFIX_PATH', '/usr/') | |
qgisConfigPath = os.environ.get('QGIS_CUSTOM_CONFIG_PATH', QGIS_CUSTOM_CONFIG_PATH) | |
# QGIS native plugins | |
sys.path.append(os.path.join(qgisPrefixPath, "share/qgis/python/plugins/")) | |
# QGIS user plugins | |
# uncomment if needed | |
#sys.path.append('/srv/qgis/plugins/') | |
os.environ['LC_ALL'] = "fr_FR.UTF-8" | |
os.environ['LANG'] = "fr_FR.UTF-8" | |
os.environ['HOME'] = QGIS_HOME | |
os.environ['DISPLAY'] = ":99" | |
# Initialize PostgreSQL service connection file PGSERVICEFILE | |
os.environ['PGSERVICEFILE'] = pg_service_file | |
# Import QGIS AND QT modules | |
from qgis.core import QgsApplication | |
from qgis.PyQt.QtCore import QCoreApplication, QSettings | |
from processing.core.Processing import Processing | |
from qgis.analysis import QgsNativeAlgorithms | |
# Create QGIS app | |
QgsApplication.setPrefixPath(qgisPrefixPath, True) | |
app = QgsApplication([], False, qgisConfigPath) | |
# Set QSettings format and path | |
# needed so that db_manager plugin can read the settings from QGIS3.ini | |
QCoreApplication.setOrganizationName(QgsApplication.QGIS_ORGANIZATION_NAME) | |
QCoreApplication.setOrganizationDomain(QgsApplication.QGIS_ORGANIZATION_DOMAIN) | |
QCoreApplication.setApplicationName(QgsApplication.QGIS_APPLICATION_NAME) | |
QSettings.setDefaultFormat(QSettings.IniFormat) | |
QSettings.setPath(QSettings.IniFormat, QSettings.UserScope, qgisConfigPath) | |
# Init QGIS | |
app.initQgis() | |
# Initialize processing | |
Processing.initialize() | |
# Add Processing providers | |
reg = app.processingRegistry() | |
# Native QGIS provider | |
reg.addProvider(QgsNativeAlgorithms()) | |
# Plugin provider, uncomment if needed | |
# from myplugin.processing.provider import MypluginProvider | |
# reg.addProvider(MypluginProvider()) | |
# Get parameters | |
input_alg = sys.argv[1] | |
parameters = sys.argv[2] | |
print(input_alg) | |
print(parameters) | |
input_params = json.loads(parameters) | |
# Run Alg | |
from qgis.core import QgsProcessingFeedback | |
feedback = QgsProcessingFeedback() | |
from processing import run as processing_run | |
res = processing_run( | |
input_alg, | |
input_params, | |
feedback=feedback | |
) | |
print("RESULT = %s" % json.dumps(res)) | |
# Exit | |
app.exitQgis() | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment