Trying to create a Python script that processes some data. I'm running this on QGIS 3.01 on MacOS 10.12.6. Having trouble with it as every time I run the below code, it completely crashes QGIS.
All I'm trying to do is run a series of processing algorithms. If I comment out the processing.run
line, QGIS doesn't crash but also I can't do anything.
import processing
from qgis.PyQt.QtCore import QCoreApplication
from qgis.core import QgsProcessingAlgorithm, QgsProcessing, QgsProcessingParameterFeatureSink
class testAlg(QgsProcessingAlgorithm):
OUTPUT = 'OUTPUT'
def tr(self, text):
return QCoreApplication.translate('testalg', text)
def createInstance(self):
return type(self)()
def group(self):
return self.tr('Test')
def groupId(self):
return 'test'
def __init__(self):
super().__init__()
def initAlgorithm(self, config=None):
self.addParameter(QgsProcessingParameterFeatureSink(self.OUTPUT, self.tr('Output'), QgsProcessing.TypeVectorPolygon))
def name(self):
return 'testalg'
def displayName(self):
return self.tr('Test Algorithm')
def processAlgorithm(self, parameters, context, feedback):
# Commenting out the next line stops QGIS from crashing, but it means I can't actually do anything
processing.run('qgis:fieldcalculator', {'INPUT':'/Users/myusername/Documents/temp/test.geojson','FIELD_NAME':'test','FIELD_TYPE':0,'FIELD_LENGTH':10,'FIELD_PRECISION':6,'NEW_FIELD':True,'FORMULA':'( "count" / sum( "count" )) * 100','OUTPUT':'memory:'})
return {}
Am I doing something horribly wrong in my script or is this a bug in QGIS 3?
--
Edit: By crash, I mean that QGIS freezes and becomes unresponsive. I have to Force Quit it and reopen it before I can do anything else.
Edit 2: This also freeze/crashes Windows 10.
-
I'm facing the same problem. I've made a processing scrip to perform K-Means classification with sklearn. The script works perfectly on my Linux installation, but crashes my QGIS on my Mac.lcoandrade– lcoandrade2019年09月04日 20:44:03 +00:00Commented Sep 4, 2019 at 20:44
1 Answer 1
Use processing.run
in this way:
# it'll be sensible to assing the result of processing to a variable
output = processing.run(
'qgis:fieldcalculator',
{'INPUT':'/Users/myusername/Documents/temp/test.geojson',
'FIELD_NAME':'test',
'FIELD_TYPE':0,
'FIELD_LENGTH':10,
'FIELD_PRECISION':6,
'NEW_FIELD':True,
'FORMULA':'( "count" / sum( "count" )) * 100',
'OUTPUT':'memory:'},
feedback=feedback, # add this line
context=context)['OUTPUT'] # and this line # ['OUTPUT'] is optional
When I use in that way, it doesn't crash QGIS. Otherwise, QGIS freezes. (I tried in Windows 10)