7

I want to use the algorithm output within the input expression of another algorithm in the Graphical Modeler of QGIS 3.30.3. Specifically, I want to create clusters of areas previously identified as suitable Suitable Areas with the algorithm K-means clustering. The number of clusters is defined as the quotient of the complete area of the suitable areas and the minimum cluster size Minimal Cluster Size [km²]. The graphical model looks as follows:

Graphical model

Minimal Cluster Size [km²] is a numeric input parameter. Suitable Areas is a polygon input, which is dissolved to one single feature using Dissolve, which allows the calculation of the complete area of the suitable areas. In the Advanced Python field calculator I choose Pre-calculated Value for the formula:

Advanced Python field calculator

I receive the number of clusters, I calculate the area with $area, convert it from square meters to sqaure kilometers by / 10^6 and divide it by the Minimal Cluster Size [km²] with / @minimal_cluster_size_km. I tried several versions of the formula, but I cannot figure out how to do it correctly. The Log file displays: Error encountered while running Advanced Python field calculator In the next step I want to cluster the Suitable Areas with the algorithm K-means clustering.

K-means clustering

Here I want to use the output of the Advanced Python field calculator as the number of clusters. I am also not sure if the expression is correctly applied here.


Edit:

As suggested in the comment the change from the Advanced Python field calculator to the Field calculator works and the generated output contains a column with the number of clusters. Here I also changed the Field type to integer to receive a whole number.

enter image description here

The problem I have now is accessing the calculated number of clusters within the K-means clustering. The expression @Field_calculator_OUTPUT is not working, as it uses the complete output as an input. I tried deleting the other columns of the attribute table with Refactor fields and take this as the input of the K-means clustering. But this does also not workand produces the same output as before. Is there a simple way of accessing this one specific feature with the calculated number of clusters or do I have to use another algorithm?

4
  • 1
    Welcome to GIS StackExchange! For what reason do you use the Advanced Python field calculator instead of just the Field calculator? If you really want to use the Python version, the syntax is different (Python code, actually!). But using your expression inside the normal field calculator should work (without having checked if the result is exactly what you need).
    – winnewoerp
    Dec 13, 2023 at 16:28
  • Thanks, there is no specific reason why I chose the Advanced Python field calculator over the Field calculator. I will try it tomorrow.
    – lesles93
    Dec 13, 2023 at 16:53
  • if you were able to answer your question with this, I suggest you write this answer yourself and accept it as solving your problem. In this way others can easily spot that you found what you needed.
    – Vincé
    Dec 14, 2023 at 12:39
  • Thanks for the suggestion, I will write an answer. However, I am still struggling with accessing the calculated number of clusters within the K-means clustering algorithm, which delivers 5 clusters, altough the calculated number of clusters is way higher.
    – lesles93
    Dec 14, 2023 at 13:48

1 Answer 1

1

I have found a solution to my initial problem. First I changed the Advanced Python field calculator to the Field calculator using the same expression. Than I used Refactor fields to get rid of the other columns in my attribute table. Afterwards I used Basic statistics for fields that generates an output that can be utilized as an input for the K-means clustering algorithm.

enter image description here

enter image description here

There might be a more straightforward solution to the problem.

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.