寻找点的向量和R中的多边形之间的最近距离 [英] Finding the nearest distances between a vector of points and a polygon in R
问题描述
我有一个经纬度坐标的数据框和一个代表海岸线的多边形.我试图找到每个点和最近的海岸线特征之间的距离.我想最后得到一个输出数据框,其中包括原始纬度/经度值的列和新的距离列.
I have a data frame of lat/long coordinates and a polygon, which represents a coastline. I am trying to find the distance between each point and the nearest coastline feature. I would like to end up with an output data frame that includes columns for my original lat/long values and a new distance column.
在在线阅读类似问题的答案后,我尝试使用gDistance
函数,但是我认为我缺少一些步骤,无法弄清楚.目前,我只得到一个距离值.我对R很陌生,非常感谢任何人都可以提供的任何帮助.
I have attempted to use the gDistance
function after reading answers to similar questions online, but I think that I am missing a few steps and am having trouble figuring it out. At present, I only end up with a single distance value. I am quite new to R and would really appreciate any help that anyone might be able to give.
谢谢!
#Load data
Locs = structure(list(id = 1:5, Lat = c(29.59679167, 29.43586667, 29.37642222,29.52786111, 30.10603611), Long = c(-81.02547778, -80.92573889,
-80.97714167, -81.08721667, -80.94368611)), .Names = c("id","Lat", "Long"), class = "data.frame", row.names = c(NA, -5L))
#Extract lat/long coordinates
xy = Locs[,c("Lat","Long")]
#Create SpatialPointsDataFrame from xy data and change projection to metres
spdf = SpatialPointsDataFrame(coords=xy, data=xy, proj4string = CRS("+proj=aea +zone=17 ellps=WGS84"))
#Read in shapefile as a spatialdataframe object
coast = readOGR(dsn="land data", layer="coast")
#Transform to AEA (m) projection to match projection of points
land_poly = spTransform(coast, CRS("+proj=aea +zone=17 ellps=WGS84"))
#OR load map from map package (but unfortunately map objects do not work in gDistance)
library(maps)
library(mapdata)
coast2 = map('usa', col = "grey90", fill=TRUE)
#Calculate distance between each point and the nearest land feature
for(i in 1:dim(spdf)[1]){
g = gDistance(spdf[i,],land_poly)
}
使用下面的AEF代码更改(对于for循环步骤),我能够获得每一行的gDistance值,但是输出距离不正确(请参见下文).根据arcGIS,它们应该在4-37公里之间,而不是> 500公里之间.对我在这里做错的事情有任何想法吗?我的陆地多边形和点都在同一投影中.
Using AEF's code alterations below (for the for loop step), I am able to get gDistance values for each row, however the output distances are not correct (see below). According to arcGIS they should be between 4-37km, not >500km. Any thoughts on what I am doing wrong here? My land polygon and points are both in the same projection.
gDistance输出
gDistance output
id Lat Long dist_gDist
1 1 29.59679 -81.02548 516299.0
2 2 29.43587 -80.92574 516298.8
3 3 29.37642 -80.97714 516298.9
4 4 29.52786 -81.08722 516299.0
5 5 30.10604 -80.94369 516299.0
正确的距离(在GIS中计算)
The correct distances (calculated in GIS)
id Lat Long dist_arc
1 1 29.59679 -81.02548 13.630
2 2 29.43587 -80.92574 15.039
3 3 29.37642 -80.97714 8.111
4 4 29.52786 -81.08722 4.784
5 5 30.10604 -80.94369 36.855
推荐答案
我认为您只能得到一个距离值,因为您在for
循环的每次迭代中都覆盖了g
.但是,我不知道这是否是唯一的问题,因为如果没有适当的数据,我将无法重现您的问题.
尝试将最后一个循环更改为此:
I think you get only one distance value because you overwrite g
in every iteration of your for
-loop. I do however not not know if this is the only problem because I cannot reproduce your issue without suitable data.
Try changing the last loop to this:
g = rep(NA, dim(spdf)[1])
for(i in 1:dim(spdf)[1]){
g[i] = gDistance(spdf[i,],land_poly)
}
这篇关于寻找点的向量和R中的多边形之间的最近距离的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!