Skip to content

Instantly share code, notes, and snippets.

@cengel
Last active July 27, 2016 23:09
Show Gist options
  • Save cengel/4992a0766070cbfd74f305c105ce819f to your computer and use it in GitHub Desktop.
Save cengel/4992a0766070cbfd74f305c105ce819f to your computer and use it in GitHub Desktop.
##
## this snippet loops through a directory with rasters and crates polygons
## using a call to gdal_polygonize.py
## (for Mac, with kynchaos GDAL framework)
##
# make sure to not mess up the paths here!
setwd("root/dir")
indir <- "rasterdir-ending-with-slash/"
outdir <- "polydir-ending-with-slash/"
pth <- "/path/to/gdal_polygonize.py"
# let's do it
infiles <- list.files(indir)
outfiles <- sub(".asc", ".shp", infiles) # or replace .asc with .tif
dir.create(file.path(".", outdir), showWarnings = FALSE)
sapply(1:length(infiles), function(i)
system(paste(pth, paste0(indir, infiles[i]), "-f 'ESRI Shapefile'", paste0(outdir, outfiles[i]))))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment