计算避开陆地的距离栅格

3
我正在寻找一种计算距离点的栅格的方法,类似于raster::distance的功能,但是避免陆地。因此,就像我可以制作一个到点的距离的栅格(没有任何限制)一样:
library(sp)
library(raster)


# create cooridnates sp points
col_coord <- data.frame(x      = c(-13.8309),
                        y      = c(28.9942))
coordinates(col_coord) <- ~x+y
col_coord@proj4string <- CRS(projections$WGS84)

#create base raster
baseRaster <- raster(xmn = -80, xmx =55, ymn = -70, ymx = 70, crs = CRS("+proj=longlat +ellps=WGS84"), resolution = 1)

#rasterize spatial point
colraster <- rasterize(col_coord, baseRaster)

#calculate distance
coldist <- raster::distance(colraster, doEdge = T)

#plot
plot(coldist) 

并获得这个图像: 在此输入图像描述 我想做同样的栅格,但是避免某个区域(在这种情况下为陆地)的“最短距离”。我知道有一个叫做gdistance的包,我已经尝试按照this帖子上的建议进行,但是这是我目前的进展:(Trans是一个转换层,因为计算时间很长,可以在here找到它。
library(gdistance)
# load transition layer (change directory if needed). This is in another CRS so we transform everything
load("data/Transition_layer.RData")

col_coord <- spTransform(col_coord, Trans@crs)
A <- accCost(Trans, col_coord)
plot(A) #this shows high cost in contintents which is good

enter image description here

# create a raster for water
mapWGS <- rgeos::gBuffer(rworldmap::getMap(resolution = "high"), width = 0)
water <- rasterize(mapWGS, baseRaster, mask = F, field = 1)
table(is.na(water[]))

water[water == 1] <- 0
water[is.na(water)] <- 1
water[water == 0] <- NA
plot(water)

enter image description here

water <- projectRaster(water, crs = Trans@crs)

# change resolution of A to match that of water
A <- resample(A, water)
A2 <- mask(A, water, inverse = F)

plot(A2) # this doesn't make that much sense. 

enter image description here

所以,有什么想法可以将这个转换成类似第一个的栅格图,但包含避开陆地的距离吗?非常感谢!
1个回答

4
我认为你可以使用带有omit参数的raster::gridDistance函数。
library(raster)
xy <- data.frame(x=-13.8309, y=28.9942)
r <- raster(xmn=-120, xmx=55, ymn=-70, ymx=70, res=.25)

library(maptools)
data(wrld_simpl)

r <- rasterize(wrld_simpl, r, field= -1)
rr <- rasterize(xy, r, update=TRUE)

x <- gridDistance(rr, 1, omit=-1)
plot(x / 1000000)
points(xy)

水下距离

该地图显示此技术可用于太平洋和红海(因为 wrld_simpl 没有巴拿马运河和苏伊士运河)。

但是,由于使用 gridDistance 技术的路径必须穿过栅格单元的中心点,因此估计的距离比实际距离稍长。


如何在R代码中创建一座陆地桥,让人们从北美穿越巴拿马运河进入南美,或者从俄罗斯的阿纳德尔穿越到北美大陆? - undefined

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接