input(relation: 'input.shp', name: 'exposure') as exposures_input
 -> select({*, sample(geometry: exposure.the_geom, coverage: bookmark('hazard.tif')) as hazard_sampled}) as sampled
# check we can write the sampled geometry to file. This will fail if it has different geometry types
 -> select({*, map(hazard_sampled, hv -> hv.geometry) as the_geom2})
 -> unnest(the_geom2)
 -> select({the_geom2 as the_geom})
 -> save(name: 'hazard_geom', format: 'shapefile')

# sanity-check we calculate the correct exposed_ratio 
sampled
 -> select({*, map(hazard_sampled, hv -> measure(hv.geometry) / measure(exposure)) as exposed_ratio})
 -> select({sum(exposed_ratio) as exposed_ratio})
 -> save(name: 'exposed', format: 'csv')
