![]() ![]() That by setting up a weights matrix with focal # compute weights matrixĪ <- sort(adjacent(r, 25, 8, pairs=F, include=TRUE))ĭ <- pointDistance(axy, xyFromCell(r, 25), lonlat=F) Or, more generally, the local inverse distance weighted average. What I would really want in this case is the mean of the rook-neighbors mean(r) This is very sensitive to small changes extract(r, xyFromCell(r, 25)+0.0001, method='bilinear') Is this what the on-line calculator suggests too? That is also what resample gives resample(r, r) land/ocean), but perhaps not in this case.Įxtract(r, xyFromCell(r, 25), method='bilinear') This works well, I think, to deal with NA values at edges (e.g. The raster algorithm assigns the value below the NA cell to that cell (also 8 here). The question is which three? It is arbitrary, but to make it work, the NA cell must get a value. In this case the focal cell is NA, so you get the average of the focal cell and 3 more cells. For example, for cell 23 extract(r, xyFromCell(r, 23))Įxtract(r, xyFromCell(r, 23), method='bilinear') The problem with bilinear interpolation normally uses 4 contiguous cells, but in this case, where you want the value for the center of a cell, the appropriate cell would be the value of the cell itself, because the distance to that cell is zero, and thus that is where the interpolation ends up. Then again, in case of cells that are not approximately square, bilinear would be preferable. In this case, you may want a neighborhood-based solution. I see that you dismiss "neighborhood-based solutions rather than bilinear interpoloation". R <- raster(nrow=10, ncol=10, crs='+proj=utm +zone=1 +datum=WGS84', xmn=0, xmx=1, ymn=0, ymx=1)įor this example, you might use focal values(r) <- 1:ncell(r) Let's use equal distance cells to avoid differences because of cell size variation with lon/lat data library(raster) Window_test <- c(r, r, r)Īm I missing something here? Maybe there is something clever with the weights argument of focal() that can produce a bilinear interpolation solution? # Note that this seems to be how the moving window works with equal weights # See what the mean of the moving window producesį <- focal(r, w=matrix(1,nrow=3, ncol=3), fun=mean, NAonly=TRUE, na.rm=TRUE)į # Moving window gives 5 but bilinear interp gives 8 # Different grid values than earlier examples Note that the bilinear interpolation solution should be 8 for the example below ( here is a handy calculator for tests). However, this was not clear in the example I posted (in that example moving window and bilinear interp do give the same answer), so I'll demonstrate in a new example below. The reason is that I want to try bilinear interpolation, and I don't think a moving window average always gives the same answer as bilinear interpolation. ![]() There was a great question from Hijmans about why not use a moving window average with the focal() command in the raster package. Smat <- id(x, y, rmat, nx = 10, ny = 10) # Error about NAs # Try using bilinear interpolation but with an NA Plot(raster(smat$z), main = "interpolated") Smat <- id(x, y, rmat, nx = 10, ny = 10) # works # Use bilinear interpolation (no NAs in input) (Again, I'm interpolating to a grid the same size as the original). Here's an example parallel to the example above to demonstrate. The Akima package seems like a promising alternative to the raster approach above, but I'm having trouble if there are NAs in the input grid of values (the Z matrix). # s appears to have been filled with neighbor s # The s raster is the same size as the r raster However, the results look like nearest neighbor interpolation rather than bilinear interpolation.Īm I missing something such that there is a way to do bilinear interpolation with the raster package? Or is there a better way to do bilinear interpolation simply to fill NAs? library(raster) I found that with the raster package, I can input a grid (as a raster) that contains NAs, and use the 'resample' command to output a grid of the same size. Other potential solutions do not seem to handle NAs for the input grid of values (the 'z matrix'), or are neighborhood-based solutions rather than bilinear interpoloation, or simply have no answer. I just want to fill NAs using interpolation. Most of the solutions I have found are focused on 'upsampling' (interpolation for the purpose of increasing number of samples/size of grid), but I do not want/need to change the grid size. My grid shows autocorrelation in the x and y dimensions, so I would like to try bilinear interpolation. I have a grid that contains gaps (NAs) that I want to fill using interpolation. ![]()
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |