4

I have a CSV spreadsheet containing thousands of rows, each corresponding to a georeferenced record of a lizard species. The dataset looks like the following table:

record   sex      age     color    lat    long
1        male     adult   yellow   33.6   -97.9
2        male     young   white    31.4   -94.1
3        female   adult   yellow   32.2   -98.1
4        female   young   gray     34.1   -69.9
...

I would like to make a map in QGIS showing the geographic distribution of each category with different symbols. For example, adult males would appear as circles, young males as squares, adult females as triangles, and young females as stars and colors would be represented by different colors (for example, a white young male would appear as a white square).

I used to do this by saving multiple CSV spreadsheets each containing one category. For example, I'd save one spreadsheet containing only records of adult males, and then categorize those adult males by color in QGIS. But I know there certainly are easier (and more intelligent) ways to do this in QGIS entering a single spreadsheet with all records, I just don't know how to do this.

What is the easiest and most organized way to do this? I've tried to use rule-based symbology, but it hasn't worked as I'd like. For example, when I use the "identify features" cursor and click on a point, it returns all records for that coordinate instead of returning only those records that are in the category being shown. Is there a way for doing this without using rule-based symbology, using instead the "categorized" option only?

1
  • 1
    How did you use the rule-based symbology and how didn't it work? Using rule-based symbology, it is easy (of course not super easy) to symbolize the layer as you mentioned in the middle paragraph. In the last paragraph, you ask a different question about identifying. Commented Mar 11, 2023 at 14:16

3 Answers 3

6

Using the rule-based symbology, you can set symbols with specific shape and color for features you mentioned.

  • Set the rules and labels as in the image, and assign an individual shape.

enter image description here

label         rule
adult male    "sex"='male'   and "age"='adult'
adult female  "sex"='female' and "age"='adult'
young female  "sex"='female' and "age"='young'
young male    "sex"='male'   and "age"='young'

Then, for every rule,

  • open the rule
  • select "Simple Marker"
  • click "Data defined override" button for "Fill color" option
  • choose "Field (type:string) > color"

enter image description here

Result:

enter image description here

6

This is a tricky one to answer as there are often lots of ways to achieve the same result! For example you could concatenate the sex, age and colour to remove the need for rule-based symbology and allow you to use categories. Which ever method you use to create your symbology you'll still find multiple points returned, unless you use a custom function to return the visibility of the categories and then set a filter on the function result.

Test if a feature is visible in QGIS expression?

Open the attribute table on your lizard layer, click on the button in the bottom left of the window ('Show All Features') and choose 'Advanced Filter (Expression).

attribute table

Click the Function Editor tab and paste the code from the above answer

function

paste the following text in to the expression tab

is_visible(@layer_name)=1

filter expression

click OK

If I click on a point with multiple values and all categories visible, I get both values returned (1 and 2) as shown in the Identify results panel on the right.

all visible

Now if I untick the visibility from category 1 and then click with the info tool on the same point as I did before, I only get the result from the visible category.

just category 2

5

You can simply define the shapes and colors by using data driven override and use case...when conditions (see second screenshot). For shapes and based on your data, use this expression:

case 
when  "sex" = 'male' and  "age" = 'young' then 'square'
when  "sex" = 'male' and  "age" = 'adult' then 'circle'
when  "sex" = 'female' and  "age" = 'adult' then 'triangle'
when  "sex" = 'female' and  "age" = 'young' then 'star'
end

When you hover with the mouse/cursor over a shape, you see how it is named: enter image description here

For the colors, use data driven override and simply insert the attribute name "color" as you have defined the color there. This is how the result looks with the data sample you provided:

enter image description here

1
  • 2
    Well, this is a better and shorter solution! Commented Mar 12, 2023 at 20:42

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.