1

I am trying to convert raster to polygon using stars package as suggested in this and this. I have used the following code

library(raster)
library(stars)
library(sf)
library(magrittr)

#read raster r1<-raster("raster_name.asc")

convert raster to polygon, dissolved neighboring same values

r.to.poly<-st_as_stars(r1) %>% st_as_sf(merge = TRUE)

which is giving me the following error

Error in CPL_polygonize(file, mask_name, "GTiff", "Memory", "foo", options, : basic_string::_S_construct null not valid

How to solve this error?

UseR10085
  • 137
  • 10

1 Answers1

2

I ran into the same issue a few days ago. What seemed to work for me was adding a projection before doing the conversion. I also noticed that using a SpatialGridDataFrame (instead of a RasterLayer) object worked much faster. So I suggest you try something like this (assuming you use the WGS84 CRS)

library(sf)
library(rgdal)
library(stars)
library(dplyr)

readGDAL("raster_name.asc", p4s = '+init=EPSG:4326') %>% st_as_stars() %>% st_as_sf(as_points = FALSE, merge = TRUE)