Bottom Page

Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
 NameError: name 'processing' is not defined
#1
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

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')  
Quote
#2
Indeed, on line 21, processing is referred to, but not defined anywhere prior to that. Where should it come from? It it a module that you import, or should it be a variable you create? I can't say I know anything about GIS, so can't help you there.

You should get into the habit of keeping track of changes to your source code by using some kind of version control system (e.g. Git). That way, you'd at least have a log of what you changed, when and why and could go back to an earlier version if needed. It's also useful if you want to work with others on the same codebase.
Quote
#3
OK thanks,
Python is part of QGIS and it calls process-modules like "native:extractbyexpression".
I have not build this myself, something must have been changed in later versions...

Might see if I can go back to an earlier version and see if that helps or see if someone in QGIS groups can give me a hint.

Regards
Quote

Top Page

Possibly Related Threads...
Thread Author Replies Views Last Post
  NameError 'odometer' not defined rfernandez15 1 87 Nov-25-2019, 09:57 PM
Last Post: ichabod801
  NameError: NameError: global name 'BPLInstruction' is not defined colt 7 341 Oct-27-2019, 07:49 AM
Last Post: Larz60+
  Function Annotation got NameError: name 'xxx' is not defined Lance 6 303 Oct-23-2019, 03:13 AM
Last Post: Lance
  NameError: name 'display' is not defined when running code on power bi beginner1 2 1,670 Jul-24-2019, 11:03 AM
Last Post: beginner1
  tkinter - "NameError: name 'frame' is not defined" Mocap 3 1,105 Jul-21-2019, 09:03 PM
Last Post: Yoriz
  [split] NameError: name 'lo' is not defined when using input ? Cedmo 2 432 Jul-05-2019, 09:39 PM
Last Post: Cedmo
  NameError: name 'lst' is not defined Jmekubo 2 829 Apr-27-2019, 12:34 PM
Last Post: Jmekubo
  *** NameError: name '' is not defined adamG 1 888 Apr-08-2019, 01:21 PM
Last Post: ichabod801
  help (variables) - NameError: name 'Payment' is not defined? Vqlk 5 582 Feb-27-2019, 04:57 PM
Last Post: buran
  NameError: name 'd' is not defined synthex 1 834 Feb-23-2019, 12:35 PM
Last Post: buran

Forum Jump:


Users browsing this thread: 1 Guest(s)