Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
"""
Standalone python script for QGIS3 on OSX.
"""
import os
import sys
# Define plugin locations from QGIS3
sys.path.append('/Applications/QGIS3.app/Contents/Resources/python/')
sys.path.append('/Applications/QGIS3.app/Contents/Resources/python/plugins')
# Define Qt5 plugin path since Qt5 can't find it
# http://osgeo-org.1560.x6.nabble.com/QGIS-3-Standalone-Python-Script-tp5373335p5373573.html
os.environ['QT_QPA_PLATFORM_PLUGIN_PATH'] = '/Applications/QGIS3.app/Contents/PlugIns'
# With path set, import QGIS3
from qgis.core import QgsApplication, QgsProcessingFeedback, QgsVectorLayer
# Initialize the app or QGIS3 will crash
QgsApplication.setPrefixPath("/Applications/QGIS3.app/Contents/MacOS", True)
app = QgsApplication([], False)
app.initQgis()
# Check if vector layer is valid to make sure everything is working
roads = sys.argv[1]
water = sys.argv[2]
vroads = QgsVectorLayer(roads, 'vroads', 'ogr')
vwater = QgsVectorLayer(water, 'vwater', 'ogr')
if not vroads.isValid():
print("Roads layer invalid.")
else:
print("Roads layer valid.")
if not vwater.isValid():
print("Water layer invalid.")
else:
print("Water layer valid.")
# Initialize processing and add native tools
from qgis.analysis import QgsNativeAlgorithms
import processing # processing causes psycopg2-binary warning
from processing.core.Processing import Processing
Processing.initialize()
app.processingRegistry().addProvider(QgsNativeAlgorithms())
# Set parameters for QGIS3 Line Intersection tool
params = {
'INPUT':roads,
'INTERSECT':water,
'INPUT_FIELDS':'ID',
'INTERSECT_FIELDS':'GFID',
'OUTPUT':sys.argv[3],
}
feedback = QgsProcessingFeedback()
print("Starting intersections test...")
results = processing.run("native:lineintersections", params, feedback=feedback)
print(results)
app.exitQgis()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.