在R中计算两个经纬度之间的距离,但要避开海岸线

4

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

此地图是使用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)

有没有人有什么建议?谢谢。

我相信您想要计算简单多边形内的最短路径?您的数据似乎是一堆坐标,其中一些是海岸线,而另一些则不是。您如何定义多边形,并且如何定义您想要距离的一对点? - J. Win.
@J.Won。我想计算网格上所有点到要素之间的最短距离,这些点不在海岸线上而是岛屿。[什么是到要素的最短距离]。 - Megan
我认为我还没有完全理解这个问题。当你说“不经过海岸线”时,是指路径必须完全在水中(多边形外部)还是完全在陆地上(多边形内部)?此外,这是矢量数据还是网格数据?如果您能发布用于制作该图像的代码,那将非常有帮助。 - J. Win.
@J.Won。是的,完全在水中(多边形外部)。 - Megan
1个回答

8

目前还没有完全进行错误检查,但这可能会让你有所启发。与其使用海岸线,我认为你需要从一个栅格开始,将禁止区域设置为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

1
我无法使用上述代码生成相同的地图,你有什么建议吗? - Megan
1
我将maptools和rgdal添加到库要求中,并添加了一个步骤来从最终栅格图像中屏蔽陆地(将所有陆地设置为NA)。 - J. Win.
很遗憾,这段代码目前还无法复现(无法获取土地特征数据)。 - Wilcar
@J.Win. 你能否更新你的回答并提供一个foo数据集吗?土地特征数据仍然不可用。 - Wilcar
我这里没有安装GIS包,但根据rgdal文档,'project'函数只需使用一个包含xy坐标的二列矩阵即可。因此,我编辑添加了从南极洲沿海地区获取的数据。据我所知,结果绘制出的图像仍然大致相同。 - J. Win.
@J.Win。对我很有效,感谢您的更新和贡献。您能看一下这个问题[https://dev59.com/Gmsz5IYBdhLWcg3wFUC_]吗? - Wilcar

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