5

In the Graphical Modeler in QGIS I have the algorithm to Drop Fields, which you select the fields you want to drop:

enter image description here

However, the vector layer I'm using has many fields but I just need one in the end. I'm wondering if there's any way to trick that in order for the user to just select this one field as an Input and all the others will be deleted automatically.


EDIT: Ass suggested by @Erik, I used refactor fields but with that I'm not able to get the input value from the user.

I have an input called "Field to Keep" where the user selects the field to be kept. However when I add this input to the Source Expression it understand as a string and all the rows of this column is filled up with the name of the field chosen by the user and not with the actual field value:

enter image description here

1
  • 1
    Nice, I didn't even read that you want the user to provide a column name before you edited. – Erik Aug 7 '20 at 16:31
2

Update:

Another option would be to create a custom function (using the Function Editor):

from qgis.core import *
from qgis.gui import *

@qgsfunction(args='auto', group='Custom')
def all_fields(layer, feature, parent):
    provider = layer.dataProvider()
    field_names = [field.name() for field in provider.fields()]
    return field_names

And use this custom function in an expression like this:

array_remove_all(all_fields(@InputLayer), @InputField)

I'm aware of two options for this, but there might be (and hopefully are) nicer solutions.

Option 1:

Create two field inputs:

  1. One input with all fields selected by default.
  2. One input with the field which you want to keep.

Then use these two field inputs for the Drop field(s) algorithm as pre-calculated value with the following expression:

array_remove_all(@AllFields, @InputField)

enter image description here

enter image description here

enter image description here

enter image description here

Option 2:

Use one field input with all fields selected by default, and when running the model just deselect the field you want to keep:

enter image description here

enter image description here

enter image description here

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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