Looping Raster Process in R
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
add a comment |
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
add a comment |
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
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
r loops attributes extract raster
asked Nov 21 '18 at 17:58
Lee WalstonLee Walston
31
31
add a comment |
add a comment |
1 Answer
1
active
oldest
votes
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))
add a comment |
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
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
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))
add a comment |
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))
add a comment |
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))
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))
answered Nov 21 '18 at 18:17


Ian WesleyIan Wesley
2,633728
2,633728
add a comment |
add a comment |
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.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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