首页 > 解决方案 > 如何在 R 中的空间多边形内创建线性距离衰减(1-0)?

问题描述

我正在尝试绘制工业区周围的宁静地图。工业区内的安宁度应为“0”,在距工业区450m的缓冲区内线性增加至“1”。有没有一种简单的方法来映射这种线性减少?

我在工业区周围创建了一个缓冲区(光栅包)并将其光栅化,但我不知道如何计算线性衰减。我的目标是分辨率为 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)

我想要一个栅格“e”,其值从 0 线性增加到 1(从内到外)。感谢您提供任何帮助或建议!

标签: r

解决方案


也许像这样

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)

推荐阅读