1

I'm trying to set up a shapefile with a drop down for habitat type, that then autofills the corresponding code from a CSV file in the project. I've had a look at a few questions on here and thought I had it, but the dropdown menu for the code isn't populating.

this is the csv: csv attribute table

This is the shapefile attribute table: Shapefile attribute table

When I make a new shapefile, I can get the dropdown for the habitat, but it doesn't then autofill the "PrimCode" box.

This is the Value relation for habitat

Hab value relation

and this is the Value relation for PrimCode Primcode value relation

I've tried both "PrimCode" and "Code" in the filter expression, and nothing seems to be working. I'm using QGIS, version 3.28.3.

Vince
20.5k16 gold badges49 silver badges65 bronze badges
asked Jan 21 at 14:34
2
  • See here: seems to be a similar (the same?) case as here: gis.stackexchange.com/a/489540/88814 Commented Jan 21 at 15:15
  • 1
    Hi, sorry, I'm quite new to qGIS, but having looked at that one, it doesn't seem to be the same problem, but I might be wrong! I want a column to autofill based on what is selected in another column, so when someone selects a hab type, it autofills the hab code in the next column along, and pull that from a CSV that's in the project as well. I've just tried joining the CSV to the shapefile but it doesn't seem to have helped. Commented Jan 22 at 11:19

1 Answer 1

0

Part 1: Select values from csv

To be able to choose from a dropdown menu in your vector layer in the field Habitat and select the different textstrings defined in the csv layer, create a relation and set the widget accordingly:

  1. In Project properties, Add a relation field Habitat from layer csv as parent, field Habitat from layer vector as child.

    enter image description here

  2. In peroperties of layer vector, for field Habitat, set Widget type to Relation Reference and select field Habitat as Display expression and make sure you have the correct relation selected. enter image description here

Part 2: Set a second field to autofill, based on first field

  1. For Feild PrimCode, set Widget type to Text Edit and define a default value, based on what you entered in field Habitat and look up in the csv table which code is associated with this value. Use this expression: attribute(get_feature('csv', 'Habitat', "Habitat"), 'Code').

    Make sure to have checked the box Apply default value on update and uncheck the box Editable (as you want to avoid editing this field manually, it should just display the defined value).

    enter image description here

When you now select a value in the dropdown menu in the field Habitat, the according code is automaticalle introduced in field PrimCode:

enter image description here

answered Jan 22 at 14:07

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.