找到与指定方向的空间点的最近距离

Find nearest distance from spatial point with direction specified

我想计算预定方位角 (0,45,90,135,180,225,270,315) 从空间点到空间线(或多边形)的最近距离。

这个想法是计算沿海岸线的多个海湾的暴露指数。下面提供了一个简单的例子:

创建行

library(sp)
coords<-structure(list(lon = c(-6.1468506, -3.7628174, -3.24646, 
-3.9605713, -4.4549561, -4.7955322, -4.553833, -5.9710693, -6.1468506), 
lat = c(53.884916, 54.807017, 53.46189, 53.363665, 53.507651, 53.363665, 53.126998, 53.298056,53.884916)), class = "data.frame", row.names = c(NA,-9L))
l<-Line(coords)
sl<-SpatialLines(list(Lines(list(l),ID="a")),proj4string=CRS("+init=epsg:4326"))

创建点

pt<-SpatialPoints(coords[5,]+0.02,proj4string=CRS("+init=epsg:4326"))

情节

plot(sl)
plot(pt,add=T)

我无法找到下一步的示例,需要帮助。 我想计算的距离示例

您可以使用 geosphere 库来完成它。不过,您需要在积分中添加 CRS

library(geosphere)

pt <- SpatialPoints(c[5,],
                    proj4string=CRS("+init=epsg:4326")) 

然后使用dist2Line函数:

st_distance(st_cast(sl, "POINT"), pt)

#     distance       lon      lat ID
#[1,] 2580.843 -4.451901 53.50677  1

或者,您可以使用 sf 包将多段线转换为点,然后获得距离矩阵(您需要将对象转换为 sfclass):

library(sf)

sl <- SpatialLines(list(Lines(list(l),ID="a")),
                   proj4string=CRS("+init=epsg:4326")) %>% 
  st_as_sf()

pt <- SpatialPoints(coords[5,]+0.02,
                    proj4string=CRS("+init=epsg:4326")) %>% 
  st_as_sf()

st_distance(st_cast(sl, "POINT"), pt)

#Units: [m]
#            [,1]
# [1,] 119833.165
# [2,] 149014.814
# [3,]  79215.071
# [4,]  36422.390
# [5,]   2591.267
# [6,]  30117.701
# [7,]  45287.637
# [8,] 105289.230
# [9,] 119833.165

提醒一下:当涉及到 R 中的地理数据时,我不是英雄。

另外:我没有自动计算所有方位角,而是手动执行操作以获得与 de 45 方位角相交的距离。
你必须自己弄清楚循环,因为我没有时间。完成后,请随时 provide/post 您的最终 findings/code。

这是我对这个问题的一步步破解。

#load libraries used
library(geosphere)
library(tidyverse)
library(sf)

#get bearings of lines of the polygon
df.poly <- coords %>%
  mutate( lon_next = lead(lon), lat_next = lead(lat) ) %>%
  mutate( bearing_to_next = ifelse( !is.na( lon_next ), 
                                    unlist( pmap( list( a = lon, b = lat, x = lon_next, y = lat_next ),
                                                  ~ round( geosphere::bearing( c(..1, ..2), c(..3, ..4) ) )
                                                  )
                                            ),
                                    NA ) 
          ) %>%
  filter( !is.na( lon_next ) )

#         lon      lat bearing_to_next
# 1 -6.146851 53.88492              56
# 2 -3.762817 54.80702             167
# 3 -3.246460 53.46189            -103
# 4 -3.960571 53.36366             -64
# 5 -4.454956 53.50765            -125
# 6 -4.795532 53.36366             148
# 7 -4.553833 53.12700             -78
# 8 -5.971069 53.29806             -10

#find intersection point based on the intersection of two 'great circles' 
#from two points with a bearing
gcIntersectBearing( 
  #coordinates 2nd point of polyline, with bearing to third point
  c( -3.7628174, 54.807017 ), 167,  
  #coordinates point, with bearing of 45
  c( -4.454956, 53.50765 ), 45 )

#            lon      lat      lon       lat
# [1,] -3.476074 54.07798 176.5239 -54.07798

让我们看看到目前为止我们得到了什么

p_intersect <- data.frame( lon = -3.476074, lat = 54.07798 ) %>% 
  st_as_sf( coords = c( "lon", "lat" ), crs = 4326 )

startpoint <- coords %>% slice(5) %>% mutate( lon = lon + 0.02, lat = lat + 0.02 ) %>%
  st_as_sf( coords = c("lon","lat"), crs = 4326 )

poly <- coords %>%
  as.matrix() %>%
  list() %>%
  st_polygon() %>%
  st_sfc() %>%
  st_set_crs( 4326 )

mapview::mapview( list(poly, startpoint, p_intersect) )

多边形 polystartpoint 的交点 p_intersect 与 45 度方位角的交点位置看起来是正确的。

现在您可以按如下方式计算距离:

#calculate distance
st_distance( startpoint, p_intersect )
# Units: [m]
#         [,1]
# [1,] 87993.3

Google 地图似乎在距离上一致(由于在这些点上单击鼠标,所以有一点余量,但对我来说还可以)

现在您必须想出一些聪明的办法 looping/vectorisation,您就完成了 :) 我得回去做我真正的工作了。

感谢@patL 和@Wimpel,我已根据您的建议提出了解决此问题的方法。

首先,我使用 destPoint::geosphere 从原点创建设定距离和方位角的空间线。然后我使用 gIntersection::rgeos 来获取每个横断面与海岸线相交的空间点。最后,我分别使用 gDistance::rgeos 计算每条样线从原点到所有交叉点的距离,并将最小值作为子集,即最近的交叉点。

加载包

pkgs=c("sp","rgeos","geosphere","rgdal") # list packages
lapply(pkgs,require,character.only=T) # load packages

创建数据

海岸线

coords<-structure(list(lon =c(-6.1468506,-3.7628174,-3.24646, 
-3.9605713,-4.4549561,-4.7955322,-4.553833,-5.9710693,-6.1468506), 
lat=c(53.884916,54.807017,53.46189,53.363665,53.507651,53.363665,53.126998,53.298056,53.884916)), class = "data.frame", row.names = c(NA,-9L))
l=Line(coords)
sl=SpatialLines(list(Lines(list(l),ID="a")),proj4string=CRS("+init=epsg:4326"))

sp=SpatialPoints(coords[5,]+0.02,proj4string=CRS("+init=epsg:4326"))
p=coordinates(sp) # needed for destPoint::geosphere

创建横断面线

b=seq(0,315,45) # list bearings

tr=list() # container for transect lines

for(i in 1:length(b)){
    tr[[i]]<-SpatialLines(list(Lines(list(Line(list(rbind(p,destPoint(p=p,b=b[i],d=200000))))),ID="a")),proj4string=CRS("+init=epsg:4326")) # create spatial lines 200km to bearing i from origin
    }

计算距离

minDistance=list() # container for distances


for(j in 1:length(tr)){ # for transect i
    intersects=gIntersection(sl,tr[[j]]) # intersect with coastline
    minDistance[[j]]=min(distGeo(sp,intersects)) # calculate distances and use minimum
}

do.call(rbind,minDistance)

实际上,原点是一个空间点数据框,这个过程会在多个站点循环多次。执行相交时也有一些 NULL 结果,因此循环包含一个 if 语句。