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?
Edit - Here is my code (still need help with the for loop to create separate DEMs):
library (rgdal)
library (raster)
#Import the DEM
dem <- raster("Path/to/DEM.tif")
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:
dem.copy <- dem
for (i in elevs) {
dem.copy[dem.copy>i]=NA
}
1 Answer 1
You almost had the idea, but since you didn't place your copy inside the loop you already overwrote everything, just put it inside the loop.
library (rgdal)
library (raster)
#Import the DEM
dem <- raster("Path/to/DEM.tif")
elevs <- c(5175.5, 5176.50, 5177.0, 5177.25, 5178.00)
for (i in elevs){
#copy raster, important this is inside the loop
c <- dem
#mask values
c[c > i] = NA
#out_path is whatever you want it to be
writeRaster(c, out_path)
}
-
I would highly recommend not using R primitives/functions as object names as it can have very undesirable outcomes.Jeffrey Evans– Jeffrey Evans2019年09月13日 14:26:19 +00:00Commented Sep 13, 2019 at 14:26
Explore related questions
See similar questions with these tags.
raster[raster > 5105] = NA
with whatever values you want.values = c(5105, 5225, 5450) for (i in values){raster[raster > i] = NA}