首页 > 解决方案 > r:如何使用带有 sf 几何列的 data.table 包?

问题描述

我想使用 data.table 连接两个数据框,每个数据框都有一个 sf 几何列,我想在 data.table 中进行交叉连接。下面的可重现数据和我的代码如下:

library(data.table)

#convert data from data frames to data table
#add the dummy key 'k' and remove it at the end

setkey(as.data.table(data1)[,c(k=1,.SD)],k)[as.data.table(data2). 
[,c(k=1,.SD)],allow.cartesian=TRUE][,k:=NULL]

这给出了以下结果

在此处输入图像描述

使用 data.table 更改 sf 几何列。如何在保留几何列的原始格式的同时使用 data.table?

例如,data1 的原始数据框几何列如下所示:

在此处输入图像描述

可重现的数据:

数据1:

structure(list(shape = c("polygon 1", "polygon 2"), geometry = structure(list(
structure(list(structure(c(-4e-04, -4e-04, -3e-05, -3e-05, 
-4e-04, 51.199, 51.1975, 51.1975, 51.199, 51.199), .Dim = c(5L, 
2L))), class = c("XY", "POLYGON", "sfg"), precision = 0, bbox = structure(c(xmin = -4e-04, 
ymin = 51.1975, xmax = -3e-05, ymax = 51.199), class = "bbox"), crs = structure(list(
    input = NA_character_, wkt = NA_character_), class = "crs"), n_empty = 0L), 
structure(list(structure(c(5e-05, 5e-05, 0.003, 0.003, 5e-05, 
51.1972, 51.1967, 51.1967, 51.1972, 51.1972), .Dim = c(5L, 
2L))), class = c("XY", "POLYGON", "sfg"), precision = 0, bbox = structure(c(xmin = 5e-05, 
ymin = 51.1967, xmax = 0.003, ymax = 51.1972), class = "bbox"), crs = structure(list(
    input = NA_character_, wkt = NA_character_), class = "crs"), n_empty = 0L)), class = 
c("sfc_POLYGON", 
"sfc"), precision = 0, bbox = structure(c(xmin = -4e-04, ymin = 51.1967, 
xmax = 0.003, ymax = 51.199), class = "bbox"), crs = structure(list(
input = NA_character_, wkt = NA_character_), class = "crs"), n_empty = 0L)), row.names = c(NA, 
-2L), sf_column = "geometry", agr = structure(c(shape = NA_integer_), .Label = c("constant", 
"aggregate", "identity"), class = "factor"), class = c("sf", 
"tbl_df", "tbl", "data.frame"))

数据2:

structure(list(shape = c("polygon 1", "polygon 2"), geometry = structure(list(
structure(list(structure(c(0.0095, 0.0085, 0.0075, 0.0075, 
0.01055, 0.01055, 0.012, 0.0115, 0.0095, 51.21, 51.199, 51.199, 
51.197, 51.196, 51.198, 51.198, 51.21, 51.21), .Dim = c(9L, 
2L))), class = c("XY", "POLYGON", "sfg"), precision = 0, bbox = structure(c(xmin = 0.0075, 
ymin = 51.196, xmax = 0.012, ymax = 51.21), class = "bbox"), crs = structure(list(
    input = NA_character_, wkt = NA_character_), class = "crs"), n_empty = 0L), 
structure(list(structure(c(0.0205, 0.019, 0.019, 0.02, 0.021, 
0.0205, 51.196, 51.1955, 51.194, 51.193, 51.194, 51.196), .Dim = c(6L, 
2L))), class = c("XY", "POLYGON", "sfg"), precision = 0, bbox = structure(c(xmin = 0.019, 
ymin = 51.193, xmax = 0.021, ymax = 51.196), class = "bbox"), crs = structure(list(
    input = NA_character_, wkt = NA_character_), class = "crs"), n_empty = 0L)), class = 
c("sfc_POLYGON", 
"sfc"), precision = 0, bbox = structure(c(xmin = 0.0075, ymin = 51.193, 
xmax = 0.021, ymax = 51.21), class = "bbox"), crs = structure(list(
input = NA_character_, wkt = NA_character_), class = "crs"), n_empty = 0L)), row.names = c(NA, 
-2L), sf_column = "geometry", agr = structure(c(shape = NA_integer_), .Label = c("constant", 
"aggregate", "identity"), class = "factor"), class = c("sf", 
"tbl_df", "tbl", "data.frame"))

标签: rdata.tablesf

解决方案


推荐阅读