r - 在 sf 中测试/过滤几何图形的相等性?

标签 r dplyr geospatial purrr sf

可能在这里遗漏了一些东西,但经过大量挖掘后没有找到任何东西。我正在尝试查找几何具有特定值的 sf 对象行。这是为了整理目的,其中相同的几何图形可能已与不同的关联元数据(例如 ID 和其他值)一起存储在不同的数据集中,我需要解决差异。

通常,使用 dplyr 可以很容易地过滤某些值,如下所示:

加载所需的包

library(tidyverse)
#> Loading tidyverse: ggplot2
#> Loading tidyverse: tibble
#> Loading tidyverse: tidyr
#> Loading tidyverse: readr
#> Loading tidyverse: purrr
#> Loading tidyverse: dplyr
#> Conflicts with tidy packages ----------------------------------------------
#> filter(): dplyr, stats
#> lag():    dplyr, stats
library(sf)
#> Linking to GEOS 3.6.1, GDAL 2.2.0, proj.4 4.9.3

加载示例数据集

nc <- st_read(system.file("shape/nc.shp", package="sf"))
#> Reading layer `nc' from data source `C:\Users\Calum You\Documents\R\win-library\3.4\sf\shape\nc.shp' using driver `ESRI Shapefile'
#> 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

正如预期的那样,dplyr::filter 在不同的列上运行良好。我们可以轻松地选择带有 NAME Ashe 和 CNTY_ID 1825 的行,这是第一行:

nc %>% filter(NAME == "Ashe")
#> Simple feature collection with 1 feature and 14 fields
#> geometry type:  MULTIPOLYGON
#> dimension:      XY
#> bbox:           xmin: -81.74107 ymin: 36.23436 xmax: -81.23989 ymax: 36.58965
#> epsg (SRID):    4267
#> proj4string:    +proj=longlat +datum=NAD27 +no_defs
#>    AREA PERIMETER CNTY_ CNTY_ID NAME  FIPS FIPSNO CRESS_ID BIR74 SID74
#> 1 0.114     1.442  1825    1825 Ashe 37009  37009        5  1091     1
#>   NWBIR74 BIR79 SID79 NWBIR79                       geometry
#> 1      10  1364     0      19 MULTIPOLYGON (((-81.4727554...
nc %>% filter(CNTY_ID == 1825)
#> Simple feature collection with 1 feature and 14 fields
#> geometry type:  MULTIPOLYGON
#> dimension:      XY
#> bbox:           xmin: -81.74107 ymin: 36.23436 xmax: -81.23989 ymax: 36.58965
#> epsg (SRID):    4267
#> proj4string:    +proj=longlat +datum=NAD27 +no_defs
#>    AREA PERIMETER CNTY_ CNTY_ID NAME  FIPS FIPSNO CRESS_ID BIR74 SID74
#> 1 0.114     1.442  1825    1825 Ashe 37009  37009        5  1091     1
#>   NWBIR74 BIR79 SID79 NWBIR79                       geometry
#> 1      10  1364     0      19 MULTIPOLYGON (((-81.4727554...

尝试过滤几何列时效果不佳。我希望这只返回 nc 的第一行,因为那是 nc 的行,其中 geometry 等于(根据定义)第一个几何“nc$geometry[1]”。

nc %>% filter(geometry == nc$geometry[1])
#> Error in filter_impl(.data, quo): Evaluation error: operation == not supported.
nc %>% filter(geometry == st_geometry(nc)[1])
#> Error in filter_impl(.data, quo): Evaluation error: operation == not supported.

错误消息表明即使 == 运算符也不起作用:

nc$geometry[1] == nc$geometry[1]
#> Error in Ops.sfc(nc$geometry[1], nc$geometry[1]): operation == not supported

identical() 有效,但不是为了构建我自己的过滤器。想要看到输出的第一个元素为 TRUE,但我认为 identical() 不像 map 函数中那样工作。

identical(nc$geom[1], nc$geom[1])
#> [1] TRUE
map_lgl(nc$geometry, function(x) identical(x, nc$geometry[1]))
#>   [1] FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE
#>  [12] FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE
#>  [23] FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE
#>  [34] FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE
#>  [45] FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE
#>  [56] FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE
#>  [67] FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE
#>  [78] FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE
#>  [89] FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE
#> [100] FALSE

== 是否根本没有为 sf 中的几何定义?如果是这样,是否有替代方法来测试相等性或以其他方式查找不同数据集中相同的几何形状?我即将求助于 rbind 不同的数据集并使用 duplicated() 来执行此操作,但这需要确保两者中的所有列都相同并且似乎出乎意料/不必要的麻烦。

感谢您完成问题 - 非常感谢任何建议!

最佳答案

看起来 == 确实没有为 sf 对象定义。然而,identical() 可用于此目的,如果您记得在子集中向下钻取,如 cuttlefish44 所述.例如,这工作正常:

identical(nc$geom[[1]], nc$geom[[1]])
map_lgl(nc$geometry, function(x) identical(x, nc$geometry[[1]]))
  [1]  TRUE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE
 [22] FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE
 [43] FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE
 [64] FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE
 [85] FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE

我想这是因为我对 sf 如何存储几何图形感到困惑:sfg 实际上不是一个列表,而是包含几何图形的点列表.无论如何,很高兴找到解决方法。

编辑:我没有意识到 st_equals 是在 sf 中实现的。它具有不同的语法,但可能是实现此目的的最佳工具,因为它描述的是区域相等性而不是数据结构相等性。

关于r - 在 sf 中测试/过滤几何图形的相等性?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47149388/

相关文章:

R 将 data.frame 转换为按列列出

r - 使用 dplyr : How to sort by category in one column based on sum of category in another column? 在 R 中排序

azure - 在 Azure 搜索索引中搜索引用点 x 公里范围内的文档,其中 x 是文档上的字段

javascript - 无法让我的地理空间查询工作。在 Node 中查询 Mongo

r - 在 R 中将 cbind() 与矩阵一起使用时如何保留列标题

R doParallel foreach工作超时错误并且永远不会返回

直接从另一个程序读取 csv

r - 给定条件的总和值

r - 无法使用 R 从 API 中提取数据

sql - POLYGON bigQuery 无效