4

I discovered the nice possibility in QGIS to create dropdown lists in a field, with which I (should) choose in a simple way one of several species (in my case) - while entering new features. I tried these two possibilities:

  1. I define in attribute form the widget "unique values" - you see this screenshot enter image description here

it works (nearly) fine, while entering a new features I start typing the name of a species - and I get offered all species that I already have in this Shapefile. The offer looks like that enter image description here It is a great help like that - but: the offered species are not sorted alphabetically - and I see only 7 species at a time. Is there a possibility to change that?

  1. I tried then to choose in attribute form the widget "value relation" - I linked a csv-table with all species I need to this field. Screenshot here: enter image description here The advantage with this possibility is, while entering a new feature I get - sorted alphabetically - a great number of species, and I can choose one of them. But when I look at the attribute table it looks like that enter image description here

most of the species that has been in the shape file before - are in brackets. And the new entered species of the csv-table (and even some of the "old" species - here Meum athamanticum) are not in brackets. That is no good way to work with that further.

Has anybody an idea how to use (especially the 1. variant) more comfortable? (alphabetical, and more species in the dropdown list?

1 Answer 1

4

To get a dropdown list of values used in your layer but in an alphabetical order, one workaround is to use the Value Relation widget, but then selecting the same layer itself as the Layer under Value Relation, and the species field as the Key and Value columns.

Then to prevent duplicate values appearing in the list, adapt the following expression in the Filter Expression textbox (per this answer):

$id=array_first(array_agg($id,"species_field"))

Example:

enter image description here

enter image description here


However, in both instances, the list will only ever show you what is already in your dataset. If you only have 7 unique species that is all it will show you.

To add species that aren't already in your dataset, you can only do it using the Unique Values widget and would need to enable the 'Editable' option to add additional entries, but then you no longer have a drop down box, just an autocompleter.

For your use case I suspect you would be better off using Value Relation with a master csv of species names. However, it appears you need to use a better master list as the brackets () in your existing dataset indicate values that are in your dataset, but not your master list. I would recommend checking your master list to make sure everything is in it already.

For what it's worth there have been requests to make the Unique Values list sortable since 2018 at least, I don't know that it will be implemented just yet unless you poke one of the developers directly.


Edit: The above solution can be unusably slow on large datasets. In such instances if you still only want to use values in your source layer, try creating a Virtual Layer (Layer > Create > New Virtual Layer)

And use the following query adapted to your dataset:

SELECT species_field FROM source_layer_name GROUP BY species_field

enter image description here

Then use that resulting virtual layer in your Value Relation widget.

enter image description here

It still takes a couple of seconds for the attribute form to open for a source layer shapefile with 45000 features and about 440 unique species values.

enter image description here

The only way to improve the speed would I think be to move the dataset onto an actual server with SQL views that would load a lot faster.

5
  • thanks a lot @she_weeds - your solution with <$id=array_first(array_agg($id,"species_field"))> works in my case! The only disadvantage is, that my system (not the newest hardware) it takes up to 15 seconds until the attribute table opens (shapefile contains about 2000 features.), and also several seconds until the attribute form opens to enter a new species. But when it opens - all is fine!
    – Bertram
    Commented Dec 10, 2022 at 9:35
  • 1
    It's a good point, I just tested it out on a large dataset (>40000 features) and it's painfully slow even on a rather decent computer. I have added another alternative but it's still a bit slow, ultimately I prefer using an external static csv that has everything in there but I understand that isn't always the answer.
    – she_weeds
    Commented Dec 10, 2022 at 10:06
  • 2
    now I found a better solution (that you suggested also) - with an extern csv-table. The mistake yesterday was, that in my csv-table there were spaces - not visible - in the end of every species-name. Now I could remove the spaces with the function "TRIM" (LibreOffice Calc) - now everything works fine and there are no brackets around species-names in the attribute table. thanks for your help!
    – Bertram
    Commented Dec 10, 2022 at 13:40
  • @Bertram be aware that QGIS expressions have a trim function as well - just in case...
    – Babel
    Commented Dec 10, 2022 at 14:10
  • Yes I know. But it does not work to trim a csv table within a qgis project.
    – Bertram
    Commented Dec 10, 2022 at 16:20

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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