从 MULTIPOLYGON 中提取最小和最大坐标到单独的列中
Extracting min and max coordinates from MULTIPOLYGON into separate columns
我有一个大型数据框,其中有一列名为几何。
> class(df$geometry)
[1] "sfc_MULTIPOLYGON" "sfc"
该列如下所示:
head(df$geometry)
Geometry set for 6 features
geometry type: MULTIPOLYGON
dimension: XY
bbox: xmin: -157.9813 ymin: 56.73044 xmax: -152.267 ymax: 58.93569
epsg (SRID): 4269
proj4string: +proj=longlat +ellps=GRS80 +towgs84=0,0,0,0,0,0,0 +no_defs
First 5 geometries:
MULTIPOLYGON (((-157.973 56.74355, -157.9727 56...
MULTIPOLYGON (((-157.5293 57.61359, -157.5293 5...
MULTIPOLYGON (((-152.268 57.62401, -152.2674 57...
MULTIPOLYGON (((-157.5457 58.86262, -157.5449 5...
MULTIPOLYGON (((-156.3857 58.88877, -156.3849 5...
我知道我可以访问纬度的数字向量,例如,通过深入研究列表的列表,即:
head(df$geometry[[1]][[1]][[1]][,1])
[1] -157.9730 -157.9727 -157.9721 -157.9716 -157.9708 -157.9707
我只是想提取每一行的最小和最大纬度和经度,并将每一列作为一列(因此向数据框添加四列:lat_min、lat_max、lon_min, lon_max).
我尝试了很多选项 - 包括 st_coordinates()、st_bbox()、unlist 的组合,但似乎无法将其映射到所有行。请帮忙。
软件包:dpylr、tidyverse、tidyr、sf
查看我的评论reprex
:
library(sf)
library(dplyr)
myshape <- st_read(system.file("shape/nc.shp", package = "sf"))[1:6,"NAME"]
head(myshape,3)
#> Simple feature collection with 3 features and 1 field
#> geometry type: MULTIPOLYGON
#> dimension: XY
#> bbox: xmin: -81.74107 ymin: 36.23388 xmax: -80.43531 ymax: 36.58965
#> epsg (SRID): 4267
#> proj4string: +proj=longlat +datum=NAD27 +no_defs
#> NAME geometry
#> 1 Ashe MULTIPOLYGON (((-81.47276 3...
#> 2 Alleghany MULTIPOLYGON (((-81.23989 3...
#> 3 Surry MULTIPOLYGON (((-80.45634 3...
#Extract coordinates
bbox_list <- lapply(st_geometry(myshape), st_bbox)
#To df
maxmin <- as.data.frame(matrix(unlist(bbox_list),nrow=nrow(myshape)))
#get names
names(maxmin) <- names(bbox_list[[1]])
#Final shape
myshape2=bind_cols(myshape,maxmin)
head(myshape2,3)
#> Simple feature collection with 3 features and 5 fields
#> geometry type: MULTIPOLYGON
#> dimension: XY
#> bbox: xmin: -81.74107 ymin: 36.23388 xmax: -80.43531 ymax: 36.58965
#> epsg (SRID): 4267
#> proj4string: +proj=longlat +datum=NAD27 +no_defs
#> NAME geometry xmin ymin xmax
#> 1 Ashe MULTIPOLYGON (((-81.47276 3... -81.74107 -80.90344 -76.33025
#> 2 Alleghany MULTIPOLYGON (((-81.23989 3... 36.23436 36.57286 36.07282
#> 3 Surry MULTIPOLYGON (((-80.45634 3... -81.23989 -80.96577 -75.77316
#> ymax
#> 1 -77.07531
#> 2 36.55629
#> 3 -77.21767
由 reprex package (v0.3.0)
于 2020 年 3 月 6 日创建
我有一个大型数据框,其中有一列名为几何。
> class(df$geometry)
[1] "sfc_MULTIPOLYGON" "sfc"
该列如下所示:
head(df$geometry)
Geometry set for 6 features
geometry type: MULTIPOLYGON
dimension: XY
bbox: xmin: -157.9813 ymin: 56.73044 xmax: -152.267 ymax: 58.93569
epsg (SRID): 4269
proj4string: +proj=longlat +ellps=GRS80 +towgs84=0,0,0,0,0,0,0 +no_defs
First 5 geometries:
MULTIPOLYGON (((-157.973 56.74355, -157.9727 56...
MULTIPOLYGON (((-157.5293 57.61359, -157.5293 5...
MULTIPOLYGON (((-152.268 57.62401, -152.2674 57...
MULTIPOLYGON (((-157.5457 58.86262, -157.5449 5...
MULTIPOLYGON (((-156.3857 58.88877, -156.3849 5...
我知道我可以访问纬度的数字向量,例如,通过深入研究列表的列表,即:
head(df$geometry[[1]][[1]][[1]][,1])
[1] -157.9730 -157.9727 -157.9721 -157.9716 -157.9708 -157.9707
我只是想提取每一行的最小和最大纬度和经度,并将每一列作为一列(因此向数据框添加四列:lat_min、lat_max、lon_min, lon_max).
我尝试了很多选项 - 包括 st_coordinates()、st_bbox()、unlist 的组合,但似乎无法将其映射到所有行。请帮忙。
软件包:dpylr、tidyverse、tidyr、sf
查看我的评论reprex
:
library(sf)
library(dplyr)
myshape <- st_read(system.file("shape/nc.shp", package = "sf"))[1:6,"NAME"]
head(myshape,3)
#> Simple feature collection with 3 features and 1 field
#> geometry type: MULTIPOLYGON
#> dimension: XY
#> bbox: xmin: -81.74107 ymin: 36.23388 xmax: -80.43531 ymax: 36.58965
#> epsg (SRID): 4267
#> proj4string: +proj=longlat +datum=NAD27 +no_defs
#> NAME geometry
#> 1 Ashe MULTIPOLYGON (((-81.47276 3...
#> 2 Alleghany MULTIPOLYGON (((-81.23989 3...
#> 3 Surry MULTIPOLYGON (((-80.45634 3...
#Extract coordinates
bbox_list <- lapply(st_geometry(myshape), st_bbox)
#To df
maxmin <- as.data.frame(matrix(unlist(bbox_list),nrow=nrow(myshape)))
#get names
names(maxmin) <- names(bbox_list[[1]])
#Final shape
myshape2=bind_cols(myshape,maxmin)
head(myshape2,3)
#> Simple feature collection with 3 features and 5 fields
#> geometry type: MULTIPOLYGON
#> dimension: XY
#> bbox: xmin: -81.74107 ymin: 36.23388 xmax: -80.43531 ymax: 36.58965
#> epsg (SRID): 4267
#> proj4string: +proj=longlat +datum=NAD27 +no_defs
#> NAME geometry xmin ymin xmax
#> 1 Ashe MULTIPOLYGON (((-81.47276 3... -81.74107 -80.90344 -76.33025
#> 2 Alleghany MULTIPOLYGON (((-81.23989 3... 36.23436 36.57286 36.07282
#> 3 Surry MULTIPOLYGON (((-80.45634 3... -81.23989 -80.96577 -75.77316
#> ymax
#> 1 -77.07531
#> 2 36.55629
#> 3 -77.21767
由 reprex package (v0.3.0)
于 2020 年 3 月 6 日创建