Looping Raster Process in R












0















I'd like to use the Raster package in R to process a single DEM. I'd like to use a pre-determined list of elevation values to extract multiple raster datasets from this DEM. Not all values are of equal interval. For example, my DEM ranges between 5,000 and 6,000 ft. I'd like to use a pre-determined list of 10 values to clip this raster (similar to "extract by attribute" tool in Spatial Analyst). The values do not have equal intervals, for example: 5105 5225 5450 5500 . . . and so on...



At each step, I'd like to extract all values LESS than the specific value (e.g., where Value < 5,105).



If I were to do this manually, I'd set up a batch run to repeatedly use the "extract by attribute" tool in Spatial Analyst. I don't want to do that. I will have a lot of input DEMs to do in the future and I'd like to develop a script to chug through these quickly.



Anyone have any ideas?



Here is my code (still need help with the for loop to create separate DEMs). The code below does not produce separate output rasters at each elevation slice. I need to create separate output rasters (the number of output rasters equal the number of values in the elevs list). TIA!



library (rgdal) 
library (raster)
#Import the DEM
dem <- raster("Path/to/DEM.tif")

#List of Elevations
elevs = c(5175.5, 5176.50, 5177.0, 5177.25, 5178.00)

#Extract DEM at at elevations less than elevs list
#This can be done manually as follows:
dem.5175.5 <- dem
dem.5175.5[dem.5175.5>5175.5]=NA

#Trying to do this iteratively through the list of elevs
#Need help here...

dem.copy <- dem
for (i in elevs) {
dem.copy[dem.copy>i]=NA
}









share|improve this question



























    0















    I'd like to use the Raster package in R to process a single DEM. I'd like to use a pre-determined list of elevation values to extract multiple raster datasets from this DEM. Not all values are of equal interval. For example, my DEM ranges between 5,000 and 6,000 ft. I'd like to use a pre-determined list of 10 values to clip this raster (similar to "extract by attribute" tool in Spatial Analyst). The values do not have equal intervals, for example: 5105 5225 5450 5500 . . . and so on...



    At each step, I'd like to extract all values LESS than the specific value (e.g., where Value < 5,105).



    If I were to do this manually, I'd set up a batch run to repeatedly use the "extract by attribute" tool in Spatial Analyst. I don't want to do that. I will have a lot of input DEMs to do in the future and I'd like to develop a script to chug through these quickly.



    Anyone have any ideas?



    Here is my code (still need help with the for loop to create separate DEMs). The code below does not produce separate output rasters at each elevation slice. I need to create separate output rasters (the number of output rasters equal the number of values in the elevs list). TIA!



    library (rgdal) 
    library (raster)
    #Import the DEM
    dem <- raster("Path/to/DEM.tif")

    #List of Elevations
    elevs = c(5175.5, 5176.50, 5177.0, 5177.25, 5178.00)

    #Extract DEM at at elevations less than elevs list
    #This can be done manually as follows:
    dem.5175.5 <- dem
    dem.5175.5[dem.5175.5>5175.5]=NA

    #Trying to do this iteratively through the list of elevs
    #Need help here...

    dem.copy <- dem
    for (i in elevs) {
    dem.copy[dem.copy>i]=NA
    }









    share|improve this question

























      0












      0








      0








      I'd like to use the Raster package in R to process a single DEM. I'd like to use a pre-determined list of elevation values to extract multiple raster datasets from this DEM. Not all values are of equal interval. For example, my DEM ranges between 5,000 and 6,000 ft. I'd like to use a pre-determined list of 10 values to clip this raster (similar to "extract by attribute" tool in Spatial Analyst). The values do not have equal intervals, for example: 5105 5225 5450 5500 . . . and so on...



      At each step, I'd like to extract all values LESS than the specific value (e.g., where Value < 5,105).



      If I were to do this manually, I'd set up a batch run to repeatedly use the "extract by attribute" tool in Spatial Analyst. I don't want to do that. I will have a lot of input DEMs to do in the future and I'd like to develop a script to chug through these quickly.



      Anyone have any ideas?



      Here is my code (still need help with the for loop to create separate DEMs). The code below does not produce separate output rasters at each elevation slice. I need to create separate output rasters (the number of output rasters equal the number of values in the elevs list). TIA!



      library (rgdal) 
      library (raster)
      #Import the DEM
      dem <- raster("Path/to/DEM.tif")

      #List of Elevations
      elevs = c(5175.5, 5176.50, 5177.0, 5177.25, 5178.00)

      #Extract DEM at at elevations less than elevs list
      #This can be done manually as follows:
      dem.5175.5 <- dem
      dem.5175.5[dem.5175.5>5175.5]=NA

      #Trying to do this iteratively through the list of elevs
      #Need help here...

      dem.copy <- dem
      for (i in elevs) {
      dem.copy[dem.copy>i]=NA
      }









      share|improve this question














      I'd like to use the Raster package in R to process a single DEM. I'd like to use a pre-determined list of elevation values to extract multiple raster datasets from this DEM. Not all values are of equal interval. For example, my DEM ranges between 5,000 and 6,000 ft. I'd like to use a pre-determined list of 10 values to clip this raster (similar to "extract by attribute" tool in Spatial Analyst). The values do not have equal intervals, for example: 5105 5225 5450 5500 . . . and so on...



      At each step, I'd like to extract all values LESS than the specific value (e.g., where Value < 5,105).



      If I were to do this manually, I'd set up a batch run to repeatedly use the "extract by attribute" tool in Spatial Analyst. I don't want to do that. I will have a lot of input DEMs to do in the future and I'd like to develop a script to chug through these quickly.



      Anyone have any ideas?



      Here is my code (still need help with the for loop to create separate DEMs). The code below does not produce separate output rasters at each elevation slice. I need to create separate output rasters (the number of output rasters equal the number of values in the elevs list). TIA!



      library (rgdal) 
      library (raster)
      #Import the DEM
      dem <- raster("Path/to/DEM.tif")

      #List of Elevations
      elevs = c(5175.5, 5176.50, 5177.0, 5177.25, 5178.00)

      #Extract DEM at at elevations less than elevs list
      #This can be done manually as follows:
      dem.5175.5 <- dem
      dem.5175.5[dem.5175.5>5175.5]=NA

      #Trying to do this iteratively through the list of elevs
      #Need help here...

      dem.copy <- dem
      for (i in elevs) {
      dem.copy[dem.copy>i]=NA
      }






      r loops attributes extract raster






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Nov 21 '18 at 17:58









      Lee WalstonLee Walston

      31




      31
























          1 Answer
          1






          active

          oldest

          votes


















          0














          I don't have a DEM file to test with, but I would write a helper function and use lapply to produce a list of dems split by your elevs vector. Here is a quick example:



          DemSplit <- function(dem, elev){
          dem[dem>elev] <- NA
          dem
          }

          lapply(elev, function(x) DemSplit(dem, x))





          share|improve this answer























            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%2f53418026%2flooping-raster-process-in-r%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown

























            1 Answer
            1






            active

            oldest

            votes








            1 Answer
            1






            active

            oldest

            votes









            active

            oldest

            votes






            active

            oldest

            votes









            0














            I don't have a DEM file to test with, but I would write a helper function and use lapply to produce a list of dems split by your elevs vector. Here is a quick example:



            DemSplit <- function(dem, elev){
            dem[dem>elev] <- NA
            dem
            }

            lapply(elev, function(x) DemSplit(dem, x))





            share|improve this answer




























              0














              I don't have a DEM file to test with, but I would write a helper function and use lapply to produce a list of dems split by your elevs vector. Here is a quick example:



              DemSplit <- function(dem, elev){
              dem[dem>elev] <- NA
              dem
              }

              lapply(elev, function(x) DemSplit(dem, x))





              share|improve this answer


























                0












                0








                0







                I don't have a DEM file to test with, but I would write a helper function and use lapply to produce a list of dems split by your elevs vector. Here is a quick example:



                DemSplit <- function(dem, elev){
                dem[dem>elev] <- NA
                dem
                }

                lapply(elev, function(x) DemSplit(dem, x))





                share|improve this answer













                I don't have a DEM file to test with, but I would write a helper function and use lapply to produce a list of dems split by your elevs vector. Here is a quick example:



                DemSplit <- function(dem, elev){
                dem[dem>elev] <- NA
                dem
                }

                lapply(elev, function(x) DemSplit(dem, x))






                share|improve this answer












                share|improve this answer



                share|improve this answer










                answered Nov 21 '18 at 18:17









                Ian WesleyIan Wesley

                2,633728




                2,633728
































                    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.




                    draft saved


                    draft discarded














                    StackExchange.ready(
                    function () {
                    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53418026%2flooping-raster-process-in-r%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

                    MongoDB - Not Authorized To Execute Command

                    How to fix TextFormField cause rebuild widget in Flutter

                    in spring boot 2.1 many test slices are not allowed anymore due to multiple @BootstrapWith