1

Is there a way to count weighted points inside polygons for multiple attributes at one time?

I can count weight points (below) for an individual attribute, which creates a new polygon layer with an updated attribute table. I have ten attributes of the points that I need to aggregate into the polygons, so this would take a long time to do one by one.

I'd considered setting them up as batch processes, but this generates a new layer each time. I'd like to only generate a new layer once.

screenshot of point count dialogue

  • what if you proceed with a Processing Modeler using the Count points in polygon function for n-times? Where each previous output layer (n-1, n-2 etc.) will be a temporary layer and only the last output (n) will be saved a permanent layer when all the necessary attributes were used. – Taras Oct 24 '18 at 8:02
  • got this to work? – ThingumaBob Oct 25 '18 at 12:54
  • Thanks for the input. I was rushing through this few weeks ago and ended up using some ID variables in the data to aggregate in Excel. Not pretty but got the job done. – F. Lumley Nov 13 '18 at 3:36
  • For anyone encountering the same problem I think Taras's solution is the most direct. But ThingumBob's (below) is preferable if you have experience using SpatiaLite SQL. – F. Lumley Nov 13 '18 at 3:39
2

Mind using SpatiaLite SQL?

With both your layers loaded into the project

  • open the <QGIS> | Database | DB Manager...

    • expand Virtual Layers | Project Layers in the left panel

    • open a new SQL Window (2nd button from left, top row)

    • paste and run (Execute)

      SELECT py.<id_field>,
             py.<some_other_field_1>,
             ...,
             py.<some_other_field_n>,
             COUNT(pt.<id_field>) AS cnt,
             SUM(pt.<weight_attribute_1>) AS w1,
             ...,
             SUM(pt.<weight_attribute_10>) AS w10,
             py.geometry
      FROM <polygon_layer_name> AS py
      JOIN <point_layer_name> AS pt
        ON ST_Within(pt.geometry, py.geometry)
      GROUP BY py.<id_field>,
               py.<some_other_field_1>,
               ...,
               py.<some_other_field_n>
      
    • activate Add as new layer...

      • choose new Layer name and Geometry column (-> geometry)
  • <Layer_context> | Save features as... to export as e.g. shapefile

This will select all columns (fields) you specify (<some other_field_n>) from the polygon layer (<polygon_layer_name>), plus the summed up values (w1 - w10) for each <weight_attribute_n> and the count of points (cnt) that are within each polygon, into a new polygon layer; of course you can name the new fields as you like (note: for any name having special characters in it, like "my new #field", enclose the name with "").

  • took me a while to get to trying this out but thanks for the detailed answer! – F. Lumley Nov 13 '18 at 3:33

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.