0

I'm using the QGIS Toolbox and Python Console to write a script for parsing data.

I want to do something like this:

output_path = open('C:/Users/EH/Desktop/PYQGIS_Model_Output') # Set location to save to
#Algorithm parameters
extractANN = processing.run('qgis:extractbyattribute', {
'FIELD': 'FlagANNtraining',
'INPUT': ColourCode['OUTPUT'],
'OPERATOR': 9, # Meaning extract by "FlagANNtraining" 'is not null'
'VALUE': '',
# Attempt to save output to path, with new file name
'OUTPUT': output_path + ANN.csv })

Below is a snipped of the ColourCode['OUTPUT'] - so the column is full of 1 or NULLs, whether it has been flagged or not.

enter image description here

The extract by attribute works fine when I'm saving as 'TEMPORARY_OUTPUT', so that isn't what I'm struggling with. I need to create a the kind of function where I can simply change the output path and have a ANN.csv for example saved there, and for each other 4 extracts it will be b.csv, c.csv for example.

EDIT: Thanks BERA for the snippet below. Question #2

Code snippet that now works:

    infile = ColourCode['OUTPUT']
    output_path = ('C:/Users/EH/Desktop/PYQGIS_Model_Output')
    values = ['1']
    outprefix = 'flag_ANN_training_'


    for count, val in enumerate(values):
        extractANN = processing.run('qgis:extractbyattribute', {
        'FIELD': 'FlagANNtraining',
        'INPUT': infile,
        #'OPERATOR': 9, 
        'VALUE': val,
        'OUTPUT': os.path.join(output_path, "{1}.csv".format(outprefix, count)) })

Why is the .csv that's created called '0', thought the outprefix would determine that?

  • There is no logic using a for loop if you only have one value to loop over. It is named 0 because enumerate starts counting with 0 (which you would know i you bothered clicking the link i provided). Your question is still unclear (even when Reading the comments you have made to my answer) so Im voting to Close it. – BERA Mar 13 at 18:49
1

Your question is a little bit unclear. What is different in each extraction? The operator and/or values, or do you just want five duplicate extractions with different names?

In this example I have the field 'KKOD' and want to extract values 14 and 15 as new shapefiles. I use a for loop with enumerate to give each extraction a counting number in out filename.

import os

infile = '/home/bera/GIS/test/gradnat.shp'
field = 'KKOD'
values = ['14','15']

#If you want to split by all possible/existing values in the field you can do:
#all_field_values = list(set([f[field] for f in QgsVectorLayer(infile).getFeatures()]))

outfolder = '/home/bera/GIS/test'
outprefix = 'extraction_'

for count, val in enumerate(values):
    processing.run("native:extractbyattribute", 
    {'INPUT':infile,'FIELD':'KKOD','OPERATOR':0,'VALUE':val,
    'OUTPUT':os.path.join(outfolder, "{0}{1}.shp".format(outprefix,count))})

enter image description here

| improve this answer | |
  • The output is (going to be) 5 different .csv files, each one I'm extracting by a different column value which was created earlier on in my Class. Now the task is to extract by 1 of these attributes, lets call it "FlagExtract" for the first one. So i the hope is that I can set a file path (output_path in original question) to point the script to a folder to save to, then on the 'OUTPUT' of the processing.run section i can (?) write output_path + filename.csv - or something like that. Sorry if that's still confusing for you, but any help is appreciated :) – Snowman Mar 13 at 15:26
  • Ok nice. Edit your question and add the code you tried with and what errors you get etc. – BERA Mar 13 at 15:44
  • Done :) Hope thats clearer now. – Snowman Mar 13 at 16:35
  • Where is the for loop? – BERA Mar 13 at 16:38
  • Apolgies, thought you wanted a better description of what I had tried - Just did your example and it worked a treat. Just one question, kind of confused by your use of "Values = ['14','15']" - Did the same for mine at it worked, but not entirely sure what the use of this is? – Snowman Mar 13 at 17:16

Not the answer you're looking for? Browse other questions tagged or ask your own question.