从栅格计算缓冲区内的线密度

Mar*_*ine 5 r r-raster

有没有办法计算空间线周围缓冲区内的道路密度(km /km²)?道路由光栅中的像素(1像素= 625平方米)表示.所以我开始使用该函数将道路像素转换为折线rasterToContour (package raster).然后,我正在考虑计算缓冲区内的行总长度(以km为单位)和缓冲区(以km²为单位).

r <- raster(rast_path)
x <- rasterToContour(r)
Run Code Online (Sandbox Code Playgroud)

这是一个可重复的例子:

## To create raster:
library(raster)
library(rgeos)
r <- raster(ncols=90, nrows=50)
values(r) <- sample(1:10, ncell(r), replace=TRUE)

## Road raster
r[r[] < 10] <- 0
r[r[] >= 10] <- 1
plot(r)

## To create spatial lines 
line1 <- rbind(c(-125,0), c(0,60))
line2 <- rbind(c(0,60), c(40,5))
line3 <- rbind(c(40,5), c(15,-45))
line1_sp <- spLines(line1)
line2_sp <- spLines(line2)
line3_sp <- spLines(line3)

## To create buffer around lines
line2_buff <- gBuffer(line2_sp, width=20)
plot(line2_sp,add=T)
plot(line2_buff,add=T)
Run Code Online (Sandbox Code Playgroud)

rso*_*ren 5

你正在寻找道路长度(公里)除以缓冲区(平方公里),对吧?要计算道路长度,您可以使用您的方法rasterToContour(),尽管这与您提供的示例无法重现.

但是要以平方公里计算缓冲区的面积,您可以:n <- length(extract(r, line2_buff))获取n缓冲区中的像素数,每个像素数为625平方公尺.您需要的转换因子是1平方公里^ = 1,000,000平方公尺.总之,缓冲区的面积(km ^ 2)由下式给出:

length(extract(r, line2_buff)) * 625 / 1000000
Run Code Online (Sandbox Code Playgroud)