我正在寻找一种计算距点的距离栅格的方法,类似于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
# 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)
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.
因此,任何关于如何从该栅格到类似于第一个栅格但包含避免地面距离的栅格的想法都将不胜感激!
答案 0 :(得分:2)
我认为您可以将raster::gridDistance
与omit
参数一起使用
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,路径必须经过栅格像元的中心,所以估计距离要比实际距离长一些。