将 SpatVector 转换为 sf 或 sp

Converting SpatVector to sf or sp

是否有直接的方法将多边形 SpatVector class 对象(来自 terra 库)转换为简单特征或 SpatialPolygonsDataFrames

谢谢

这是一个解决方法 (https://www.gitmemory.com/issue/rspatial/terra/71/660682352)。您可以使用相同的但不是 'sf' 使用 'sp'。希望对你有用。

示例数据

library(terra)
f <- system.file("ex/lux.shp", package="terra")
v <- vect(f)
v
#class       : SpatVector 
#geometry    : polygons 
#elements    : 12
#extent      : 5.74414, 6.528252, 49.44781, 50.18162  (xmin, xmax, ymin, ymax)
#coord. ref. : +proj=longlat +datum=WGS84 +no_defs 
#names       : ID_1, NAME_1, ID_2, NAME_2, AREA 

使用当前 terrasf,您可以从 SpatVector 创建一个 sf 对象,如下所示:

s <- sf::st_as_sf(v)

然后回到 SpatVector

vv <- vect(s)

SpatVector

创建一个 sp 对象
x <- as(v, "Spatial")
x
#class       : SpatialPolygonsDataFrame 
#features    : 12 
#extent      : 5.74414, 6.528252, 49.44781, 50.18162  (xmin, xmax, ymin, ymax)
#crs         : +proj=longlat +datum=WGS84 +no_defs 
#variables   : 5
#names       : ID_1,     NAME_1, ID_2,   NAME_2, AREA 
#min values  :    1,   Diekirch,    1, Capellen,   76 
#max values  :    3, Luxembourg,   12,    Wiltz,  312