Solutions for lack of memory using extract() in a large raster image












0














I have a Core i5 CPU and 8GB RAM computer, but I've like to use R and raster package (extract() function) for pixel calculations (SD, Skewness and Kurtosis in 1 unit radius of a buffer) in a geoTiff raster, but my original geoTiff image has 6244(nrow), 8721(ncol) and 54453924 (ncell) dimensions. This image size causes lack of memory, for example in my code below:



### <code r>
#Packages
library(raster)
library(rgdal)
library(moments) #Measures of Skewness and Kurtosis

memory.limit(size=50000)#Increase memory

## Create artificial raster for my geoTiff simulation - dimensions of my original geoTiff: 6244, 8721, 54453924 (nrow, ncol, ncell)
r <- raster(nc=8721, nr=6244)
r <- setValues(r, round(runif(ncell(r))* 255))

##Create geoTiff
writeRaster(r, "r.tif", drivername="GTiff")

##Open geoTiff
r2<-raster("r.tif")

#Extract all pixel coordinates in raster
coord_r<-coordinates(r2)

#Extract standard deviation, skewness and kurtosis
Buffer<-1
SD<-function (x, na.rm = TRUE)
{
if (is.matrix(x))
apply(x, 2, sd, na.rm = na.rm)
else if (is.vector(x))
sqrt(var(x, na.rm = na.rm))
else if (is.data.frame(x))
sapply(x, sd, na.rm = na.rm)
else sqrt(var(as.vector(x), na.rm = na.rm))
}
desv_pad_R<-extract(r2, coord_r, buffer = Buffer, fun = SD)
str(desv_pad_R)
sk_R <-extract(r2,coord_r,buffer=Buffer, fun=skewness, na.rm = TRUE)
str(sk_R)
k_R <-extract(r2,coord_r,buffer=Buffer, fun=kurtosis, na.rm = TRUE)
str(k_R)
# <END code>


There are different approaches (eg. integration with SAGA GIS or GRASS, using RQGIS, convert image to ASCII) for my problem using the same computer and working in R?



Thanks in advanced,










share|improve this question





























    0














    I have a Core i5 CPU and 8GB RAM computer, but I've like to use R and raster package (extract() function) for pixel calculations (SD, Skewness and Kurtosis in 1 unit radius of a buffer) in a geoTiff raster, but my original geoTiff image has 6244(nrow), 8721(ncol) and 54453924 (ncell) dimensions. This image size causes lack of memory, for example in my code below:



    ### <code r>
    #Packages
    library(raster)
    library(rgdal)
    library(moments) #Measures of Skewness and Kurtosis

    memory.limit(size=50000)#Increase memory

    ## Create artificial raster for my geoTiff simulation - dimensions of my original geoTiff: 6244, 8721, 54453924 (nrow, ncol, ncell)
    r <- raster(nc=8721, nr=6244)
    r <- setValues(r, round(runif(ncell(r))* 255))

    ##Create geoTiff
    writeRaster(r, "r.tif", drivername="GTiff")

    ##Open geoTiff
    r2<-raster("r.tif")

    #Extract all pixel coordinates in raster
    coord_r<-coordinates(r2)

    #Extract standard deviation, skewness and kurtosis
    Buffer<-1
    SD<-function (x, na.rm = TRUE)
    {
    if (is.matrix(x))
    apply(x, 2, sd, na.rm = na.rm)
    else if (is.vector(x))
    sqrt(var(x, na.rm = na.rm))
    else if (is.data.frame(x))
    sapply(x, sd, na.rm = na.rm)
    else sqrt(var(as.vector(x), na.rm = na.rm))
    }
    desv_pad_R<-extract(r2, coord_r, buffer = Buffer, fun = SD)
    str(desv_pad_R)
    sk_R <-extract(r2,coord_r,buffer=Buffer, fun=skewness, na.rm = TRUE)
    str(sk_R)
    k_R <-extract(r2,coord_r,buffer=Buffer, fun=kurtosis, na.rm = TRUE)
    str(k_R)
    # <END code>


    There are different approaches (eg. integration with SAGA GIS or GRASS, using RQGIS, convert image to ASCII) for my problem using the same computer and working in R?



    Thanks in advanced,










    share|improve this question



























      0












      0








      0


      1





      I have a Core i5 CPU and 8GB RAM computer, but I've like to use R and raster package (extract() function) for pixel calculations (SD, Skewness and Kurtosis in 1 unit radius of a buffer) in a geoTiff raster, but my original geoTiff image has 6244(nrow), 8721(ncol) and 54453924 (ncell) dimensions. This image size causes lack of memory, for example in my code below:



      ### <code r>
      #Packages
      library(raster)
      library(rgdal)
      library(moments) #Measures of Skewness and Kurtosis

      memory.limit(size=50000)#Increase memory

      ## Create artificial raster for my geoTiff simulation - dimensions of my original geoTiff: 6244, 8721, 54453924 (nrow, ncol, ncell)
      r <- raster(nc=8721, nr=6244)
      r <- setValues(r, round(runif(ncell(r))* 255))

      ##Create geoTiff
      writeRaster(r, "r.tif", drivername="GTiff")

      ##Open geoTiff
      r2<-raster("r.tif")

      #Extract all pixel coordinates in raster
      coord_r<-coordinates(r2)

      #Extract standard deviation, skewness and kurtosis
      Buffer<-1
      SD<-function (x, na.rm = TRUE)
      {
      if (is.matrix(x))
      apply(x, 2, sd, na.rm = na.rm)
      else if (is.vector(x))
      sqrt(var(x, na.rm = na.rm))
      else if (is.data.frame(x))
      sapply(x, sd, na.rm = na.rm)
      else sqrt(var(as.vector(x), na.rm = na.rm))
      }
      desv_pad_R<-extract(r2, coord_r, buffer = Buffer, fun = SD)
      str(desv_pad_R)
      sk_R <-extract(r2,coord_r,buffer=Buffer, fun=skewness, na.rm = TRUE)
      str(sk_R)
      k_R <-extract(r2,coord_r,buffer=Buffer, fun=kurtosis, na.rm = TRUE)
      str(k_R)
      # <END code>


      There are different approaches (eg. integration with SAGA GIS or GRASS, using RQGIS, convert image to ASCII) for my problem using the same computer and working in R?



      Thanks in advanced,










      share|improve this question















      I have a Core i5 CPU and 8GB RAM computer, but I've like to use R and raster package (extract() function) for pixel calculations (SD, Skewness and Kurtosis in 1 unit radius of a buffer) in a geoTiff raster, but my original geoTiff image has 6244(nrow), 8721(ncol) and 54453924 (ncell) dimensions. This image size causes lack of memory, for example in my code below:



      ### <code r>
      #Packages
      library(raster)
      library(rgdal)
      library(moments) #Measures of Skewness and Kurtosis

      memory.limit(size=50000)#Increase memory

      ## Create artificial raster for my geoTiff simulation - dimensions of my original geoTiff: 6244, 8721, 54453924 (nrow, ncol, ncell)
      r <- raster(nc=8721, nr=6244)
      r <- setValues(r, round(runif(ncell(r))* 255))

      ##Create geoTiff
      writeRaster(r, "r.tif", drivername="GTiff")

      ##Open geoTiff
      r2<-raster("r.tif")

      #Extract all pixel coordinates in raster
      coord_r<-coordinates(r2)

      #Extract standard deviation, skewness and kurtosis
      Buffer<-1
      SD<-function (x, na.rm = TRUE)
      {
      if (is.matrix(x))
      apply(x, 2, sd, na.rm = na.rm)
      else if (is.vector(x))
      sqrt(var(x, na.rm = na.rm))
      else if (is.data.frame(x))
      sapply(x, sd, na.rm = na.rm)
      else sqrt(var(as.vector(x), na.rm = na.rm))
      }
      desv_pad_R<-extract(r2, coord_r, buffer = Buffer, fun = SD)
      str(desv_pad_R)
      sk_R <-extract(r2,coord_r,buffer=Buffer, fun=skewness, na.rm = TRUE)
      str(sk_R)
      k_R <-extract(r2,coord_r,buffer=Buffer, fun=kurtosis, na.rm = TRUE)
      str(k_R)
      # <END code>


      There are different approaches (eg. integration with SAGA GIS or GRASS, using RQGIS, convert image to ASCII) for my problem using the same computer and working in R?



      Thanks in advanced,







      r gis raster geotiff






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Nov 20 at 17:18

























      asked Nov 20 at 17:09









      Leprechault

      8010




      8010





























          active

          oldest

          votes











          Your Answer






          StackExchange.ifUsing("editor", function () {
          StackExchange.using("externalEditor", function () {
          StackExchange.using("snippets", function () {
          StackExchange.snippets.init();
          });
          });
          }, "code-snippets");

          StackExchange.ready(function() {
          var channelOptions = {
          tags: "".split(" "),
          id: "1"
          };
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function() {
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled) {
          StackExchange.using("snippets", function() {
          createEditor();
          });
          }
          else {
          createEditor();
          }
          });

          function createEditor() {
          StackExchange.prepareEditor({
          heartbeatType: 'answer',
          autoActivateHeartbeat: false,
          convertImagesToLinks: true,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: 10,
          bindNavPrevention: true,
          postfix: "",
          imageUploader: {
          brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
          contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
          allowUrls: true
          },
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          });


          }
          });














          draft saved

          draft discarded


















          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53398089%2fsolutions-for-lack-of-memory-using-extract-in-a-large-raster-image%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown






























          active

          oldest

          votes













          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes
















          draft saved

          draft discarded




















































          Thanks for contributing an answer to Stack Overflow!


          • Please be sure to answer the question. Provide details and share your research!

          But avoid



          • Asking for help, clarification, or responding to other answers.

          • Making statements based on opinion; back them up with references or personal experience.


          To learn more, see our tips on writing great answers.





          Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


          Please pay close attention to the following guidance:


          • Please be sure to answer the question. Provide details and share your research!

          But avoid



          • Asking for help, clarification, or responding to other answers.

          • Making statements based on opinion; back them up with references or personal experience.


          To learn more, see our tips on writing great answers.




          draft saved


          draft discarded














          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53398089%2fsolutions-for-lack-of-memory-using-extract-in-a-large-raster-image%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown





















































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown

































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown







          Popular posts from this blog

          Wiesbaden

          Marschland

          Dieringhausen