来自共享公共属性(唯一且一对一)的两个 sf 对象的相交多边形
我有两个 sf 对象,每个对象包含 3 个多边形(圆形)。每个对象中的每个多边形都有一个唯一的、一对一的标识符(“名称”)。我想使这两个对象相交,以便它返回一个相同长度的对象,其中每个几何图形都是具有相同名称的两个多边形的交集。例如,生成的几何图形 A 将是第一个 sf 对象的几何图形 A 与第二个对象的几何图形 A 的交集。这在 QGIS 中很容易完成,但我正在尝试自动化该工作流程。
下面是一些在简单的 xy 空间中创建三个圆圈的代码。如何按照描述将 pts_a 与 pts_b 相交?
我尝试过st_intersects和aggregate
(我刚接触R几天,所以我真的不知道如何正确格式化这些问题 - 感谢您的帮助)
library(sf)
#create some XY coordinates
pts_a <- (matrix(c(1,1,3,2,5,3),nrow=3,ncol=2))
pts_b <- (matrix(2:7,nrow=3,ncol=2))
#convert to a data frame
pts_a <- data.frame(pts_a)
pts_b <- data.frame(pts_b)
#set values to numbers, create a points object and add point names for pts_a
pts_a$X1 <- as.numeric(pts_a$X1)
pts_a$X2 <- as.numeric(pts_a$X2)
pts_a <- st_as_sf(pts_a,coords=c('X1','X2'))
pts_a <- cbind(pts_a,c('A','B','c'))
#set values to numbers, create a points object and add point names for pts_b
pts_b$X1 <- as.numeric(pts_b$X1)
pts_b$X2 <- as.numeric(pts_b$X2)
pts_b <- st_as_sf(pts_b,coords=c('X1','X2'))
pts_b <- cbind(pts_b,c('A','B','c'))
#rename columns
colnames(pts_a) <- c('name','geometry')
colnames(pts_b) <- c('name','geometry')
#create polygons (circles of radius 2)
pts_a <- st_buffer(pts_a,2)
pts_b <- st_buffer(pts_b,2)```
This is where I'm stuck after having tried a number of approaches.
I have two sf objects that each contains 3 polygons (circles). Each polygon in each object has a unique, one-to-one identifier ('name'). I want to intersect these two objects so that it returns an object of the same length where each geometry is the intersection of the two polygons with the same name. For instance, the resulting geometry A will be the intersection of geometry A from the first sf object with geometry A of the second object. This is easily done in QGIS, but I'm trying to automate that workflow.
Here is some code that creates three circles in a simple xy space. How can I intersect pts_a with pts_b as described?
I've tried st_intersects
and aggregate
(I'm only a few days into R, so I don't really know how to format these questions properly - thanks for your help)
library(sf)
#create some XY coordinates
pts_a <- (matrix(c(1,1,3,2,5,3),nrow=3,ncol=2))
pts_b <- (matrix(2:7,nrow=3,ncol=2))
#convert to a data frame
pts_a <- data.frame(pts_a)
pts_b <- data.frame(pts_b)
#set values to numbers, create a points object and add point names for pts_a
pts_a$X1 <- as.numeric(pts_a$X1)
pts_a$X2 <- as.numeric(pts_a$X2)
pts_a <- st_as_sf(pts_a,coords=c('X1','X2'))
pts_a <- cbind(pts_a,c('A','B','c'))
#set values to numbers, create a points object and add point names for pts_b
pts_b$X1 <- as.numeric(pts_b$X1)
pts_b$X2 <- as.numeric(pts_b$X2)
pts_b <- st_as_sf(pts_b,coords=c('X1','X2'))
pts_b <- cbind(pts_b,c('A','B','c'))
#rename columns
colnames(pts_a) <- c('name','geometry')
colnames(pts_b) <- c('name','geometry')
#create polygons (circles of radius 2)
pts_a <- st_buffer(pts_a,2)
pts_b <- st_buffer(pts_b,2)```
This is where I'm stuck after having tried a number of approaches.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您想要使用
st_intersection()
,它返回空间交集。st_intersects()
返回一个逻辑值,指示对象是否存在交集,由于名称相似,这是一个非常常见的错误。如果我正确理解了您想要的输出,那么在
st_intersection()
之后,您可能只需要过滤掉具有相同名称的对象的交集(请注意,相交后,最终结果具有对象 <代码>pts_a和pts_b
)。我认为在您的示例中,
pts_a
和pts_b
上的“c”对象不相交。You want to use
st_intersection()
, that returns the spatial intersection.st_intersects()
return a logical indicating wheter there are a intersection or not of the objects, this is a pretty common error due to the the similarity of names.If I understood properly your desired output, after
st_intersection()
you may need just to filter out those intersections of the objects that have the same name (note that after intersecting the final result has the values of objectpts_a
andpts_b
).I think in your example "c" objects on
pts_a
andpts_b
does not intersect.