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

时间:2019-03-28 08:54:55

标签: r spatial nearest-neighbor sp

我想为预定方位(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)

我在寻找下一步可能需要帮助的示例时遇到了麻烦。 Example of what distance I would like to calculate 我要计算距离的示例

3 个答案:

答案 0 :(得分:2)

您可以使用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包将折线转换为点,然后获取距离矩阵(您需要将对象转换为sf类):

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

答案 1 :(得分:2)

单挑:关于R中的地理数据,我不是英雄。

此外:我并没有自动计算所有轴承,而是手动执行了一些操作,以获得在45度轴承上相交的距离。
您将不得不自己弄清楚循环,因为我没有时间。完成后,请随时在此处提供/发布您的最终发现/代码。

这是我逐步解决这个问题的方法。

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

enter image description here

p_intersectpoly的多边形startpoint上的交点#calculate distance st_distance( startpoint, p_intersect ) # Units: [m] # [,1] # [1,] 87993.3 的位置为45度,看起来正确。

现在您可以计算出距离,如下所示:

predictor_vars <- stack(var1, var2, var3, var4)
# create a stack of all predictor variables

study_area <- readOGR(dsn = paste0(wd, "/Data/"), layer = "study_area", stringsAsFactors = FALSE)
# read the shp-layer of the study area (has already a huge attribute table)

beginCluster(n = 3)
study_area_extract <- extract(x = predictor_vars , y = study_area , method = simple, df = TRUE)
endCluster()
# for a tutorial see URL:http://www.nickeubank.com/wp-content/uploads/2015/10/RGIS2_MergingSpatialData_part1_Joins.html
# ATTENTION: method = simple because the spatial resolution is matching!
# ATTENTION: eventually raster::getValues() to do it faster?

Google地图似乎在距离上是一致的(由于鼠标单击周围的点而产生的边距,但对我来说不错) enter image description here

现在,您将必须找出一些巧妙的循环/矢量化方法,然后完成:) 我必须回到我的真实工作中。

答案 2 :(得分:0)

感谢@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语句。