将 SpatVector 转换为 sf 或 sp

问题描述 投票:0回答:2

是否有一种直接的方法将多边形

SpatVector
类对象(来自
terra
库)转换为简单特征或
SpatialPolygonsDataFrames

谢谢

r r-raster
2个回答
20
投票

示例数据

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 

您可以像这样从

sf
创建一个
SpatVector
对象:

s <- sf::st_as_sf(v)

SpatVector
往另一个方向去创建
sf

vv <- vect(s)

sp
创建一个
SpatVector
对象:

library(raster)
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 

您可以使用 Spatial* 矢量类型对象创建一个 SpatVector

vs <- vect(x)

-1
投票

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

© www.soinside.com 2019 - 2024. All rights reserved.