首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >找出距离指定方向的空间点最近的距离

找出距离指定方向的空间点最近的距离
EN

Stack Overflow用户
提问于 2019-03-28 08:54:55
回答 3查看 1.6K关注 0票数 1

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

这个想法是为海岸线上的一些海湾计算一个暴露指数。下面是一个简单的例子:

创建线条

代码语言:javascript
复制
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"))

创建点

代码语言:javascript
复制
pt<-SpatialPoints(coords[5,]+0.02,proj4string=CRS("+init=epsg:4326"))

绘图

代码语言:javascript
复制
plot(sl)
plot(pt,add=T)

我很难找到下一步可能是什么,需要帮助的例子。

我想计算的距离的例子

EN

回答 3

Stack Overflow用户

回答已采纳

发布于 2019-05-16 08:29:49

谢谢@patL和@Wimpel,我使用了您的建议来解决这个问题。

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

装载包

代码语言:javascript
复制
pkgs=c("sp","rgeos","geosphere","rgdal") # list packages
lapply(pkgs,require,character.only=T) # load packages

创建数据

海岸线

代码语言:javascript
复制
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"))

代码语言:javascript
复制
sp=SpatialPoints(coords[5,]+0.02,proj4string=CRS("+init=epsg:4326"))
p=coordinates(sp) # needed for destPoint::geosphere

创建横断面线

代码语言:javascript
复制
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
    }

计算距离

代码语言:javascript
复制
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)

实际上,原点是一个空间点数据帧,对于许多站点,这个过程被循环多次。在执行intersect时,也有许多NULL结果,因此循环包含一个if语句。

票数 0
EN

Stack Overflow用户

发布于 2019-03-28 09:38:21

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

代码语言:javascript
复制
library(geosphere)

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

然后使用dist2Line函数:

代码语言:javascript
复制
st_distance(st_cast(sl, "POINT"), pt)

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

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

代码语言:javascript
复制
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
票数 3
EN

Stack Overflow用户

发布于 2019-03-28 13:01:23

作为一个提醒:我不是英雄,当谈到地理数据在R。

此外:我还没有自动计算所有轴承,但手动执行操作,以获得距离的相交德45轴承。

你得自己找出循环,因为我没有时间。完成后,可以在这里提供/发布您的最终发现/代码。

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

代码语言:javascript
复制
#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

让我们看看到目前为止

代码语言:javascript
复制
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) )

从带有45度方位的多边形p_intersect到多边形poly上的交点poly的位置看起来是正确的。

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

代码语言:javascript
复制
#calculate distance
st_distance( startpoint, p_intersect )
# Units: [m]
#         [,1]
# [1,] 87993.3

谷歌地图似乎在距离上达成了一致(在我看来,这有点像鼠标绕点一样,但看上去没问题)

现在你必须想出一些聪明的循环/矢量化,然后你就完成了:)我必须回到我真正的工作上。

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/55393494

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档