matlabcat
matlabcat

Reputation: 192

How to smooth out a raster in r while maintaining the data limits?

I have a raster that looks a little 'pixel-y' and I want to create a more smoothed out version to produce a nice map. Here's an example dataset:

library(raster)
r <- raster(ncol=10, nrow=10)
values(r) <- runif(ncell(r))

I tried:

plot(r,interpolate=TRUE)

but I don't like how it looks. Then I tried:

rr <- disaggregate(r, 3, method='bilinear')

which looks alot better, but it changes the upper limit of my values. I need the new values created from the smoothing to maintain the limits of the old data (i.e. 0 to 1) Any ideas how I can do this?

Upvotes: 1

Views: 1093

Answers (1)

Robert Hijmans
Robert Hijmans

Reputation: 47391

A simple approach would be to clamp the values after resampling

library(raster)
r <- raster(nrow=10, ncol=10, xmn=0, xmx=10, ymn=0, ymx=10, vals=(1:100)/100)
rr <- disaggregate(r, 3, method='bilinear')
rr <- clamp(rr, 0, 1)

Which is probably good enough as you should only get (a few) values outside the observed range at the edges.

An alternative approach would be to use focal.

x <- disaggregate(r, 3)
f <- focal(x, matrix(1,3,3))

Upvotes: 1

Related Questions