6

I am using QGIS 3.10 for Mac and facing some problems with the "Select by Expression" tool. The function I'm using is not selecting any features, even though it should. I have tested it with a few other functions, and the tool works just fine––this particular function seems to be hairy, and I'm not sure why.

I need to select features in the layer that have a certain population density. As such, I am using the following function in "Search by expression":

"TotPop_" / "sq_mi" >= 1000

I'm not sure what's wrong with the function, because it's not any selecting features at all, even though I manually went through and found a couple that apply! (The table is, unfortunately, too long for me to sift through them all manually.)

Does anyone have ideas about what I might be doing wrong?

Noura
3,4373 gold badges21 silver badges41 bronze badges
asked Mar 29, 2020 at 23:07

1 Answer 1

5

Because you are asking to select an evaluation of the division expression to get the population density, you need to to add eval expression before apply the density:

I am expecting the following expression to work:

eval("TotPop_" / "sq_mi" >= 1000)
answered Mar 29, 2020 at 23:29
8
  • 1
    Not OP, but wondering why division requires eval() and not addition/subtraction/multiplication? Commented Mar 29, 2020 at 23:38
  • 1
    @she_weeds I don't know. But yes it is strange, addition/subtraction/multiplication work with / without eval() except for division. Commented Mar 29, 2020 at 23:45
  • 3
    Investigating further, I only seem to encounter this issue with dividing fields where one or both fields are integer datatype. If both are decimal/real there it works. It also works if I use to_real() around the division expression. Also, I get erroneous results when using < rather than > with integer fields - it selects all features. I think this might be a bug. I'll lodge an issue... Commented Mar 29, 2020 at 23:59
  • 1
    @she_weeds thanks for reporting. I agree. It looks like a bug in the expression as the issue is only limited to division expression. For the OP question, at least eval() solve the problem. Commented Mar 30, 2020 at 0:04
  • 3
    Added issue on github here. github.com/qgis/QGIS/issues/35449 Curiously it only seems to be an issue with shapefiles. I did not have the same problem with a number of other data formats. Commented Mar 30, 2020 at 0:46

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.