如何使用 R 获取距离点最近的关联多边形的信息?
我正在研究如何在形状文件中的点和多边形之间进行交集(空间连接)。我的想法是获得最近的点和多边形内完全匹配的点。在 ARGIS 中,有一个名为 CLOSEST 的匹配选项函数,它们的定义如下:“连接要素中最接近目标要素的要素被匹配。两个或多个联接要素与目标的距离可能相同当这种情况发生时,会随机选择一个连接特征作为匹配特征。”
我有一个将点相交成多边形的函数,它是由 Lyndon Estes 在 r-sig-geo 列表中慷慨贡献的,当所有多边形都填充了所有区域时,代码运行得很好。第二种情况称为空间连接距离,在 ArcGIS 中,当 match_option 为 CLOSEST 时,称为 INTERSECT,正如 ArcGIS 所做的那样。因此,当区域没有被所有多边形填充时,您可以修改点与多边形之间的最小距离。
这是第一个 INTERSECT 的数据和函数:
library(rgeos)
library(sp)
library(maptools)
library(rgdal)
library(sp)
xy.map <- readShapeSpatial("http://www.udec.cl/~jbustosm/points.shp")
manzana.map <- readShapeSpatial("http://www.udec.cl/~jbustosm/manzanas_from.shp" )
IntersectPtWithPoly <- function(x, y) {
# Extracts values from a SpatialPolygonDataFrame with SpatialPointsDataFrame, and appends table (similar to
# ArcGIS intersect)
# Args:
# x: SpatialPoints*Frame
# y: SpatialPolygonsDataFrame
# Returns:
# SpatialPointsDataFrame with appended table of polygon attributes
# Set up overlay with new column of join IDs in x
z <- overlay(y, x)
# Bind captured data to points dataframe
x2 <- cbind(x, z)
# Make it back into a SpatialPointsDataFrame
# Account for different coordinate variable names
if(("coords.x1" %in% colnames(x2)) & ("coords.x2" %in% colnames(x2))) {
coordinates(x2) <- ~coords.x1 + coords.x2
} else if(("x" %in% colnames(x2)) & ("x" %in% colnames(x2))) {
coordinates(x2) <- ~x + y
}
# Reassign its projection if it has one
if(is.na(CRSargs(x@proj4string)) == "FALSE") {
x2@proj4string <- x@proj4string
}
return(x2)
}
test<-IntersectPtWithPoly (xy.map,manzana.map)
与 Lyndon 分享一些想法,他告诉我这一点:
我认为最简单的方法是在每个点周围放置一个缓冲区(如果在,则可以指定 50 m)投影坐标),将它们转换为多边形,然后您的任务就变成两个不同多边形对象的交集。
我还没有在 R 中完成过这种类型的操作,但我怀疑您可以使用以下函数找到答案:
library(sp)
?over
library(rgeos)
?gBuffer
?gIntersects
我建议放置数据的子集来说明问题,然后也许其他人对多边形有更好的想法多边形相交/叠加可以建议该方法。
应该在形状文件中的点半径中进行设置,以便使它们进入最近的多边形。
我知道这个功能可以帮助实现它。
library(sp)
?over
library(rgeos)
?gBuffer
?gIntersects
我正在努力,因此任何评论或帮助,将不胜感激!
I'm figuring out how to do a Intersection (Spatial Join) between point and polygons from shapefiles. My idea is to get the closest points and those points that match completely inside the polygons. In ARGIS there's a function for match option named CLOSEST and they have defined by: "The feature in the join features that is closest to a target feature is matched. It is possible that two or more join features are the same distance away from the target feature. When this situation occurs, one of the join features is randomly selected as the matching feature."
I have a function to intersect points into polygons, it was kindly contributed by Lyndon Estes at the r-sig-geo list and the code works very well when all the polygons have filled all the area. The second case is known as a Spatial join distance and in ArcGIS is know as INTERSECT when match_option is CLOSEST, as ArcGIS does. So, you can modify the minimal distance between the point and the polygon when the area is not filled by all polygons.
Here's the data and the function of the first INTERSECT:
library(rgeos)
library(sp)
library(maptools)
library(rgdal)
library(sp)
xy.map <- readShapeSpatial("http://www.udec.cl/~jbustosm/points.shp")
manzana.map <- readShapeSpatial("http://www.udec.cl/~jbustosm/manzanas_from.shp" )
IntersectPtWithPoly <- function(x, y) {
# Extracts values from a SpatialPolygonDataFrame with SpatialPointsDataFrame, and appends table (similar to
# ArcGIS intersect)
# Args:
# x: SpatialPoints*Frame
# y: SpatialPolygonsDataFrame
# Returns:
# SpatialPointsDataFrame with appended table of polygon attributes
# Set up overlay with new column of join IDs in x
z <- overlay(y, x)
# Bind captured data to points dataframe
x2 <- cbind(x, z)
# Make it back into a SpatialPointsDataFrame
# Account for different coordinate variable names
if(("coords.x1" %in% colnames(x2)) & ("coords.x2" %in% colnames(x2))) {
coordinates(x2) <- ~coords.x1 + coords.x2
} else if(("x" %in% colnames(x2)) & ("x" %in% colnames(x2))) {
coordinates(x2) <- ~x + y
}
# Reassign its projection if it has one
if(is.na(CRSargs(x@proj4string)) == "FALSE") {
x2@proj4string <- x@proj4string
}
return(x2)
}
test<-IntersectPtWithPoly (xy.map,manzana.map)
Sharing some ideas with Lyndon, he told me this:
I think the easiest thing to do would be to put a buffer around each of the points (you could specify 50 m if it is in projected coordinates), converting them to polygons, and then your task becomes an intersection of two different polygon objects.
I haven't done this type of operation in R, but I suspect you could find your answer with the following functions:
library(sp)
?over
library(rgeos)
?gBuffer
?gIntersects
I suggest putting up a subset of your data illustrating the problem, and then maybe someone else who has a better idea on polygon to polygon intersects/overlays could suggest the method.
should be made in the points radius which are in the shapefile in order to make them get into the nearest polygon.
I know that this functions could help to achive it.
library(sp)
?over
library(rgeos)
?gBuffer
?gIntersects
I'm working on it, so any comment or help, would be very apreciated!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我知道可以使用 sp 和 rgeos 进行多边形到多边形的叠加。加载 sp 后,您需要加载 rgeos。
I have got that it's possible doing polygon to polygon overlays using
sp
andrgeos
. You'd need to load rgeos after you load sp.