Is there a way to convert a raster.tif into a single polygon?

# Loading raster.tif
DEM <- raster ("C: /Users//raster.tif")
# We generate boundary of the raster
poly_DEM <- DEM> -Inf
polygon <- rasterToPolygons (poly_DEM, dissolve = TRUE)*** 

I have tried rasterToPolygons if it works, but for longer rasters it takes too long, I think it is because it has to dissolve many pixels.

In summary, I just want to extract the edge of a raster in a single polygon.


This has nothing to do with improving raster to polygon conversion but, rather a bit of lack of understanding in how this should be applied. Converting a raster to polygons is intended to convert integer rasters with clustered values (eg., landcover) to a vector geometry. You are trying to convert a floating point raster with no discrete spatial patterns. In effect, you are converting every unique cell in your raster to a polygon.

Here is a simple solution to what you are after. The process is to create a raster with a uniform value, based on a copy of your original raster, then just mask it. This will give you a raster with a single value representing the areas with values in the original raster. This can then be converted to polygon(s).

r <- raster(system.file("external/rlogo.grd", 
  r[r >= 200] <- NA

e <- raster(extent(r), nrows = nrow(r), 
            ncols = ncol(r))
  e[] <- 1
    e <- mask(e, r)  

p <- rasterToPolygons(e, dissolve = TRUE)
| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.