TransWikia.com

Save multiple raster files as shapefiles

Geographic Information Systems Asked by zigmod on September 3, 2021

I am trying to reproject multiple raster layers and then save the resultant images as shapefiles. I seem to be getting an error this error – inherits(obj, "Spatial") is not TRUE. Below is the code.

outpath <- "output/path"

setwd("X:/input/patht") 

files <- list.files(pattern=".tif") 

outfiles <- paste0(outpath, files)

for(i in 1:length(files)) {

  r <-raster(files[i])

  newproj <- CRS("+proj=longlat +datum=WGS84 +no_defs +ellps=WGS84 +towgs84=0,0,0 ")

  rWGS <-  projectRaster(r, crs= newproj, res= 0.00012603363)

  rc1 <- writeOGR(rWGS, outfiles[i], driver="ESRI Shapefile", overwrite=TRUE)

}

One Answer

Raster files, like tiff images, cannot be directly converted into vector formats. However, it is possible to convert each pixel into small rectangular polygon and save the polygons as vectors into some vector format like shapefile. Some utility programs, like gdal_polygonize https://gdal.org/programs/gdal_polygonize.html, can do a little bit more and merge adjacent pixel-polygons into larger polygons if they have the same pixel values.

The R function to use for converting pixels into polygons is rasterToPolygons https://www.rdocumentation.org/packages/raster/versions/3.3-7/topics/rasterToPolygons

Answered by user30184 on September 3, 2021

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP