2

Can somebody help me on how to copy multiple filenames in a single CSV file? I have 365 raster files and a polygon dataset with 5 features. What I am trying to do is extract cell values corresponding to polygon features. I am able to do this but not able to copy the corresponding raster filenames in a single CSV. Below is my code:

library(rgdal)
library(raster)

setwd("C:/PHP_Win7/Etios/2007")
polygong <- readOGR("C:/PHP_Win7/Etios/2007", "Ethn")
rasterfiles <- list.files(".", pattern = "tif")

for (j in 1:length(rasterfiles)) {
  curRaster <- raster(paste("C:/PHP_Win7/Etios/2007", rasterfiles[j], sep = "/"))
  csvDatatoWrite <- data.frame(coordinates(polygong),
                               polygong$Name,
                               extract(curRaster, polygong))
  write.table(csvDatatoWrite, file = "output.csv",
              append = TRUE, sep = ",", row.names = F)
}

The output I am getting is

output sample

I would like to replace "extract.curRaster..polygong." column heading in output sample with corresponding filenames. Any help on this?

  • One problem that I see is that using raster extract on a polygon feature class will result in a list object containing multiple raster cell values for each polygon. This is not collapsible into a data.frame because the number of cell values will vary by polygon. If your "polygong" object is a point this is not an issue, unless of course you define the buffer argument in extract. – Jeffrey Evans Sep 8 '16 at 15:48
1

Your assumed data structure is not correct. Given a polygon feature class, using the extract function will result in a list object with multiple raster cell values associated with each polygon. The way you are collapsing the data in a data.frame will yield unexpected and undesirable results.

You need to create an object using extract and the use lapply to summarize the raster data (eg., mean of raster) for each polygon. This will result in a vector that can then be directly matched to your polygon object.

This is readily illustrated in this modified example of extract's documentation.

library(raster)
r <- raster(ncol=50, nrow=50)
  r[] <- runif(ncell(r))
polys <- spPolygons(rbind(c(-180,-20), c(-160,5), c(-60, 0), c(-160,-60), c(-180,-20)),
                    rbind(c(80,0), c(100,60), c(120,0), c(120,-55), c(80,0)))
    polys <- SpatialPolygonsDataFrame(polys, data.frame(ID=1:2))

plot(r)
plot(polys, add=TRUE)

# Extract raster values for each polygon and check object class
( v <- extract(r, polys) )
class( v )

# Use lapply to summarize polygon raster values to mean
( v <- unlist(lapply( v, mean)) )
class( v )

# Join raster means to @data slot in polys
polys@data["r"] <- unlist(lapply( v, mean))
  polys@data["r"]

# Write csv of polygon attributes
write.csv( polys@data, "myfile.csv", row.names = FALSE)

If you, in fact, want all of the raw values associated with each polygon it will be very difficult to write this into a single csv containing all of the polygons, rasters and unique values (which will vary by polygon). You will really need to rethink your data structure and associated code.

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.