This function uses the gdal_polygonize.py GDAL utility. Its implementation was adapted from the solution developed by John Baumgartner and Francisco Rodriguez-Sanchez.
APpolygonize(
inRaster,
readToMemory = TRUE,
outFile = NULL,
OSGeoPath = "C:/OSGeo4W64",
connectivity = 4
)
a RasterLayer or a path to a raster file
logical. Read output polygons into memory as a SpatialPolygonsDataFrame
character. Optional path for saving output as an Esri Shapefile.
character. Path to the OSGeo4W installation directory
numeric. Can be either set to 4 (rook's case) or 8 (queen's case)
SpatialPolygonsDataFrame
This function needs OSGeo4W to be installed. The OSGeo4W installation path, set to 'C:/OSGeo4W64' by default, will then be used to find the gdal_polygonize.bat file.
GDAL: https://gdal.org/
OSGeo4W download page: https://trac.osgeo.org/osgeo4w/
John Baumgartner's blog post on gdal_polygonize: https://johnbaumgartner.wordpress.com/2012/07/26/getting-rasters-into-shape-from-r/