在 R sf 中的分组列上创建新几何
Create new geometry on grouped column in R sf
我想创建一个新的 shapefile 或一个新的几何变量,以允许我在 R 中绘制区域周围的边界。我正在使用 sf
并使用 tmap
进行映射。基本上,我将一个字符向量添加到 sf
对象,并希望将字符向量设为 new/preferred 映射边界。
这是我的方法的一个示例,它不符合我的要求。我不能说它有什么作用。
library(tidyverse)
library(sf)
library(tmap)
## use North Carolina example
nc = st_read(system.file("shape/nc.shp", package="sf"))
nc_new.region <- nc %>% ## add new region variable
mutate(new.region = sample(c('A', 'B', 'C'), nrow(.),replace = T))
nc_union <- nc_new.region %>%
group_by(new.region) %>% # group by the new character vector
mutate(new_geometry = st_union(geometry)) # union on the geometry variable
# map with tmap package
tm_shape(nc_union)+
tm_borders()
发生这种情况是因为 mutate(new_geometry = st_union(geometry))
在原始 sf 对象中创建了一个 "new" 列,但绘图仍然使用 "original" 几何列。实际上,如果您查看 nc_union
对象,您会发现它仍然包含 100 个特征(因此,没有 "dissolving" 真正完成)。
要按照您的意愿行事,您应该使用 summarize
在组上创建一个 "new" sf 对象:
library(tidyverse)
library(sf)
library(tmap)
## use North Carolina example
nc = st_read(system.file("shape/nc.shp", package="sf"))
#> Reading layer `nc' from data source `D:\Documents\R\win-library.5\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
nc_new.region <- nc %>% ## add new region variable
mutate(new.region = sample(c('A', 'B', 'C'), nrow(.),replace = T))
nc_union <- nc_new.region %>%
group_by(new.region) %>%
summarize()
> nc_union
Simple feature collection with 3 features and 1 field
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 tibble: 3 x 2
new.region geometry
<chr> <MULTIPOLYGON [°]>
1 A (((-78.65572 33.94867, -79.0745 34.30457, -79.04095 34.3193, -79.02947 34.34737, -7~
2 B (((-79.45597 34.63409, -79.6675 34.80066, -79.68596 34.80526, -79.66015 34.8179, -7~
3 C (((-78.65572 33.94867, -78.63472 33.97798, -78.63027 34.0102, -78.58778 34.03061, -~
tm_shape(nc_union)+
tm_borders()
您可以看到现在 nc_union
仅包含 3 个多边形,并且绘图反映了 "aggregation"。
另请参阅:https://github.com/r-spatial/sf/issues/290
由 reprex package (v0.3.0)
于 2019-08-23 创建
我想创建一个新的 shapefile 或一个新的几何变量,以允许我在 R 中绘制区域周围的边界。我正在使用 sf
并使用 tmap
进行映射。基本上,我将一个字符向量添加到 sf
对象,并希望将字符向量设为 new/preferred 映射边界。
这是我的方法的一个示例,它不符合我的要求。我不能说它有什么作用。
library(tidyverse)
library(sf)
library(tmap)
## use North Carolina example
nc = st_read(system.file("shape/nc.shp", package="sf"))
nc_new.region <- nc %>% ## add new region variable
mutate(new.region = sample(c('A', 'B', 'C'), nrow(.),replace = T))
nc_union <- nc_new.region %>%
group_by(new.region) %>% # group by the new character vector
mutate(new_geometry = st_union(geometry)) # union on the geometry variable
# map with tmap package
tm_shape(nc_union)+
tm_borders()
发生这种情况是因为 mutate(new_geometry = st_union(geometry))
在原始 sf 对象中创建了一个 "new" 列,但绘图仍然使用 "original" 几何列。实际上,如果您查看 nc_union
对象,您会发现它仍然包含 100 个特征(因此,没有 "dissolving" 真正完成)。
要按照您的意愿行事,您应该使用 summarize
在组上创建一个 "new" sf 对象:
library(tidyverse)
library(sf)
library(tmap)
## use North Carolina example
nc = st_read(system.file("shape/nc.shp", package="sf"))
#> Reading layer `nc' from data source `D:\Documents\R\win-library.5\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
nc_new.region <- nc %>% ## add new region variable
mutate(new.region = sample(c('A', 'B', 'C'), nrow(.),replace = T))
nc_union <- nc_new.region %>%
group_by(new.region) %>%
summarize()
> nc_union
Simple feature collection with 3 features and 1 field
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 tibble: 3 x 2
new.region geometry
<chr> <MULTIPOLYGON [°]>
1 A (((-78.65572 33.94867, -79.0745 34.30457, -79.04095 34.3193, -79.02947 34.34737, -7~
2 B (((-79.45597 34.63409, -79.6675 34.80066, -79.68596 34.80526, -79.66015 34.8179, -7~
3 C (((-78.65572 33.94867, -78.63472 33.97798, -78.63027 34.0102, -78.58778 34.03061, -~
tm_shape(nc_union)+
tm_borders()
您可以看到现在 nc_union
仅包含 3 个多边形,并且绘图反映了 "aggregation"。
另请参阅:https://github.com/r-spatial/sf/issues/290
由 reprex package (v0.3.0)
于 2019-08-23 创建