可能在这里遗漏了一些东西,但经过一堆挖掘后没有找到任何东西。我试图找到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()
在地图函数中不会这样工作。
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()
来执行此操作,但这需要确保两者中的所有列都相同,并且看起来像是意外/不必要的麻烦。
感谢您完成问题 - 任何建议都非常感谢!