Hallie Sheikh
Hallie Sheikh

Reputation: 431

How to remove set range of pixel values of DEM in R?

I have a DEM tiff file of a specific region:

> imported_raster
class       : RasterLayer 
dimensions  : 28034, 53030, 1486643020  (nrow, ncol, ncell)
resolution  : 0.0008333334, 0.0008333334  (x, y)
extent      : 60.85375, 105.0454, 15.95708, 39.31875  (xmin, xmax, ymin, ymax)
coord. ref. : NA 
data source : C:\XX.tif 
names       : XX
values      : -27, 8806  (min, max)
attributes  :
         ID OBJECTID Value Count
 from:    0        1   -27     2
 to  : 8528     8529  8806     1

I want to set a range of pixel values to NULL. For instance if I want to remove elevation pixel values from -27 to 0 , and 0 to 1000 to NULL. How can I carry this about in R?

Upvotes: 1

Views: 534

Answers (1)

Elia
Elia

Reputation: 2584

In your case this should work well:

library(raster)
library(terra)
# with raster -------------------------------------------------------------

r <-  raster()
r[] <- -27:(ncell(r)-28)


new.r <- clamp(r,lower=1000,useValues=F)

# with terra --------------------------------------------------------------

t <- rast(r)
new.t <- clamp(t,lower=1000,values=F)

However, there are several ways to do this, for example with raster::reclassify, terra::classify, but also r[r<1000] <- NA

Upvotes: 1

Related Questions