且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

计算两岸之间的距离,但不要穿过R海岸线

更新时间:2023-02-14 12:55:15

尚未完全检查错误,但它可能会让您开始。我认为你需要从不使用区域设置为NA的栅格开始。

 库(栅格)
库(gdistance)
库(maptools)
库( rgdal)

#我在antarctica上找到的投影
antcrs< - crs(+ proj = stere + lat_0 = -90 + lat_ts = -71 + datum = WGS84)

#为您的特性设置投影
#function'project'来自rgdal包
antfeat< - project(feature,crs(antcrs,asText = TRUE))

#制作一个类似于你的
#的栅格,所有的土地都有NA值
#如果你有它,使用你自己的shapefile或栅格
#wrld_simpl数据集是从maptools包
data(wrld_simpl)
world ant 范围(ras)#rasterize将海洋设置为NA,因此我们将其反转为
#并将水设置为1
#land因为它是不NA
antmask< - 光栅化(antshp,ras)
antras< - is.na(antmask)

#原来是I将土地发送到不适用
#但这似乎使您的一些功能看不见
#,因此在999土地(即所有零为零)
#变得非常昂贵不可能
antras [antras == 0]< - 999
#每个单元格antras现在的值为零或999,没有其他任何

#创建Transition对象光栅
#这个计算花费了一点时间
tr tr = geoCorrection(tr,scl = FALSE)

#不包括土地的距离矩阵
#只是选择一些功能来证明它的工作原理
sel_feat A
#now A仍显示昂贵的陆地
#我们掩盖它仅适用于海上旅行
A plot(A)
points(sel_feat)

似乎有效,因为左边的海洋比右边的海洋有更高的值,同样当你进入罗斯海时也是如此。




I am trying to calculate the closest distance between locations in the ocean and points on land but not going through a coastline. Ultimately, I want to create a distance to land-features map. For example: For example http://modata.ceoe.udel.edu/dev/mcimino/ex_data/distmap.png. This map was created using rdist.earth and is a straight line distance. Therefore it is not always correct because it not taking into account the curvatures of the coastline.

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)

Here is my coastline and land feature data.

Does anyone have any suggestions? Thanks

Not fully errorchecked yet but it may get you started. Rather than coastlines, I think you need to start with a raster whose the no-go areas are set to NA.

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

# 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)

Seems to be working because the left side ocean has higher values than the right side ocean, and likewise as you go down into the Ross Sea.