本文介绍了r-将sf :: st_within的输出转换为矢量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用R中的sf包,使用 st_within 函数查看sf对象是否在另一个sf对象中。我的问题是此函数的输出是稀疏几何二进制谓词- sgbp ,我需要一个向量作为输出,以便可以使用 dplyr 包进行过滤。这是一个简化的示例:

Im trying to use the sf package in R to see if sf object is within another sf object with the st_within function. My issue is with the output of this function which is sparse geometry binary predicate - sgbp and I need a vector as an output so that I can use the dplyr package afterwards for filtering. Here is a simplified example:

# object 1: I will test if it is inside object 2
df <- data.frame(lon = c(2.5, 3, 3.5), lat = c(2.5, 3, 3.5), var = 1) %>% 
st_as_sf(coords = c("lon", "lat"), dim = "XY") %>% st_set_crs(4326) %>%
  summarise(var = sum(var), do_union = F) %>% st_cast("LINESTRING")

# object 2: I will test if it contains object 1
box <- data.frame(lon = c(2, 4, 4, 2, 2), lat = c(2, 2, 4, 4,2), var = 1) %>%
  st_as_sf(coords = c("lon", "lat"), dim = "XY") %>% st_set_crs(4326) %>% 
  summarise(var = sum(var), do_union = F) %>% st_cast("POLYGON")

# test 1
df$indicator <- st_within(df$geometry, box$geometry) # gives geometric binary predicate on pairs of sf sets which cannot be used 
df <- df %>% filter(indicator == 1)

这会导致错误:列指标必须为1d原子向量或列表。

This gives Error: Column indicator must be a 1d atomic vector or a list.

我尝试在下面解决此问题:

I tried solving this problem below:

# test 2
df$indicator <- st_within(df$geometry, box$geometry, sparse = F) %>% 
  diag() # gives matrix that I convert with diag() into vector
df <- df %>% filter(indicator == FALSE)

此方法有效,它将删除包含TRUE值的行,但将其变为矩阵因为我的真实数据包含许多观察值,所以计算速度非常慢。有没有办法使 st_within 的输出成为字符向量,或者有办法将 sgbp 转换为字符向量与 dplyr 兼容而不创建矩阵?

This works, it removes the row that contains TRUE values but the process of making a matrix is very slow for my calculations since my real data contains many observations. Is there a way to make the output of st_within a character vector, or maybe a way to convert sgbp to a character vector compatible with dplyr without making a matrix?

推荐答案

您可以从稀疏几何二进制谓词中获得逻辑向量:

Here is how you can get a logical vector from sparse geometry binary predicate:

df$indicator <- st_within(df, box) %>% lengths > 0

或子集而不创建新变量:

or to subset without creating a new variable:

df <- df[st_within(df, box) %>% lengths > 0,]

不幸的是,我无法对您的大型数据集进行测试,但是请问它是否比矩阵方法。

I cannot test on your large dataset unfortunately but please let me know if it is faster than matrix approach.

这篇关于r-将sf :: st_within的输出转换为矢量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-12 10:48