[Qgis-user] QGIS 3 Standalone Python Script

Chris B snowstorms at mac.com
Mon Aug 6 08:03:29 PDT 2018


Thanks so much for helping out with this.

The script now no longer fails looking for Qt, great!

But I'm still getting a very basic error -- loading a layer from the file
system doesn't work.  Here's the script:

import sys
 
sys.path.insert(0,'/Applications/QGIS3.app/Contents/Frameworks/')
sys.path.insert(0,'/Applications/QGIS3.app/Contents/Resources/python/')
sys.path.insert(0,'/Applications/QGIS3.app/Contents/Resources/python/plugins')
 
from qgis.core import (
     QgsApplication, 
     QgsProcessingFeedback, 
     QgsVectorLayer
)
from qgis.analysis import QgsNativeAlgorithms

# Start the QGIS App

qgs = QgsApplication([], False)
qgs.initQgis()

# Start Processing plugin

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

# Load Layer

layer = QgsVectorLayer('21185.shp', 'my layer', 'ogr')

if not layer.isValid():
  print("Layer failed to load!")

# Set parameters

params = {
    'INPUT': layer,
    'OUTPUT': 'memory:'
}
feedback = QgsProcessingFeedback()

# Run processing algorithm

res = processing.run("native:extractvertices", params, feedback=feedback)
res['OUTPUT'] # Access your output layer

When I test for layer validity, it says the layer fails to load and then
segfaults (which makes sense since the object is null).

I tried referring to the file locally (eg. 'file.shp') and from the root of
the drive ('/Users/[user]/path/to/script/file.shp') and neither worked.

Am I missing something for QgsVectorLayer to work?  Seems like an odd error.

Thanks again,
C.



--
Sent from: http://osgeo-org.1560.x6.nabble.com/QGIS-User-f4125267.html



More information about the Qgis-user mailing list