计算2 lon lats之间的距离,但避免穿过R的海岸线

时间:2015-02-18 22:43:55

标签: r geometry distance raster geography

我正在尝试计算海洋中的位置与陆地上的点之间的最近距离,但不是通过海岸线。最终,我想创建一个到陆地特征地图的距离。

此地图是使用rdist.earth创建的,是一条直线距离。因此,它并不总是正确的,因为它没有考虑到海岸线的曲率。

c<-matrix(coast_lonlat[,1], 332, 316, byrow=T)
image(1:316, 1:332, t(c))

min_dist2_feature<-NULL
for(q in 1:nrow(coast_lonlat)){
diff_lonlat <- rdist.earth(matrix(coast_lonlat[q,2:3],1,2),as.matrix(feature[,1:2]), miles = F)
min_dist2_feature<-c(min_dist2_feature, min(diff_lonlat,na.rm=T))
}

distmat <- matrix( min_dist2_feature, 316, 332)
image(1:316, 1:332, distmat)

地面要素数据是xy坐标的两列矩阵,例如:

ant_x <- c(85, 90, 95, 100)
ant_y <- c(-68, -68, -68, -68)
feature <- cbind(ant_x, ant_y)

有没有人有任何建议?感谢

1 个答案:

答案 0 :(得分:5)

尚未完全错误检查但它可能会让您入门。我认为你需要从一个不允许区域设置为NA的栅格开始,而不是海岸线。

library(raster)
library(gdistance)
library(maptools)
library(rgdal)

# a mockup of the original features dataset (no longer available)
# as I recall it, these were just a two-column matrix of xy coordinates
# along the coast of East Antarctica, in degrees of lat/long
ant_x <- c(85, 90, 95, 100)
ant_y <- c(-68, -68, -68, -68)
feature <- cbind(ant_x, ant_y)

# a projection I found for antarctica
antcrs <- crs("+proj=stere +lat_0=-90 +lat_ts=-71 +datum=WGS84")

# set projection for your features
# function 'project' is from the rgdal package
antfeat <- project(feature, crs(antcrs, asText=TRUE))

# make a raster similar to yours 
# with all land having "NA" value
# use your own shapefile or raster if you have it
# the wrld_simpl data set is from maptools package
data(wrld_simpl)
world <- wrld_simpl
ant <- world[world$LAT < -60, ]
antshp <- spTransform(ant, antcrs)
ras <- raster(nrow=300, ncol=300)
crs(ras) <- crs(antshp)
extent(ras) <- extent(antshp)
# rasterize will set ocean to NA so we just inverse it
# and set water to "1"
# land is equal to zero because it is "NOT" NA
antmask <- rasterize(antshp, ras)
antras <- is.na(antmask)

# originally I sent land to "NA"
# but that seemed to make some of your features not visible
# so at 999 land (ie everything that was zero)
# becomes very expensive to cross but not "impossible" 
antras[antras==0] <- 999
# each cell antras now has value of zero or 999, nothing else

# create a Transition object from the raster
# this calculation took a bit of time
tr <- transition(antras, function(x) 1/mean(x), 8)
tr = geoCorrection(tr, scl=FALSE)

# distance matrix excluding the land    
# just pick a few features to prove it works
sel_feat <- head(antfeat, 3)
A <- accCost(tr, sel_feat)

# now A still shows the expensive travel over land
# so we mask it out for sea travel only
A <- mask(A, antmask, inverse=TRUE)
plot(A)
points(sel_feat)

似乎工作正常,因为左侧海洋的价值高于右侧海洋,同样当您进入罗斯海时。

enter image description here

相关问题