[Qgis-user] Using aggregates functions to count intersected features from another layer

Matthias Kuhn matthias at opengis.ch
Wed Oct 19 23:14:20 PDT 2016


On 10/20/2016 12:33 AM, Nyall Dawson wrote:
> On 19 October 2016 at 22:22, Neumann, Andreas <a.neumann at carto.net> wrote:
> - attributes from the parent feature are automatically available
> within the filter, UNLESS there's a duplicate field name from the
> child feature

I like this, e.g. PostgreSQL handles it the same way for joined fields.
But it warns in case of duplication and requires an explicit source
table statement in case of duplication. In the case where the child
feature suddenly gets a new field with the same name, the semantic of
the expression should not silently change but a warning be issued.

> - expose the parent feature via the variable @parent. This could be
> used with the functions geometry(@parent), attribute(@parent,
> "some_dup_field_name")

Based on above statement, there should also be a @child variable.
Or attributes could be exposed as parent.attr / child.attr.

> 
> Then you'd be able to write:
> 
> aggregate ('mypoint', 'count', "id", intersects ($geometry, geometry(@parent)))

aggregate('mypoint', 'count', "id", mypoint.fk = @parent.id))

or

aggregate('mypoint', 'count', "id", attribute(@child, "id") =
attribute(@parent, "id"))

# Error in case both have a field "id"
aggregate('mypoint', 'count', "id", mypoint.fk = "id"))

> 
> The trickiest part in implementing this would be making sure the
> current aggregate value cache correctly handles use of parent feature
> attributes/geometry.
> 
> Re virtual layers: I've always seen these as a "heavier" (+ more
> powerful!) approach, which is OK for some use cases but for others (Eg
> data defined controls, composer labels, etc) it's preferable to have
> these calculations available directly within the expression engine.

I also think this needs to be fixed in expressions. I have a use-case
for it with default values at the moment.

Matthias



More information about the Qgis-user mailing list