I have RasterLayer objects with >100k cells that I am attempting to interpolate values for based on two columns of a .csv file with 20 rows (see example data below) to create a new raster. I have created a response curve from these 20 data points using ggplot, and am trying to create a new raster based on the twenty segments within that curve. My plan was to use a function that performs a lookup (similar to the tool in ArcGIS), and interpolates the values in the raster objects that fall between the values in the csv file (which will be most of the cells). I am attempting to use raster::calc() to accomplish this, using approx() as described in this post.
I had been attempting to calculate an equation to approximate the ggplot curves, but this is rather convoluted and introduced more error into the final raster than is acceptable.
These are the two columns: 'focal_var' corresponds to the potential range of values in the input raster, and the 'pointp' column to the range of values to be assigned to the output raster.
focal_var | pointp |
---|---|
0.8052998 | 0.098823205 |
1.834359811 | 0.110267362 |
2.863419821 | 0.122470704 |
3.892479832 | 0.135436234 |
4.921539842 | 0.149168837 |
5.950599853 | 0.163676304 |
6.979659863 | 0.178970181 |
8.008719874 | 0.195066436 |
9.037779884 | 0.211985998 |
10.06683989 | 0.229755227 |
11.09589991 | 0.248406296 |
12.12495992 | 0.267977361 |
13.15401993 | 0.288512216 |
14.18307994 | 0.310058975 |
15.21213995 | 0.332667215 |
16.24119996 | 0.3563831 |
17.27025997 | 0.381242275 |
18.29931998 | 0.40726082 |
19.32837999 | 0.434425128 |
20.35744 | 0.462682111 |
As a manageable reproducible example:
library(raster)
r <- raster(ncol=10, nrow=10) #input raster
values(r) <- runif(ncell(r)) * 21
csv <- read.csv("csv_example.csv", header = TRUE) #lookup/interpolation table above
output_raster <-
calc(x = r,
fun = (with(csv,
approx(
x = focal_var,
y = pointp,
xout = r,
method = "linear",
rule = 2,
na.rm = TRUE
)))
Which returns this error:
Error in h(simpleError(msg, call)) : error in evaluating the argument 'fun' in selecting a method for function 'calc': no method for coercing this S4 class to a vector
I'm wondering if there is a function than calc() to use with rasters in combination with approx(), as there seems to be an issue with object types here, or perhaps an entirely different combination/strategy to use for interpolating rasters within R?
I'm still pretty new to coding (this is my first stack overflow question), so I appreciate any suggestions! Thanks!
That's because your function returns a data.table, which then you try to apply to one-dimensional data. In this case you can to define your function first, save it to session, then apply the function in calculation.
Lines <- "focal_var pointp
0.8052998 0.098823205
1.834359811 0.110267362
2.863419821 0.122470704
3.892479832 0.135436234
4.921539842 0.149168837
5.950599853 0.163676304
6.979659863 0.178970181
8.008719874 0.195066436
9.037779884 0.211985998
10.06683989 0.229755227
11.09589991 0.248406296
12.12495992 0.267977361
13.15401993 0.288512216
14.18307994 0.310058975
15.21213995 0.332667215
16.24119996 0.3563831
17.27025997 0.381242275
18.29931998 0.40726082
19.32837999 0.434425128
20.35744 0.462682111"
csv <- read.csv(textConnection(Lines), header = TRUE, as.is = TRUE, sep = " ")
library(raster)
#> Loading required package: sp
#> Warning: multiple methods tables found for 'direction'
#> Warning: multiple methods tables found for 'gridDistance'
r <- raster(ncol=10, nrow=10) #input raster
values(r) <- runif(ncell(r)) * 21
c <- with(csv,
approxfun(
x = focal_var,
y = pointp,
method = "linear",
rule = 2,
na.rm = TRUE
))
output_raster <- calc(x = r, function(x){c(x)})
plot(r)
plot(output_raster)
Created on 2022-01-24 by the reprex package (v2.0.1)