简体   繁体   中英

RASTER TO VECTOR : WriteVector from terra package leads to fatal error

I have this sampled code that I tried on RStudio these last days:

library ('terra')

binar <- rast("C:/Users/BIC_/Desktop/21_binarisation_SWIR.tif")
poly_rast <- as.polygons(binar , values = TRUE,extent=FALSE)

I want to convert my raster "binar" into a vector; I have tried with the function "rasterToPolygons" from the package "raster" but it was too long.

After 'vectorizing' my raster I wanted to export it as a shapefile, with this line:

writeVector(poly_rast, filename ="C:/Users/BIC_/Desktop/21_contour_im",filetype = "ESRI Shapefile")

But every time I am doing this it's leading me to a fatal error and R crashes. I do not know how to do and what I've done wrong.

Also I have notice that my poly_rast file has a 0x0 dimension, weird...

> binar
class       : SpatRaster 
dimensions  : 10980, 10980, 1  (nrow, ncol, nlyr)
resolution  : 10, 10  (x, y)
extent      : 3e+05, 409800, 7590220, 7700020  (xmin, xmax, ymin, ymax)
coord. ref. : +proj=utm +zone=38 +south +datum=WGS84 +units=m +no_defs 
source      : 0121_binarisation_SWIR_MOROMBE_T38KLB.tif 
name        : 0121_binarisation_SWIR_MOROMBE_T38KLB 
min value   :                                     0 
max value   :                                     1 
> poly_rast
 class       : SpatVector 
 geometry    : none 
 dimensions  : 0, 0  (geometries, attributes)
 extent      : -180, 180, -90, 90  (xmin, xmax, ymin, ymax)
 coord. ref. : +proj=utm +zone=38 +south +datum=WGS84 +units=m +no_defs 

When plotting "poly_rast" to see what's returned this message appears and my plot window is blank:

Error in g[[i]] : subscript out of bounds

Thanks for your help !!

This is probably a bug in a very old version of terra .

You are using 1.2-5 while the CRAN version is 1.5-21 --- so please update terra and report back. Using the current version is especially important if you report what appears to be a bug (bugs are best reported here ).

You can also install the development version like this: install.packages('terra', repos='https://rspatial.r-universe.dev')

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM