Sep-07-2019, 11:27 AM
Hello python experts
I use to run this piece of code in QGIS, but it has been awhile (6 months)
It has been working perfect before but now I get this error that I can not solve.
"processing" is not defined, is the error message
Is this related to this code or I have to look into QGIS groups for an answer?
Thank you
Regards
I use to run this piece of code in QGIS, but it has been awhile (6 months)
It has been working perfect before but now I get this error that I can not solve.
"processing" is not defined, is the error message
Is this related to this code or I have to look into QGIS groups for an answer?
Thank you
Regards
from datetime import datetime trolls = '/Users/username/Desktop/test.gpkg|layername=test' rules_per_code = { 'FA_058': [("Ordertype = 2 AND PLD_1 = 1", 53), ("Ordertype = 5 AND PLD_1 = 2", 78)], 'FA_030': [("Ordertype = 2 AND PLD_1 = 1", 32), ("Ordertype = 5 AND PLD_1 = 2", 54)] } results = {} my_timestamp = datetime.strftime(datetime.now(),'%Y%m%d_%H%M%S') for code, rules in rules_per_code.items(): print('Processing {} ...'.format(code)) results[code] = [] for rule in rules: expression = rule[0] percentage = rule[1] extracted = processing.run("native:extractbyexpression", {'INPUT':trolls,'EXPRESSION':expression,'OUTPUT':'memory:'} )['OUTPUT'] random_sample = processing.run("qgis:randomextract", {'INPUT':extracted,'METHOD':1,'NUMBER':percentage,'OUTPUT':'memory:'} )['OUTPUT'] results[code].append(random_sample) for code, results in results.items(): merged = processing.run("native:mergevectorlayers", {'LAYERS':results,'CRS':None, 'OUTPUT':'/Users/username/Desktop/{}_{}.gpkg'.format(code,my_timestamp)} )['OUTPUT'] iface.addVectorLayer(merged, '', 'ogr')