I have taken the reflectance image from Landsat8 bands.

Now the extents of bounding box showing negative values , to proceed to my next level of the process i need to remove this negative values and make them as NULL.

Can you suggest the procedure to do that using R code?


I assume you imported the data using the raster package. If not you should have a look here

If you haven't imported the landsat data as a stack. you could do the following.


s <- stack('path_to_your_file')
s[s==value] <- NA
writeRaster(s, filename, format, ...)

Where "value" is your negative value around the scene. In my example it is replaced by NA. You could also use another value like 0.

If you want to do the same for the single bands (assuming they use 0 for the background) before calculation. You can do the following:

r <- raster('path_to_band_x_file')
r[r==0] <- NA
writeRaster(r, filename, format, ...)

You can use the "dir" function and a for loop to loop through the single bands.

  • can I specify the negative value directly or will it changes for image to image – user28536 Sep 29 '15 at 2:47
  • I can't answer that precisely, as it is depending on what you did with the image and how the original input looks like. It might be that the original input bands in DN have 0 (zero) values in the background. If this is the case you could use the same procedure to set all background values of the single bands to NA before doing your calculations for reflectance. – Generic Wevers Sep 29 '15 at 6:48
  • yes original Lansat Band5 image having 0 's in the background – user28536 Sep 29 '15 at 7:04
  • Is it better to perform background values to NA before reflectance or else after – user28536 Sep 29 '15 at 7:05
  • I've added a few lines to my answer. I am not sure about Landsat8 but calculation of reflectance for Landsat7 has different corrction factors for each band. Thus the background value would change for each scene and each band. By setting the background to NA before calculation you don't have to find the exact background value after calculation. But be careful, to set the values to NA you have to switch from integer to float, else it won't work I guess. – Generic Wevers Sep 29 '15 at 7:14

You could also use reclassify from the raster package:


s <- stack('path_to_your_file')

#check if s is an Raster*object
str(s) #Is Formal class 'RasterLayer'?
#If not: 

s_reclass <- reclassify(s , rcl=cbind(-Inf,0, NA)) #works only for Raster*object
#rcl is a matrix with 3 values (see ?reclassify)! Here it means: reclassify in all bands all values between minus Infinite and 0 to NA

(in my test reclassify is faster than s[s<0]<- NA)

  • Important: rcl needs always a matrix with 3 values: The first two columns are "from" "to" of the input values, and the third column "becomes" has the new value for that range (see ?reclassify). So rcl=cbind(-0.240596,0,NA) would be valid and rcl=cbind(-0.240596,NA) is not valid! – Iris Sep 30 '15 at 9:13
  • when i tried reclassify on the band its changing 0 to NA – user28536 Sep 30 '15 at 9:14
  • when i try that (reclassify) on the reflconS() function output (written in writeGDAL) its not working why? – user28536 Sep 30 '15 at 9:15
  • Can u suggest which of the following is correct reclassify before reflectance or after? – user28536 Sep 30 '15 at 9:16
  • its not happen sir tell me the alternative – user28536 Sep 30 '15 at 9:35

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.