我正在根据慢跑坐标生成路径。像我一样的样本可以在下面复制:
#Reproduce Data
library(data.table)
library(sf)
dt <- data.table(lon=sample(c(380:390), 1000000, replace = TRUE)/10,lat=sample(c(80:90), 1000000, replace = TRUE)/10, pgrp=rep(c(1:100),10000))
tpathline <- st_as_sf(dt, coords = c("lon","lat"),crs=4326)
下面以线段为单位编译点的时间永远长。我有很多事情要做,有没有比下面的dplyr管道更快/更好的方式来编译行?
#Works but slowest thing ever
library(dplyr)
baseline <- tpathline %>%
dplyr::group_by(pgrp) %>%
dplyr::summarise(do_union=F) %>%
sf::st_cast("MULTILINESTRING")
提前谢谢!
答案 0 :(得分:2)
这是使用sf
和data.table
时的通用方法,而且速度也很快。
library(data.table)
library(sf)
dt <- data.table(lon=sample(c(380:390), 1000000, replace = TRUE)/10,lat=sample(c(80:90), 1000000, replace = TRUE)/10, pgrp=rep(c(1:100),10000))
sf <- dt[
, {
geometry <- sf::st_linestring(x = matrix(c(lon, lat), ncol = 2))
geometry <- sf::st_sfc(geometry)
geometry <- sf::st_sf(geometry = geometry)
}
, by = pgrp
]
sf <- sf::st_as_sf(sf)
在创建LINESTRING时,这假设您的数据是有序的。
答案 1 :(得分:1)
也许是吗?
library(data.table)
library(sf)
dt <- data.table(lon=sample(c(380:390), 1000000, replace = TRUE)/10,lat=sample(c(80:90), 1000000, replace = TRUE)/10, pgrp=rep(c(1:100),10000))
tpathline <- data.table(st_as_sf(dt, coords = c("lon","lat"),crs=4326))
lns = st_as_sf(tpathline[, list(geometry = st_cast(st_union(geometry), "LINESTRING")), by = pgrp])