-
-
Save jgrocha/d75d50305e49ba044ff84e96a28f4ed8 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
""" | |
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