0

I have the following problem trying to somehow solve:

I created 10 arrays of different IDW interpolations using the gstat/gdal package.

Now I have the following variables, which I´m trying to export to my harddrive:

DI.IDW.SAND.P.0.1, DI.IDW.SAND.P.0.2, [...]

In all it´s 10 different arrays for the different powers (ipd values) used during the interpolation.

Manual export to the hard drive works just fine:

writeGDAL(DI.IDW.Sand.P.0.1, fname = vec.rast[1], drivername = "GTiff", mvFlag = -1)

But now automation is where I can´t figure out the correct solution:

I created a data.frame containing the relevant information for the for loop:

runner.sand = data.frame("IDP" = c(0.1,0.2,0.3,0.4,0.5,0.6,0.7,0.8,0.9,1), "str"=c("DI.IDW.Sand.P.0.1", "DI.IDW.Sand.P.0.2", "DI.IDW.Sand.P.0.3", "DI.IDW.Sand.P.0.4", "DI.IDW.Sand.P.0.5", "DI.IDW.Sand.P.0.6", "DI.IDW.Sand.P.0.7", "DI.IDW.Sand.P.0.8", "DI.IDW.Sand.P.0.9", "DI.IDW.Sand.P.1"))

Then I tried:

for (i in c(1:length(runner.sand[,1]))) {
writeGDAL(paste("DI.IDW.Sand.P", runner.sand[i,1], sep = "."), runner.sand[i,2], drivername = "GTiff")
}

But I always get this error:

Error in nchar(fname) : 'nchar()' requires a character vector

Also when trying to automate proj4string I get this error:

for (i in c(1:length(runner.sand[,2]))) {
  proj4string(runner.sand[i,2]) = CRS(paste("+init=epsg:",epsg,sep=""))
  #dat.ov = over(IDP.opt, runner.sand[i,2]) # for later idp optimazation with residual values
  #IDP.opt = cbind(IDP.opt@data, dat.ov)
}

Error in (function (classes, fdef, mtable)  : 
  unable to find an inherited method for function 'proj4string' for signature '"character"'

Again, it perfectly works if I put in the "normal" variable name manually:

proj4string(DI.IDW.Sand.P.0.1) = CRS(paste("+init=epsg:",epsg,sep=""))

I also tried solutions shown here with assign, but nothing does the job. Thanks for your help!

Konrad Rudolph
  • 530,221
  • 131
  • 937
  • 1,214
  • The solution is to have all these values in a (named) list rather than as separate variables. – Konrad Rudolph Mar 26 '19 at 12:15
  • thanks for the help, but I don´t get your point just yet, I tried this before: `testlist = c(DI.IDW.Schluff.P.0.1, DI.IDW.Schluff.P.0.2, DI.IDW.Schluff.P.0.3) for (i in testlist) { writeGDAL(i, fname = i, drivername = "GTiff", mvFlag = -1) }` thats the error: `Error in nchar(fname) : no method for coercing this S4 class to a vector` – Microwave_Woodchuck Mar 26 '19 at 12:36
  • also using `testlist2 = list( DI.IDW.Schluff.P.0.1, DI.IDW.Schluff.P.0.2, DI.IDW.Schluff.P.0.3) for (i in testlist2) { writeGDAL(i, fname = i, drivername = "GTiff", mvFlag = -1) }` doesn´t work.. (same error) – Microwave_Woodchuck Mar 26 '19 at 12:42

0 Answers0