【问题标题】:How to filter an R simple features collection using sf methods like st_intersects()?如何使用 st_intersects() 等 sf 方法过滤 R 简单特征集合?
【发布时间】:2019-07-12 21:49:59
【问题描述】:

SF 是 R-Spatial 包,旨在使用 dyplr 和管道等整洁的语法。

我想对一个简单的特征集合对象做一个简单的空间过滤器。给定一个简单的特征集合,我想从集合中返回满足某些几何条件的所有特征。特别是,我想找到与另一个对象相交的特征。

SF 提供了函数st_intersects(x,y,...) 来执行此操作,但我无法让它与 dplyr 一起使用。

我正在使用 R 3.5.2 和从 github 安装的最新 sf。

library(tidyverse)
library(sf)
#> Linking to GEOS 3.6.1, GDAL 2.2.3, PROJ 4.9.3

# many multipolygons:
nc <- st_read(system.file("shape/nc.shp", package="sf"))

#> Simple feature collection with 100 features and 14 fields
#> geometry type:  MULTIPOLYGON
#> dimension:      XY
#> bbox:           xmin: -84.32385 ymin: 33.88199 xmax: -75.45698 ymax: 36.58965
#> epsg (SRID):    4267
#> proj4string:    +proj=longlat +datum=NAD27 +no_defs

# A point in Ashe County:
ash_point <- nc %>% 
  filter(NAME == "Ashe") %>% 
  st_point_on_surface()

# how many counties intersect ash_point? 
nc %>% 
  st_intersects(ash_point, sparse = FALSE) %>% 
  sum()
#> [1] 1

# return the features which intersect ash_point:
nc %>% 
  filter(st_intersects(ash_point, sparse = FALSE)) 
#> although coordinates are longitude/latitude, st_intersects assumes that they are planar
#> Simple feature collection with 100 features and 14 fields
#> geometry type:  MULTIPOLYGON
#> dimension:      XY
#> bbox:           xmin: -84.32385 ymin: 33.88199 xmax: -75.45698 ymax: 36.58965
#> epsg (SRID):    4267
#> proj4string:    +proj=longlat +datum=NAD27 +no_defs
#> First 10 features:
#>     AREA PERIMETER CNTY_ CNTY_ID        NAME  FIPS FIPSNO CRESS_ID BIR74
#> 1  0.114     1.442  1825    1825        Ashe 37009  37009        5  1091
#> 2  0.061     1.231  1827    1827   Alleghany 37005  37005        3   487
#> 3  0.143     1.630  1828    1828       Surry 37171  37171       86  3188
#> 4  0.070     2.968  1831    1831   Currituck 37053  37053       27   508
#> 5  0.153     2.206  1832    1832 Northampton 37131  37131       66  1421
#> 6  0.097     1.670  1833    1833    Hertford 37091  37091       46  1452
#> 7  0.062     1.547  1834    1834      Camden 37029  37029       15   286
#> 8  0.091     1.284  1835    1835       Gates 37073  37073       37   420
#> 9  0.118     1.421  1836    1836      Warren 37185  37185       93   968
#> 10 0.124     1.428  1837    1837      Stokes 37169  37169       85  1612
#>    SID74 NWBIR74 BIR79 SID79 NWBIR79                       geometry
#> 1      1      10  1364     0      19 MULTIPOLYGON (((-81.47276 3...
#> 2      0      10   542     3      12 MULTIPOLYGON (((-81.23989 3...
#> 3      5     208  3616     6     260 MULTIPOLYGON (((-80.45634 3...
#> 4      1     123   830     2     145 MULTIPOLYGON (((-76.00897 3...
#> 5      9    1066  1606     3    1197 MULTIPOLYGON (((-77.21767 3...
#> 6      7     954  1838     5    1237 MULTIPOLYGON (((-76.74506 3...
#> 7      0     115   350     2     139 MULTIPOLYGON (((-76.00897 3...
#> 8      0     254   594     2     371 MULTIPOLYGON (((-76.56251 3...
#> 9      4     748  1190     2     844 MULTIPOLYGON (((-78.30876 3...
#> 10     1     160  2038     5     176 MULTIPOLYGON (((-80.02567 3...

reprex package (v0.3.0.9000) 于 2019 年 7 月 12 日创建

st_intersects() 单独返回正确的逻辑矩阵,但在过滤器中使用时,所有结果都会返回,即使是逻辑矩阵具有“FALSE”的特征。

【问题讨论】:

    标签: r dplyr spatial sf


    【解决方案1】:

    注意st_intersection(, sparse = TRUE) 返回一个逻辑matrix,而filter 需要一个向量。我们可以通过对矩阵进行子集化得到选择向量:

    nc %>%
      filter(st_intersects(., ash_point, sparse = FALSE)[1,])
    

    . 需要 nc 也是 st_intersects 的参数,而不仅仅是 filter

    如果filter.sf 方法可以直接对st_intersects 的输出敏感,而不需要sparse=FALSE[1,],那就太好了。我会把它放在一些 TODO 列表中。

    【讨论】:

    • 我明白了,所以我想为 st_intersects 提供来自 nc 的 geometry 列而不是 . 也会将输出更改为向量,就像 [1,] 方法一样。我同意 dplyr 动词有一个预期的工作流程,而让 sf 几何验证函数在该流程中工作的额外样板并不理想。
    • 使用[1,] 时得到的结果不正确,但使用[,1] 时结果正确。这可能是[,1],所以我们使用列而不是行?
    • 这个答案对我也不起作用我得到Error in FALSE[1, ] : incorrect number of dimensions
    【解决方案2】:

    显然,要使 dplyr 动词与 sf 函数一起使用,您需要指定列名“geometry”。

    修正版:

    nc %>% 
      filter(st_intersects(geometry, ash_point, sparse = FALSE))
    

    【讨论】:

    • 另外,请注意st_point_on_surface() 将您的多多边形强制转换为一个点,以便它只与边界多边形相交。如果您将其保留为多边形st_polygonize(),它将与其他 4 个多边形相交。
    • 是的。我认为使用只有一个相交多边形的单个点是一个更简单的示例。
    【解决方案3】:

    提醒一下:我在这个用例中使用建议的方法没有成功。但是,直接操作 st_intersects 的输出以创建指标变量对我有用:r - Convert output from sf::st_within to vector

    【讨论】:

    • 欢迎@Sebastian Krays,这似乎没有帮助。您可以在解决方案中放置一些代码 sn-ps。这是我获得更多声誉的建议。
    猜你喜欢
    • 2022-01-06
    • 1970-01-01
    • 2015-11-27
    • 2018-07-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-07-25
    • 2014-06-10
    相关资源
    最近更新 更多