input($exposure_layer, name: 'exposure') as exposures_input
 -> select({ *, map($use_types,
                    cat -> if(cat = 'All Buildings' or starts_with(exposure.Use_Cat, cat) or ends_with(exposure.Use_Cat, cat), 1, 0)
                   ) as to_include })
 -> filter(max(to_include) = 1)
 -> select({exposure}) as exposures
 -> join(on: true) as exposures_join_hazards
 -> select({*, sample_closest(geometry: exposure, coverage: hazard) as hazard}) as "sample_hazard_layer"
 -> join(on: true) as exposures_join_areas
 -> select({*, sample_closest(geometry: exposure, coverage: area, buffer-distance: 1000) as area})
 -> select({*}) as sampled
 -> select({*, consequence: if(hazard > $min_depth, 1, 0) })
 -> select({*, if_null(consequence, 0) as consequence }) as event_impact_table
input(value: $hazard_layer, name: 'hazard') as hazards_input
 -> select({hazard as hazard}) as hazards
 -> exposures_join_hazards.rhs
input(relation: $area_layer, name: 'area') as areas_input
 -> group({to_coverage(area) as area}) as areas
 -> exposures_join_areas.rhs
event_impact_table
 -> select({*}) as "report_event-impact"
 -> select({ exposure.*, hazard, consequence, area.Region })
 -> save(name: 'event-impact', format: 'geopackage') as "save_event-impact"
event_impact_table
 -> select({*}) as "report_summary"
 -> select({ *, area.Region, exposure.Cons_Frame as Construction_type, exposure.Use_Cat as Use_Category, 'Total' as Nationally })
 -> group(by: { $aggregate_by as Grouping }, select: {*, count(consequence = 1) as Number_Exposed, count(exposure) as Total_number, bucket_range(pick: hazard, select: {count(*)}, range: $depths) as Depth})
 -> sort([Grouping], direction: ['ASC'])
 -> save(name: 'summary', format: 'csv') as "save_summary"
