4

I´d like to integrate the for loop in my model in ModelBuilder in QGIS3 (similar as the for interator in ArcGIS ModelBuilder).

data:

testdata: point layer with different classes (assignment to different classes)

hexagons: polygon layer (hexagrids)

aim:

For each class of the point layer testdata calculate the number of points per hexagrid and assign the count to the centroid of the grid where the points are situated. The result should be again 1 point layer (hexagrid centroids) with the number of points per hexagrid for each class as fields in the attribute table. If a hexagrid contains points from different classes, for each class a separate feature (separate row in the attribute table) should be available.

Example:

attribute table result

I suppose the for-loop (iteration) is not integrated as a tool in the QGIS ModelBuilder so a PyQGIS script is necessary. I´ve tried something but it didn´t worked properly.

My idea (without using PyQGIS) was the following one:

  1. select hexagrids with at least 1 point and create a new layer from the selection (extract by location)
  2. create centroids for the hexagrids
  3. join the grid ID to the points (join attributes by location)
  4. join the points to the centroids based on the grid ID (join attributes by field)
  5. split the point layer testdata by attribute (class)
  6. run the tool "Count points in polygons" in the batch mode for each splitted point layer
  7. merge the created polygons together (1 layer)
  8. join the merged hexagrid layer with the number of points per grid to the centroid layer (grid ID as the join field)

(alternatively to 6. and 7., join the hexagrid layers with the point count with the centroid layer in the batch mode and then merge them together into 1 layer).

This workflow functions with a limited number of layers (point classes) but for thousands of classes this is impossible so a PyQGIS script seems as the only appropriate sollution.

Could you help me how the python script should look like or do you have any other suggestions?


Here is a picture for better understanding: enter image description here

Each centroid (red) of the hexagrid represents the number of points within the same hexagrid. The assignment to the centroid is based on the hexagrid ID (join attributes by location). If a hexagrid contains points from different classes, for each class a new centroid is created (see hexagrids in circles).

8
  • 1
    Points can't be within points, otherwise they're identical.
    – Erik
    Commented Mar 2, 2020 at 14:48
  • The number of points per hexagrid is calculated. The count is assigned to the centroid of the hexagrid. If 1 hexagrid contains points from different classes, multiple centroids (per class) for the hexagrid exist. The centroids have identical XY but different values in the attribute table. Of course they are identical. Otherwise I don´t know what do you mean.
    – Mapos
    Commented Mar 2, 2020 at 14:56
  • Step 3: "join the centroid-ID to the points which are within" - this implies that you want to join the centroid-ID to another pointlayer which is located within the grid, but not necessarily identical to the centroids.
    – Erik
    Commented Mar 2, 2020 at 15:14
  • The points in the hexagrid will be joined to the centroid. This is due to the anonymisation of the data. The points represent the addresses of the customers, so I want to join all of them to the centroid.
    – Mapos
    Commented Mar 2, 2020 at 15:38
  • 1
    Wrong explained, sorry. 1) Join attributes by location to assign the grid ID to the points. 2) join the points based on the grid ID with the grid centroid (join by attributes). So 2 steps.
    – Mapos
    Commented Mar 3, 2020 at 10:51

1 Answer 1

1

For the steps above I´ve created a model in the QGIS3 Graphical Modeler so I get for each point (per class) a centroid. For the aggregation I´ve used a workaround in R. I´ve exported the centroid layer as csv. and used the count function from the plyr package in R count(df, c("grid-ID", "point_class")). The aggregated table I can again merge with the centroid layer. But still I´m curious if the integration of the for-loop in the Graphical Modeler is possible without workaround as the PyQGIS script...

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.