I have 3 layers on map, State, County, City. I'm trying to create an Atlas based print layout by the State layer, that aggregates population totals in City layer, grouped by the county.
I can easily extract the data by all the cities in a county
aggregate(
'City',
'sum',
"Population",
intersects(@geometry, geometry(@parent))
)
Now I'm trying to nest that into the state layer. The best I can come up with is to create an array of the featureid of the counties within the state:
aggregate(
'County',
'array_agg',
"geoid",
within(@geometry, geometry(@parent))
)
I then take that array and apply an array_foreach
to cycle through and run the first aggregate as the expression:
array_foreach(
aggregate(
'County',
'array_agg',
"geoid",
within(@geometry, geometry(@parent))
),
aggregate(
'City',
'sum',
"Population",
intersects(
@geometry,
geometry(get_feature('County', 'geoid', @element))
)
)
)
This function does return an array, however all values in the array are equal, and are the sum of the first county in the array.
To further test this, I removed the first aggregate
function and defined the initial array:
array_foreach(
array('123','124','125'),
aggregate(
'City',
'array_agg',
"Population",
intersects(
@geometry,
geometry(get_feature('County', 'geoid', @element))
)
)
)
The result was the same. Am I missing something on the for_each
command? Is there a better way to do this?