1

I have an output from ST-DBSCAN with the attribute "CLUSTER_SIZE". I'd like to extract only attributes belonging to clusters of size >= 10. If this condition cannot be met, then extract attributes with clusters of size >= 7. If this condition cannot be met, NULL can be returned.

I am working with the following code:

CASE
WHEN "CLUSTER_SIZE" >= 10 THEN TRUE
WHEN "CLUSTER_SIZE" >= 7 THEN TRUE
END

Since layers with CLUSTER_SIZE >= 10 will always also have CLUSTER_SIZE >= 7, the algorithm needs to stop if the first condition is already met. According to QGIS documentation for CASE, this code would provide exactly that:

CASE is used to evaluate a series of conditions and return a result for the first condition met. The conditions are evaluated sequentially, and if a condition is true, the evaluation stops, and the corresponding result is returned.

Yet outputs for layers with CLUSTER_SIZE >= 10 are instead filtered by CLUSTER_SIZE >= 7. It seems CASE is not stopping at the first condition.

I have also tried:

CASE
    WHEN "CLUSTER_SIZE" >= 10 THEN TRUE
    ELSE "CLUSTER_SIZE" >= 7
END

and

if("CLUSTER_SIZE" >= 10, TRUE, if( "CLUSTER_SIZE" >= 7, TRUE, FALSE))

But the problem persists. How can I write a conditional code for QGIS' "Extract by Expression" tool that returns if the first condition is met and continues only if it's not?

For clarification, the outputs I need are:

  • Layers only with clusters >= 10 if they have clusters of at least this size
  • Layers only with clusters >= 7 if they do not have any clusters bigger than 10

I am basically loosening my filter in case I have layers with clusters smaller than 10, but this is to be done as an exception only.

2
  • 1
    Any cluster with size >= 10 also meets your second condiction... so why have the first condition anyway?
    – Erik
    Commented Jun 5, 2023 at 9:58
  • @Erik if a layer has cluster sizes >= 7 and also >= 10, I want only clusters of size >= 10 to be extracted. Clusters of size 7, 8, 9 should only be extracted if the layer does not have clusters of size >= 10. I have edited my question so that it is hopefully clearer. Commented Jun 5, 2023 at 10:24

1 Answer 1

1

You need to check, whether you cluster size is equal to or exceeds 10, before selecting features. You can use maximum() for this task.

if(maximum("CLUSTER_SIZE")>=10,"CLUSTER_SIZE">=10,"CLUSTER_SIZE">=7)

does, what you're looking for. maximum("CLUSTER_SIZE")>=10 checks, whether your cluster sizes meet or exceed 10. If that is true, all clusters with a size 10+ are selected. Otherwise all clusters with 7+ features are selected.

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.