r - 将sf :: st_within的输出转换为vector

时间:2018-03-15 08:50:10

标签: r dataframe dplyr gis sf

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

# 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)

这给出了错误:列indicator必须是1d原子向量或列表。

我尝试在下面解决这个问题:

# 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兼容的字符向量而无需制作矩阵的方法?

3 个答案:

答案 0 :(得分:4)

以下是如何从稀疏几何二元谓词中获取逻辑向量:

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

或子集而不创建新变量:

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

我遗憾地无法对您的大型数据集进行测试,但如果它比矩阵方法更快,请告诉我。

答案 1 :(得分:1)

is_within的结果实际上是一个列表列,因此您可以使用它 通过“不公开”来解决这个问题。像这样的东西会起作用:

library(dplyr)
library(sf)

# object 1: I will test if it is inside object 2 - to make this more interesting
# I added a second not-contained line
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")

df2 <- data.frame(lon = c(4.5, 5, 6), lat = c(4.5, 5, 6), var = 2) %>% 
  st_as_sf(coords = c("lon", "lat"), dim = "XY") %>% st_set_crs(4326) %>%
  summarise(var = sum(var), do_union = F) %>% st_cast("LINESTRING")
df3 <- rbind(df, df2)

# 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")

plot(df3) 
plot(st_geometry(box), add = TRUE)

# see if the lines are within the box and build a data frame with results
is_within <- st_within(df3$geometry, box$geometry) %>% 
  lapply(FUN = function(x) data.frame(ind = length(x))) %>% 
  bind_rows()

# add the "indicator" to df3
df3 <- dplyr::mutate(df3, indicator = is_within$ind) 
df3
#> Simple feature collection with 2 features and 2 fields
#> geometry type:  LINESTRING
#> dimension:      XY
#> bbox:           xmin: 2.5 ymin: 2.5 xmax: 6 ymax: 6
#> epsg (SRID):    4326
#> proj4string:    +proj=longlat +datum=WGS84 +no_defs
#>   var indicator                       geometry
#> 1   3         1 LINESTRING (2.5 2.5, 3 3, 3...
#> 2   6         0 LINESTRING (4.5 4.5, 5 5, 6 6)

HTH

reprex package(v0.2.0)创建于2018-03-15。

答案 2 :(得分:0)

不要直接使用st_within功能,而是尝试使用spatial join。 查看以下示例st_joins如何工作

library(sf)
library(tidyverse)

lines <-
data.frame(id=gl(3,2), x=c(-3,2,6,11,7,10), y=c(-1,6,-5,-9,10,5)) %>%
  st_as_sf(coords=c("x","y"), remove=F) %>% 
  group_by(id) %>% 
  summarise() %>%
  st_cast("LINESTRING")

yta10 <-
    st_point(c(0, 0)) %>%
    st_buffer(dist = 10) %>%
    st_sfc() %>%
    st_sf(yta = "10m")

使用左连接保留所有线条,但您可以看到哪些线条位于多边形内

lines %>% st_join(yta10, left=TRUE)

内部联接(左= FALSE)仅保留内部联接

lines %>% st_join(yta10, left=FALSE)

后者也可以通过

获得
lines[yta10,]