所以问题是我有一列“位置”,这是一个列表列表。嵌套列表的长度为2,并且所有索引的点1
的纬度值和索引2
的经度值。当我想sfc
从此列创建-object时,我想将每个嵌套列表转换为-objectsfg
以便随后将其转换为sfc
-object。
前三个嵌套列表如下所示:
> geometry[1:3]
[[1]]
[1] 49.45082 11.07702
[[2]]
[1] 49.45006 11.07262
[[3]]
[1] 49.45704 11.08664
所以我只是有点卡住,不知道如何sfc
从此嵌套列表创建-object。任何提示都太棒了!
如果只有一个带有坐标的向量列表,则可以使用purrr::map
或lapply
对其进行迭代sf
,然后转换为点对象列表,然后将其转换为sfc
。
library(dplyr)
library(sf)
coords <- list(
c(49.45082, 11.07702),
c(49.45006, 11.07262),
c(49.45704, 11.08664)
)
purrr::map(coords, st_point) %>%
st_as_sfc()
#> Geometry set for 3 features
#> geometry type: POINT
#> dimension: XY
#> bbox: xmin: 49.45006 ymin: 11.07262 xmax: 49.45704 ymax: 11.08664
#> epsg (SRID): NA
#> proj4string: NA
#> POINT (49.45082 11.07702)
#> POINT (49.45006 11.07262)
#> POINT (49.45704 11.08664)
如果您将几何的更常见形式用作数据框列,则可以对该列执行相同的操作:
dat <- tibble(geometry = coords)
dat %>%
mutate(geometry = purrr::map(geometry, st_point) %>%
st_as_sfc())
#> # A tibble: 3 x 1
#> geometry
#> <POINT>
#> 1 (49.45082 11.07702)
#> 2 (49.45006 11.07262)
#> 3 (49.45704 11.08664)
或将整个事物转换为sf
数据框:
dat %>%
mutate(geometry = purrr::map(geometry, st_point)) %>%
st_as_sf()
#> Simple feature collection with 3 features and 0 fields
#> geometry type: POINT
#> dimension: XY
#> bbox: xmin: 49.45006 ymin: 11.07262 xmax: 49.45704 ymax: 11.08664
#> epsg (SRID): NA
#> proj4string: NA
#> # A tibble: 3 x 1
#> geometry
#> <POINT>
#> 1 (49.45082 11.07702)
#> 2 (49.45006 11.07262)
#> 3 (49.45704 11.08664)
这些之间的选择仅取决于您的使用。
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句