Skip to content
Prev 24260 / 29559 Next

How to reduce the buffering time with the function "buffer" (package raster)

So you're correct, the buffer() and distance() functions you're using will
take a long period of time.  The code below illustrates the shortcut that
I've used in the past when buffering only by the limit of one pixel, since
it uses queens-case connectance and distance between cell-centers based on
that connectance rule. This uses the gridDistance() function which is much
faster than calculating straight line distances. You can see that the
distance-based calculation takes under 40 seconds. But do notice that the
buffer it creates by setting the threshold is around both classes and is
indiscriminate of the nearest class:

library(raster)

clip_buff <- raster("clip_study_area_2010.tif")

system.time(d <- gridDistance(clip_buff, c(2,3)))
#   user  system elapsed
#  37.36   11.00   48.41

rf[(rf != 2 & rf != 3)] <- NA

## Set buffer to 10m just for fun and assign
## value of 4 to that buffered region:
rf[ d > 0 & d <= 10 ] <- 4
plot(rf)

If you want to buffer by more than one pixel dimension then you'll probably
want straight line distances and I'd suggest using GDAL and using the
gdal_proximity utility via a system() call. Alternatively, you could look
into other utilities that have better-optimized raster-based distance
tools. Hopefully this gives you some ideas on how to proceed?

Sincerely,
Forrest
On Wed, Apr 13, 2016 at 9:18 PM Nelly Reduan <nell.redu at hotmail.fr> wrote:

            

  
  
Message-ID: <CAEBQMM=+jQVff5Lhs0r==tr+-SnNmHFo8=j7kpaSG8FJLZDJFw@mail.gmail.com>
In-Reply-To: <CY1PR0501MB1772FE403FAFCFCBAE00AFC199960@CY1PR0501MB1772.namprd05.prod.outlook.com>