3

I have a "Phenomenon" table, listing the location (Province, or admin level 3 in this case) of where a certain phenomenon was observed. I would like to return the centroid geometry from a Provinces polygon layer to this Phenomenon table using a QGIS aggregate function.

I have standardized columns with shared PCodes (admin3Pcode OR adm3Pcode) between Provinces and Phenomenon tables.

I am not getting any output from the following aggregate function and I don't understand why.

aggregate(
 layer:= 'adm3_polys', 
 aggregate:='array_agg',
 expression:=geom_to_wkt(centroid(@geometry)),
 filter:='adm3Pcode'= attribute(@parent, 'admin3Pcode')
 )
2
  • 1
    First, fix the double quoting in the filter: filter:="adm3Pcode"= attribute(@parent, 'admin3Pcode'). Single quote is used to define a constant string, double quoting refers to a field.
    – Mayo
    Jun 15, 2022 at 22:16
  • Thanks for this. I'm not sure I'm using the correct aggregate function. I think the 'collect' aggregate might be the way to return a geometry. But then I don't know what to put in the expression. I'm using GeoPackage - one geometry table and one non-geometry table. aggregate( layer:= ''adm3_polys'', aggregate:=collect(centroid($geometry)), expression:=(centroid($geometry)), filter:="admin3Pcode"=attribute(@parent,"admin3Pcode"))
    – mbela
    Jun 16, 2022 at 14:05

1 Answer 1

2

You can put the geometry in a field, but it will be more complex. I suggest you to create two fields: x_centroid and y_centroid.

Try this:

To calculate x:

aggregate(
    layer:= 'adm3_polys', 
    aggregate:='array_agg',
    expression:=x(centroid(@geometry)),
    filter:="adm3Pcode"= attribute(@parent, 'admin3Pcode')
)[0]

To calculate y:

aggregate(
    layer:= 'adm3_polys', 
    aggregate:='array_agg',
    expression:=y(centroid(@geometry)),
    filter:="adm3Pcode"= attribute(@parent, 'admin3Pcode')
)[0]

And be careful with the double quotation, it's a little bit confusing. For example: here filter:="adm3Pcode"= attribute(@parent, 'admin3Pcode')) you use double quotes for "adm3Pcode" because we are referring a field, but here attribute(@parent, 'admin3Pcode') you use single quotes because the attribute() function takes the name of the field as a string.

3
  • Thanks for this. I think it's really close but I don't have it working just yet. I'm writing to the two text fields, because we're casting each coordinate to WKT. But for some reason, there's just no output. Why do you use the 'y( ) around the larger aggregate function?
    – mbela
    Jun 16, 2022 at 14:53
  • 1
    x and y are the functions for extracting coordinates
    – Mayo
    Jun 22, 2022 at 2:17
  • 1
    Sorry if it's too late, yes you are right there was an error in my answer, I updated with the expressions fixed.
    – Mayo
    Jul 7, 2022 at 21:59

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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