Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.4k views
in Technique[技术] by (71.8m points)

r: How can I use data.table package with sf geometry column?

I want to use data.table to join two dataframes that each have a sf geometry column, and I want to do a cross join in data.table. Reproducible data below and my code is as follows:

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]

This gives the following result

enter image description here

Using data.table changes the sf geometry column. How can I use data.table while preserving the original format of the geometry column?

For instance, the orginial dataframe geometry column for data1 looks like this:

enter image description here

Reproducible data:

data1:

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"))

data2:

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"))

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)
等待大神答复

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...