Lisa.H.
Lisa.H.

Reputation: 29

How to create a linear distance decay (1-0) within a spatial polygon in R?

I am trying to map tranquility around industrial areas. The tranquility should be "0" within the industrial areas, increasing linearly to "1" within a buffer of 450m from the industrial areas. Is there an easy way to map this linear decrease?

I created a buffer (raster package) around the industrial areas and rasterized it, but I don´t know how to calculate the linear decay. I aim for a raster with a resolution of 25m.

x_coord <- c(16,  17,  24, 22, 16)
y_coord <- c(59, 55, 55, 61, 59)
xym <- cbind(x_coord, y_coord)

library(sp)
sps = SpatialPolygons(list(Polygons(list(Polygon(xym)),1)))
proj4string(sps) = CRS("+proj=longlat +datum=WGS84 +no_defs     +ellps=WGS84 +towgs84=0,0,0")
plot(sps)

b <- buffer(sps, width = 4)
e <- erase(b,sps)
plot(e, add=T, col="red") # here I choose 4m instead of 450m buffer width

ext <- extent(10,70,10,70)
r <- raster(ext, res=0.25) # here I choose 0.25m instead of 25m   resolution
e <- rasterize(e, r)

I would like to have a raster "e" with values that linearly increase from 0 to 1 (from inner to outer). Thankful for any help or advice!

Upvotes: 1

Views: 231

Answers (1)

Robert Hijmans
Robert Hijmans

Reputation: 47146

Perhaps something like this

library(raster)
x_coord <- c(16,  17,  24, 22, 16)
y_coord <- c(59, 55, 55, 61, 59)
xym <- cbind(x_coord, y_coord)
sps <- spPolygons(xym, crs="+proj=longlat +datum=WGS84")
b <- buffer(sps, width = 4)

ext <- extent(10,40,40,70)
r <- raster(ext, res=0.25)
e <- rasterize(sps, r)
d <- distance(e)
x <- mask(d, b)
z <- x / maxValue(x)

plot(z)
lines(sps)
lines(b)

Upvotes: 1

Related Questions