如何找到距离R中某个点最近的多边形?

如何找到距离R中某个点最近的多边形?,r,gis,geocoding,polygons,R,Gis,Geocoding,Polygons,我有一个空间点数据框和一个空间多边形数据框。例如,我的多边形将是曼哈顿每个区块的多边形。点是人,散落在一起,有时落在街道的中间,这不是多边形的一部分。 我知道如何检查一个点是否包含在一个多边形中,但我如何才能将点指定给它们最近的多边形 ## Make some example data set.seed(1) library(raster) library(rgdal) library(rgeos) p <- shapefile(system.file("external/lux.shp

我有一个空间点数据框和一个空间多边形数据框。例如,我的多边形将是曼哈顿每个区块的多边形。点是人,散落在一起,有时落在街道的中间,这不是多边形的一部分。

我知道如何检查一个点是否包含在一个多边形中,但我如何才能将点指定给它们最近的多边形

## Make some example  data
set.seed(1)
library(raster)
library(rgdal)
library(rgeos)
p <- shapefile(system.file("external/lux.shp", package="raster"))
p2 <- as(1.5*extent(p), "SpatialPolygons")
proj4string(p2) <- proj4string(p)
pts <- spsample(p2-p, n=10, type="random")

## Plot to visualize
plot(pts, pch=16, cex=.5,col="red")
plot(p, col=colorRampPalette(blues9)(12), add=TRUE)
##制作一些示例数据
种子(1)
图书馆(光栅)
图书馆(rgdal)
图书馆(rgeos)

p这里有一个答案,它使用了一种基于mdsumner在几年前所描述的方法

一个重要的注意事项(作为2015年2月8日的编辑添加):rgeos,此处用于计算距离,预计其操作的几何图形将投影在平面坐标中。对于这些示例数据,这意味着它们应该首先转换为UTM坐标(或其他一些平面投影)。如果您错误地将数据保留在其原始lat long坐标中,则计算的距离将不正确,因为他们将纬度和经度视为长度相等

library(rgeos)

##  First project data into a planar coordinate system (here UTM zone 32)
utmStr <- "+proj=utm +zone=%d +datum=NAD83 +units=m +no_defs +ellps=GRS80"
crs <- CRS(sprintf(utmStr, 32))
pUTM <- spTransform(p, crs)
ptsUTM <- spTransform(pts, crs)

## Set up containers for results
n <- length(ptsUTM)
nearestCanton <- character(n)
distToNearestCanton <- numeric(n)

## For each point, find name of nearest polygon (in this case, Belgian cantons)
for (i in seq_along(nearestCanton)) {
    gDists <- gDistance(ptsUTM[i,], pUTM, byid=TRUE)
    nearestCanton[i] <- pUTM$NAME_2[which.min(gDists)]
    distToNearestCanton[i] <- min(gDists)
}

## Check that it worked
data.frame(nearestCanton, distToNearestCanton)
#       nearestCanton distToNearestCanton
# 1             Wiltz           15342.222
# 2        Echternach            7470.728
# 3            Remich           20520.800
# 4          Clervaux            6658.167
# 5        Echternach           22177.771
# 6          Clervaux           26388.388
# 7           Redange            8135.764
# 8            Remich            2199.394
# 9  Esch-sur-Alzette           11776.534
# 10           Remich           14998.204

plot(pts, pch=16, col="red")
text(pts, 1:10, pos=3)
plot(p, add=TRUE)
text(p, p$NAME_2, cex=0.7)
库(rgeos)
##首先将数据投影到平面坐标系中(此处为UTM分区32)

utmStr我在这里的聚会迟到了,但我刚刚找到了这条线索,为了它的价值,我提出了这个建议。RANN包中的nn2函数允许您仅在有限的半径上搜索(最近的点),这可以节省大量时间。我的建议是在多边形上添加点,将点与多边形关联,然后搜索最近的点。 看起来gDistance方法在点不多的情况下速度更快,但nn2方法可以更好地扩展到更大的问题,因为它搜索有限的半径(当然,如果该半径内没有点,它将无法找到匹配,因此必须正确选择半径)。 我是新手,所以这可能不是最好的。如果gDistance允许受限搜索,那就太好了

## Make some example  data
set.seed(1)
library(raster)
library(rgdal)
library(rgeos)
library(RANN)
library(spatialEco)

p <- shapefile(system.file("external/lux.shp", package="raster"))

##  Project data into a planar coordinate system (here UTM zone 32)
utmStr <- "+proj=utm +zone=%d +datum=NAD83 +units=m +no_defs +ellps=GRS80 +towgs84=0,0,0"
crs <- CRS(sprintf(utmStr, 32))
pUTM <- spTransform(p, crs)

# the points of interest (all within some threshold distance of the polygons)
ptsUTM <- spsample(gBuffer(pUTM,width=2000)-pUTM, n=10000, type="random")
## Plot to visualize
plot(ptsUTM, pch=16, cex=.5,col="red")
plot(pUTM, col=colorRampPalette(blues9)(12), add=TRUE)

# the gDistance method
starttime <- Sys.time()
## Set up container for results
n <- length(ptsUTM)
nearestCantons <- character(n)
## For each point, find name of nearest polygon (in this case, Belgian cantons)
for (i in seq_along(nearestCantons)) {
    nearestCantons[i] <- pUTM$NAME_2[which.min(gDistance(ptsUTM[i,], pUTM, byid=TRUE))]
}
Sys.time()-starttime

# the nn2 method
starttime <- Sys.time()
## create search points and associate with polygon attributes
rp <- spsample(pUTM,n=10000,type="regular")
rp2 <- point.in.poly(rp,pUTM)
# search for nearest point (with radius)
nn <- nn2(coordinates(rp2),coordinates(ptsUTM),k=1,searchtype="radius",radius=5000)$nn.idx
nearestCantons2 <- rp2$NAME_2[nn]
Sys.time()-starttime

##制作一些示例数据
种子(1)
图书馆(光栅)
图书馆(rgdal)
图书馆(rgeos)
图书馆(RANN)
图书馆(spatialEco)

p首先你带一些代码和数据。。。。然后我们修复它。请查看以使我们能够更轻松地回答您的问题。我不知道如何做,因为这不是一个真正的错误,而且我没有发布数据的权限。我将尝试创建一些数据。在那里,我添加了一些可复制的数据。非常感谢Josh。由于我有很多数据,它运行了一段时间,而且运行得非常完美!非常感谢你,乔希。太棒了。我还没有处理好我需要的一切。我处理了大约100000个点和1000个多边形,这大约是我必须做的事情的1%。大约花了30分钟。@Lucho--Yikes。看起来还不够快。关于更快的替代开源(QGIS?)或ArcGIS(如果你有)方法,这可能值得一问。这听起来是个好主意,谢谢!我刚刚开始了解这一点,但我有ArcGIS,所以我会尝试一下。这非常有效,只是需要注意的是,
lUTM
必须替换为
pUTM
。实际上,我是用SpatialLinesDataFrame而不是SpatialPolygonsDataFrame实现的。它同样适用于距离输出。我没有保存最近线路的标识,当然,这对线路没有意义。