Closed scra88le closed 4 years ago
Does this return what you're after?
exact_extract(raster, poly_sf, function(value, frac) {
tapply(frac, value, sum) / sum(frac)
})
Thanks! I will try it. I ended up using:
function(values, frac)
data.frame(v = values) %>%
group_by(v) %>%
tally()
)
When I run this, as the function works through the set of polygons I get this warning:
(EPSG:NA)Polygons transformed to raster CRS (EPSG:NA)Polygons transformed to raster CRS (EPSG:NA)Polygons transformed to raster CRS
Is there anything I should be concerned about here?
The raster CRS is:
+proj=tmerc +lat_0=49 +lon_0=-2 +k=0.9996012717 +x_0=400000 +y_0=-100000 +ellps=airy +towgs84=446.448,-125.157,542.06,0.1502,0.247,0.8421,-20.4894 +units=m +no_defs
The poly CRS is:
+proj=tmerc +lat_0=49 +lon_0=-2 +k=0.9996012717 +x_0=400000 +y_0=-100000 +ellps=airy +towgs84=446.448,-125.157,542.06,0.15,0.247,0.842,-20.489 +units=m +no_defs
They are very slightly different. Is that the reason?
Yes, the warning is because the CRS are different. It doesn't look significant.
If I have a raster that contain cells with integer values that represent categorical variables (say 1 to 10), and I'd like to know the percentage cover for each value type, from a subset of the raster as determined by an sf polygon, how should I go about doing this? I'd like to return a vector containing percentages (one for each value type), for a given bounding polygon. I thought it would be something along the lines of:
exact_extract(raster, poly_sf, function(value) count(value)/no_of_cells_in_poly_sf)
But I'm not sure how to extend this to return a vector for the % cover for each value type. Thank you