Currently, I have a .csv file with a bunch of lat/lon points that have fire radiance (FRP) values associated with each geographic point. What I would like to do is rasterize this CSV, then overlay that with a multipolygon vector layer and extract the average for each individual polygon in that layer. Here is the code I am using the accomplish this task:
library(choroplethr)
library(choroplethrMaps)
library(ggmap)
library(exactextractr) #For `exact_extract`
library(matrixStats) #For `colWeightedMeans`
library(raster) #For `brick`
library(sf) #For `st_read`
library(stringr) #For `str_sub`
library(tidyr)
library(dplyr)
library(RColorBrewer)
library(ncdf4) # package for netcdf manipulation
library(rasterVis)
library(raster) # package for raster manipulation
library(rgdal) # package for geospatial analysis
library(ggplot2) # package for plotting
library(maptools)
library(rgeos)
library(maps)
library(data.table)
library(reshape)
library(doBy)
# TOY .csv file, in real code I load in a much larger file
viirs_2020 <- read.csv(text = "frp, lat, long,
1.43,46.73021,-92.0754,
1.27,46.72907,-92.0775,
1.8,40.72152,-84.1262,
1.23,41.63972,-87.1309,
0.82,41.61905,-87.3285,
1.61,41.64,-87.4095,
1.56,41.64461,-87.141, header=TRUE")
# setting extent + projection for raster data
viirs2020_r <- raster(xmn = -125, xmx = -65, ymn = 15, ymx = 55, res = 0.75, crs = "+proj=longlat +datum=WGS84")
# converting .csv file into a raster
viirs2020_raster <- rasterize(viirs_2020[, c('long', 'lat')], viirs2020_r, viirs_2020[, 'frp'], fun=mean)
plot(viirs2020_raster)
# Load in shapefile which is a grid over a wildfire
cp_grid <- st_read('Drop Effectiveness/data/map data/cameronpeak_grid.shp')
cp_grid <- st_transform(cp_grid, 4269)
cp_grid$id <- factor(cp_grid$id)
#Function for taking the mean of values within shapes in coverage
get_frp = function(values, coverage){
values %>% summarize(across(everything(), ~ weighted.mean(.x, coverage, na.rm=TRUE)))
}
###extract the averages
out = exact_extract(viirs2020_raster, cp_grid, fun=get_frp, stack_apply=FALSE)
I've managed to do this successfully using the exact_extract function with NetCDF files in the past on different projects. But when I attempt to perform this task with the rasterized .csv file, I get the following error on the final line of the code I pasted above:
"in UseMethod("summarise") :
no applicable method for 'summarise' applied to an object of class "c('double', 'numeric')"
Anyone have an idea what could be causing this error?
I was unable to figure out how to solve this problem by changing the custom function that I wrote. However, I was able to replicate the process by doing a simple 'exact_extract' first, then calculate the weighted mean afterward.
out = exact_extract(viirs2020_raster, cp_grid)
# Create data frame from extraction
frp_combined <- bind_rows(out, .id = "id") %>%
as_tibble()
# Create a weighted mean based on values and coverage of raster
frp_by_id <- frp_combined %>%
#--- convert from character to numeric ---#
mutate(id = as.numeric(id)) %>%
#--- group summary ---#
group_by(id) %>%
summarise(frp_aw = sum(value * coverage_fraction) / sum(coverage_fraction))
# Merge extracted values to each polygon
cameronpeak.frp <- cp_grid %>%
mutate(id := seq_len(nrow(.))) %>%
left_join(., frp_by_id, by = "id") %>%
dplyr::select(id, frp_aw)