查找与指定方向的空间点的最近距离 [英] Find nearest distance from spatial point with direction specified
问题描述
我想计算预定方位 (0,45,90,135,180,225,270,315) 的空间点到空间线(或多边形)的最近距离.
I would like to calculate the nearest distance from a spatial point to spatial lines (or polygons) for predetermined bearings (0,45,90,135,180,225,270,315).
这个想法是计算沿海岸线的多个海湾的暴露指数.下面提供了一个简单的例子:
The idea is to calculate an exposure index for a number of bays along a coastline. A simple example is provided below:
创建线条
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)
我无法找到下一步可能需要帮助的示例.我想计算什么距离的例子
I'm having trouble finding examples of what the next step might be and need help. Example of what distance I would like to calculate
推荐答案
感谢@patL 和@Wimpel,我已经根据您的建议提出了解决此问题的方案.
Thankyou to @patL and @Wimpel, I've used your suggestions to come up with a solution to this problem.
首先,我使用 destPoint::geosphere
从原点创建设定距离和方位的空间线.然后我使用 gIntersection::rgeos
来获取每个横断面与海岸线相交的空间点.最后,我分别使用 gDistance::rgeos
计算每条横断面从原点到所有相交点的距离,并对最小值进行子集化,即最近的相交.
First I create spatial lines of set distance and bearings from an origin point using destPoint::geosphere
. I then use gIntersection::rgeos
to obtain the spatial points where each transect intersects the coastline. Finally I calculate the distance from the origin point to all intersect points for each transect line respectively using gDistance::rgeos
and subset the minimum value i.e. the nearest intersect.
加载包
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)
实际上,原点是一个空间点数据框,并且该过程针对多个站点循环多次.执行intersect时也有一些NULL结果,因此循环包含一个if语句.
In reality the origin point is a spatial point data frame and this process is looped multiple times for a number of sites. There are also a number of NULL results when carry out the intersect so the loop includes an if statement.
这篇关于查找与指定方向的空间点的最近距离的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!