Mosaikowanie rastrów w R?


10

Próbuję utworzyć mozaikę kilku rastrów w jeden duży raster w R. Za pomocą skryptu opublikowanego w /programming/15287807/how-can-i-create-raster-mosaic-using-list-of-rasters Ale otrzymałem komunikat ostrzegawczy i komunikat o błędzie.

rasters1 <- list.files("F:\\MOD15A2_LAI_1km\\MOD15A2_LAI_2009", 
                      pattern = "mod15a2.a2009001.*.005.*.img$", 
                      full.names = TRUE, recursive = TRUE)

mos1 <-mosaic(rasters1, fun=mean)

Zgłaszał błąd, jak poniżej

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

Potem wypróbowałem inną wersję.

rasters1.mosaicargs <- rasters1
rasters1.mosaicargs$fun <- mean

Ale tutaj jakiś komunikat ostrzegawczy, jak poniżej

Warning message:
In rasters1.mosaicargs$fun <- mean : Coercing LHS to a list

Zignorowałem wiadomość, a następnie kontynuowałem

mos2 <- do.call(mosaic, rasters1.mosaicargs)

ale tutaj ten sam błąd wymieniony powyżej

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

Znalazłem również następujący skrypt, ale nie działa nceas.ucsb.edu/scicomp/usecases/createrasterimagemosaic
Vandka

Odpowiedzi:


17

Problem polega na tym, że mozaika i do.call oczekują na liście obiektu rastrowego, a nie tylko nazw rastrowych zawartych w wektorze „rasters1”. W rzeczywistości pytasz o mozaikę nazwy w wektorze, a nie o obiekcie rastrowym.

# Create some example data
require(raster)
    r <- raster(ncol=100, nrow=100)
      r1 <- crop(r, extent(-10, 11, -10, 11))
        r1[] <- 1:ncell(r1)
          r2 <- crop(r, extent(0, 20, 0, 20))
          r2[] <- 1:ncell(r2)
      r3 <- crop(r, extent(9, 30, 9, 30))
    r3[] <- 1:ncell(r3)

# If I create a list object of the raster names, as your are doing with list.files, 
#    do.call will fail with a character signature error 
rast.list <- list("r1","r2","r3")   
  rast.list$fun <- mean     
    rast.mosaic <- do.call(mosaic,rast.list)

# However, if I create a list contaning raster objects, the do.call function 
#   will work when mosaic is passed to it.      
rast.list <- list(r1, r2, r3)     
  rast.list$fun <- mean
    rast.mosaic <- do.call(mosaic,rast.list)
      plot(rast.mosaic)

# You could specify a for loop to create a list object, 
#   contaning raster objects
rasters1 <- list.files("F:/MOD15A2_LAI_1km/MOD15A2_LAI_2009", 
                       pattern="mod15a2.a2009001.*.005.*.img$", 
                       full.names=TRUE, recursive=TRUE)
rast.list <- list()
  for(i in 1:length(rasters1)) { rast.list[i] <- raster(rasters1[i]) }

# And then use do.call on the list of raster objects
rast.list$fun <- mean
  rast.mosaic <- do.call(mosaic,rast.list)
    plot(rast.mosaic)

1

Tylko niewielka odmiana na temat. Możesz uniknąć tworzenia pustej listy i pętli for ...

    rast.list <- list()

    for(i in 1:length(rasters1)) { 
rast.list[i] <- raster(rasters1[i])
}

... z głupkowatym poleceniem.

    rast.list <- lapply(1:length(rasters1),
 function(x) {
raster(rasters1[x])
})
Korzystając z naszej strony potwierdzasz, że przeczytałeś(-aś) i rozumiesz nasze zasady używania plików cookie i zasady ochrony prywatności.
Licensed under cc by-sa 3.0 with attribution required.