Use group step to produce a shapefile with aggregated information - help!

Hello,

I am using RiskScape to calculate risk from a large event set (NetCDF). I have managed to calculate the required metric for each exposed asset for every event but want to aggregate to get average annual risk. I have been able to use the group step to group by exposure ID and year but get the output as a table with no other information e.g. I lose the event_id and year info. How can I group the data but produce shp output? i.e. I would like a map with the average annual risk in each grid (my exposure data is gridded NetCDF).

Thanks for any advice!
Emma

Hi Emma,

It depends a bit on your input data and your type of model (Monte Carlo vs hazard-based probabilistic, etc). But presumably your exposure data will have geometry associated with it, so if you group by the whole exposure (rather than just the exposure ID), then the geometry should be retained in the final results, and you would get a shapefile output rather than CSV.

So for example, if your hazard data is event-based (aka Monte Carlo), then you can follow the pipeline example here:
https://riskscape.org.nz/docs/advanced/probabilistic/event-based.html#pipeline

However, you would need to update the group steps so that they grouped by the exposure, e.g.

event_loss_table
  ->
# sum the losses by year to get a yearly loss for each exposure
group(
    by: { exposure, year },
    select: {
        *,
        sum(total_loss) as per_year_loss
    })
  ->
# turn the yearly losses into a list, so it's easier to work with
group(
    by: exposure,
    select: {
        *,
        to_list(per_year_loss) as yearly_losses
    })
 ->
...

You would also have to make sure the subsequent select steps also all contain either ‘*’ or ‘exposure’.

Hope that helps.
Tim

1 Like

Great, that worked! Thanks very much for your help.
Emma