sf:将 Lat/Long 从几何体写入单独的列并保留 ID 列
sf: Write Lat/Long from geometry into separate column and keep ID column
我有一个 df,其中包含来自 shapefile 的多边形 ID 及其在几何列中的中心点:
# A tibble: 3 x 2
ID geometry
<dbl> <POINT [°]>
1 1 (117.2 31.8)
2 2 (116.4 40.1)
3 4 (117.9 26)
我想将 latitude/longitude 值放入单独的列中,所以我这样做:
library(sf)
centres<- as.data.frame(st_coordinates(df))
这个新的 'centres' 数据框具有经纬度值,但缺少 ID 列。
我怎样才能保留它,或者是否有另一种方法可以将经纬度值从几何列中获取到单独的列中,同时将 ID 保持在相同的 df 中?
数据帧的输入是:
df <- structure(list(ID = c(1, 2, 4),
geometry = structure(list(structure(c(117.2, 31.8),
class = c("XY", "POINT", "sfg")), structure(c(116.4, 40.1),
class = c("XY", "POINT", "sfg")), structure(c(117.9, 26.0),
class = c("XY", "POINT", "sfg"))), class = c("sfc_POINT", "sfc"),
precision = 0, bbox = structure(c(xmin = 116.4, ymin = 26.0, xmax = 117.9, ymax = 40.1),
class = "bbox"), crs = structure(list(epsg = 4326L,
proj4string = "+proj=longlat +datum=WGS84 +no_defs"), class = "crs"), n_empty = 0L)),
row.names = c(NA, -3L), class = c("sf", "tbl_df", "tbl", "data.frame"),
sf_column = "geometry", agr = structure(c(ID = NA_integer_),
class = "factor", .Label = c("constant", "aggregate", "identity")))
使用unlist + map()的解决方案
library(tidyverse)
separated_coord <- df %>%
mutate(long = unlist(map(df$geometry,1)),
lat = unlist(map(df$geometry,2)))
separated_coord
一种可能的方法是将其取消列出。
setNames(data.frame(df[[1]],
matrix(unlist(df[2]), ncol=2, byrow=TRUE)),
c("ID", "lon", "lat"))
# ID lon lat
# 1 1 117.2 31.8
# 2 2 116.4 40.1
# 3 4 117.9 26.0
说明
数据结构检查 str(df)
显示,变量 - geometry
- 是 list
格式,这可能不方便。解决这个问题的一种方法是 unlist()
它,将其转换为 2 列矩阵,然后将其与第一列重新组合。使用 setNames()
我们可以一步分配新的列名。
st_geometry
可与 dplyr::mutate
一起使用:
library(magrittr) #for the pipe
df <- df %>%
dplyr::mutate(lon = sf::st_coordinates(.)[,1],
lat = sf::st_coordinates(.)[,2])
df
Simple feature collection with 3 features and 3 fields
geometry type: POINT
dimension: XY
bbox: xmin: 116.4 ymin: 26 xmax: 117.9 ymax: 40.1
CRS: EPSG:4326
# A tibble: 3 x 4
ID geometry lon lat
* <dbl> <POINT [°]> <dbl> <dbl>
1 1 (117.2 31.8) 117. 31.8
2 2 (116.4 40.1) 116. 40.1
3 4 (117.9 26) 118. 26
如果您不再关心 geometry
,请使用 df %>% sf::st_set_geometry(NULL)
在标准数据框中进行转换
我有一个 df,其中包含来自 shapefile 的多边形 ID 及其在几何列中的中心点:
# A tibble: 3 x 2
ID geometry
<dbl> <POINT [°]>
1 1 (117.2 31.8)
2 2 (116.4 40.1)
3 4 (117.9 26)
我想将 latitude/longitude 值放入单独的列中,所以我这样做:
library(sf)
centres<- as.data.frame(st_coordinates(df))
这个新的 'centres' 数据框具有经纬度值,但缺少 ID 列。 我怎样才能保留它,或者是否有另一种方法可以将经纬度值从几何列中获取到单独的列中,同时将 ID 保持在相同的 df 中?
数据帧的输入是:
df <- structure(list(ID = c(1, 2, 4),
geometry = structure(list(structure(c(117.2, 31.8),
class = c("XY", "POINT", "sfg")), structure(c(116.4, 40.1),
class = c("XY", "POINT", "sfg")), structure(c(117.9, 26.0),
class = c("XY", "POINT", "sfg"))), class = c("sfc_POINT", "sfc"),
precision = 0, bbox = structure(c(xmin = 116.4, ymin = 26.0, xmax = 117.9, ymax = 40.1),
class = "bbox"), crs = structure(list(epsg = 4326L,
proj4string = "+proj=longlat +datum=WGS84 +no_defs"), class = "crs"), n_empty = 0L)),
row.names = c(NA, -3L), class = c("sf", "tbl_df", "tbl", "data.frame"),
sf_column = "geometry", agr = structure(c(ID = NA_integer_),
class = "factor", .Label = c("constant", "aggregate", "identity")))
使用unlist + map()的解决方案
library(tidyverse)
separated_coord <- df %>%
mutate(long = unlist(map(df$geometry,1)),
lat = unlist(map(df$geometry,2)))
separated_coord
一种可能的方法是将其取消列出。
setNames(data.frame(df[[1]],
matrix(unlist(df[2]), ncol=2, byrow=TRUE)),
c("ID", "lon", "lat"))
# ID lon lat
# 1 1 117.2 31.8
# 2 2 116.4 40.1
# 3 4 117.9 26.0
说明
数据结构检查 str(df)
显示,变量 - geometry
- 是 list
格式,这可能不方便。解决这个问题的一种方法是 unlist()
它,将其转换为 2 列矩阵,然后将其与第一列重新组合。使用 setNames()
我们可以一步分配新的列名。
st_geometry
可与 dplyr::mutate
一起使用:
library(magrittr) #for the pipe
df <- df %>%
dplyr::mutate(lon = sf::st_coordinates(.)[,1],
lat = sf::st_coordinates(.)[,2])
df
Simple feature collection with 3 features and 3 fields
geometry type: POINT
dimension: XY
bbox: xmin: 116.4 ymin: 26 xmax: 117.9 ymax: 40.1
CRS: EPSG:4326
# A tibble: 3 x 4
ID geometry lon lat
* <dbl> <POINT [°]> <dbl> <dbl>
1 1 (117.2 31.8) 117. 31.8
2 2 (116.4 40.1) 116. 40.1
3 4 (117.9 26) 118. 26
如果您不再关心 geometry
,请使用 df %>% sf::st_set_geometry(NULL)