将一个 sf 对象导出到多个 shapefile
export one sf object to a multiple shapefile
我有一个大的 shapefile,我需要按属性(分组值)分成几个。在 ArcGIS 中,此函数调用 Split By Attribute
让我们从 sf
库中读取 nc
数据框作为示例
library(tidyverse)
library(sf)
nc = st_read(system.file("shape/nc.shp", package="sf"))
nc
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
1 0.114 1.442 1825 1825 Ashe 37009 37009 5
2 0.061 1.231 1827 1827 Alleghany 37005 37005 3
3 0.143 1.630 1828 1828 Surry 37171 37171 86
我想根据 NAME
变量将其拆分为 100 个 shapefile。所以我要像这样 group_by
、nest
和 walk
。
nc %>%
mutate(group = as.character(NAME)) %>%
group_by(group) %>%
nest() %>%
mutate(data = map(data, ~st_as_sf(.x))) %>%
walk2(.x = data, .y = group,
~st_write(obj = .x,
dsn = paste0(.y,
".shp")))
这个漂亮的烟斗returns我有以下错误:
Error: Can't convert a grouped_df/tbl_df/tbl/data.frame
object to function
nc %>%
mutate(group = as.character(NAME)) %>%
group_by(group) %>%
nest() %>%
#move walk2 inside mutate as data and group were invisibale for walk2
#also no need for data you can ran walk2 directly
mutate(#data = map(data, ~st_as_sf(.x)),
txt = walk2(.x = data, .y = group, ~st_write(obj = .x, dsn = paste0(.y, ".shp"))))
我有一个大的 shapefile,我需要按属性(分组值)分成几个。在 ArcGIS 中,此函数调用 Split By Attribute
让我们从 sf
库中读取 nc
数据框作为示例
library(tidyverse)
library(sf)
nc = st_read(system.file("shape/nc.shp", package="sf"))
nc
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
1 0.114 1.442 1825 1825 Ashe 37009 37009 5
2 0.061 1.231 1827 1827 Alleghany 37005 37005 3
3 0.143 1.630 1828 1828 Surry 37171 37171 86
我想根据 NAME
变量将其拆分为 100 个 shapefile。所以我要像这样 group_by
、nest
和 walk
。
nc %>%
mutate(group = as.character(NAME)) %>%
group_by(group) %>%
nest() %>%
mutate(data = map(data, ~st_as_sf(.x))) %>%
walk2(.x = data, .y = group,
~st_write(obj = .x,
dsn = paste0(.y,
".shp")))
这个漂亮的烟斗returns我有以下错误:
Error: Can't convert a
grouped_df/tbl_df/tbl/data.frame
object to function
nc %>%
mutate(group = as.character(NAME)) %>%
group_by(group) %>%
nest() %>%
#move walk2 inside mutate as data and group were invisibale for walk2
#also no need for data you can ran walk2 directly
mutate(#data = map(data, ~st_as_sf(.x)),
txt = walk2(.x = data, .y = group, ~st_write(obj = .x, dsn = paste0(.y, ".shp"))))