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?
1 Answer 1
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)
-
1Not OP, but wondering why division requires
eval()
and not addition/subtraction/multiplication?she_weeds– she_weeds2020年03月29日 23:38:57 +00:00Commented 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.ahmadhanb– ahmadhanb2020年03月29日 23:45:54 +00:00Commented Mar 29, 2020 at 23:45 -
3Investigating 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...she_weeds– she_weeds2020年03月29日 23:59:14 +00:00Commented 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.ahmadhanb– ahmadhanb2020年03月30日 00:04:31 +00:00Commented Mar 30, 2020 at 0:04 -
3Added 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.she_weeds– she_weeds2020年03月30日 00:46:22 +00:00Commented Mar 30, 2020 at 0:46