Skip to content

Masking a raster changes its min and max values

3 messages · O'Hanlon, Simon J, Thiago V. dos Santos

#
? Dear colleagues,

? To mask this image (note max and min values):
class ? ? ? : RasterLayer?
dimensions ?: 5568, 8289, 46153152 ?(nrow, ncol, ncell)
resolution ?: 0.00898, 0.00898 ?(x, y)
extent ? ? ?: -104.4326, -29.99736, -40.00064, 10 ?(xmin, xmax, ymin, ymax)
coord. ref. : +proj=longlat +datum=WGS84 +no_defs +ellps=WGS84 +towgs84=0,0,0?
values ? ? ?: /mnt/disco3/MODIS/original_images/temp/R_raster_tmp/thiago/raster_tmp_2012-08-16_081042_8103108.grd?
layer name ?: layer?
min value ? : 0?
max value ? : 7?

? I am using raster's mask function. Here is the raster object I am using to mask (which is composed only of 0's and NA's):
class ? ? ? : RasterLayer?
dimensions ?: 5568, 8289, 46153152 ?(nrow, ncol, ncell)
resolution ?: 0.00898, 0.00898 ?(x, y)
extent ? ? ?: -104.4326, -29.99736, -40.00064, 10 ?(xmin, xmax, ymin, ymax)
coord. ref. : +proj=longlat +datum=WGS84 +no_defs +ellps=WGS84 +towgs84=0,0,0?
values ? ? ?: /mnt/disco3/MODIS/original_images/temp/R_raster_tmp/thiago/raster_tmp_2012-08-16_080805_76515.grd?
layer name ?: layer?
min value ? : 1?
max value ? : 1?


? However, please see the min and max values of the resulting raster:
class ? ? ? : RasterLayer?
dimensions ?: 5568, 8289, 46153152 ?(nrow, ncol, ncell)
resolution ?: 0.00898, 0.00898 ?(x, y)
extent ? ? ?: -104.4326, -29.99736, -40.00064, 10 ?(xmin, xmax, ymin, ymax)
coord. ref. : +proj=longlat +datum=WGS84 +no_defs +ellps=WGS84 +towgs84=0,0,0?
values ? ? ?: /mnt/disco3/MODIS/original_images/temp/R_raster_tmp/thiago/raster_tmp_2012-08-16_081343_910828.grd?
layer name ?: layer?
min value ? : 0.1?
max value ? : 6.9?

? Does anyone know why this happens? How can I prevent this from happening?

? Latest R version running with updated packages. See below my session info:
R version 2.15.1 (2012-06-22)
Platform: x86_64-unknown-linux-gnu (64-bit)

locale:
?[1] LC_CTYPE=en_US.UTF-8 ? ? ? LC_NUMERIC=C ? ? ? ? ? ? ? LC_TIME=en_US.UTF-8 ? ? ? ?LC_COLLATE=en_US.UTF-8 ? ?
?[5] LC_MONETARY=en_US.UTF-8 ? ?LC_MESSAGES=en_US.UTF-8 ? ?LC_PAPER=C ? ? ? ? ? ? ? ? LC_NAME=C ? ? ? ? ? ? ? ??
?[9] LC_ADDRESS=C ? ? ? ? ? ? ? LC_TELEPHONE=C ? ? ? ? ? ? LC_MEASUREMENT=en_US.UTF-8 LC_IDENTIFICATION=C ? ? ??

attached base packages:
[1] stats ? ? graphics ?grDevices utils ? ? datasets ?methods ? base ? ??

other attached packages:
[1] ncdf_1.6.6 ? ? ?raster_2.0-08 ? maptools_0.8-16 lattice_0.20-6 ?foreign_0.8-50 ?rgdal_0.7-12 ? ?sp_0.9-99 ? ? ?

loaded via a namespace (and not attached):
[1] grid_2.15.1 ?tools_2.15.1

? Thanks in advance,
? Thiago.
#
Hi Thiago,
mask() simply creates a new raster which sets any cells in the calls first argument (lai.cal) to NA if they are NA in the second argument (qc.cal), the mask raster. As far as I am aware it does not otherwise manipulate values in your original raster. 

I would conclude, that since your rasters have the same extent, resolution and coordinate system, that your mask  raster qc.cal contains NA cells over all cells in your query raster which are 0 and 7. I notice that in your min/max value in qc.cal is 1 not 0 (although this wouldn't necessarily matter, only the location of NA / not NA cells matters).

I do wonder however if you are implying that your data in the lai.cal raster are supposed to be integer values between 0 and 7?

You could just try to plot both rasters to see where the mask non-NA values are and where the 0 and 7 values are in your lai.cal raster. This may show you what is happening?

As it stands it doesn't seem like anything is 'wrong'  as such. Perhaps if this is not what you want, you can outline what output you would like to achieve.

HTH,

Simon

-----Original Message-----
From: r-sig-geo-bounces at r-project.org [mailto:r-sig-geo-bounces at r-project.org] On Behalf Of Thiago Veloso
Sent: 16 August 2012 14:02
To: R-SIG list
Subject: [R-sig-Geo] Masking a raster changes its min and max values

? Dear colleagues,

? To mask this image (note max and min values):
class ? ? ? : RasterLayer
dimensions ?: 5568, 8289, 46153152 ?(nrow, ncol, ncell) resolution ?: 0.00898, 0.00898 ?(x, y) extent ? ? ?: -104.4326, -29.99736, -40.00064, 10 ?(xmin, xmax, ymin, ymax) coord. ref. : +proj=longlat +datum=WGS84 +no_defs +ellps=WGS84 +towgs84=0,0,0 values ? ? ?: /mnt/disco3/MODIS/original_images/temp/R_raster_tmp/thiago/raster_tmp_2012-08-16_081042_8103108.grd
layer name ?: layer
min value ? : 0
max value ? : 7?

? I am using raster's mask function. Here is the raster object I am using to mask (which is composed only of 0's and NA's):
class ? ? ? : RasterLayer
dimensions ?: 5568, 8289, 46153152 ?(nrow, ncol, ncell) resolution ?: 0.00898, 0.00898 ?(x, y) extent ? ? ?: -104.4326, -29.99736, -40.00064, 10 ?(xmin, xmax, ymin, ymax) coord. ref. : +proj=longlat +datum=WGS84 +no_defs +ellps=WGS84 +towgs84=0,0,0 values ? ? ?: /mnt/disco3/MODIS/original_images/temp/R_raster_tmp/thiago/raster_tmp_2012-08-16_080805_76515.grd
layer name ?: layer
min value ? : 1
max value ? : 1?


? However, please see the min and max values of the resulting raster:
class ? ? ? : RasterLayer
dimensions ?: 5568, 8289, 46153152 ?(nrow, ncol, ncell) resolution ?: 0.00898, 0.00898 ?(x, y) extent ? ? ?: -104.4326, -29.99736, -40.00064, 10 ?(xmin, xmax, ymin, ymax) coord. ref. : +proj=longlat +datum=WGS84 +no_defs +ellps=WGS84 +towgs84=0,0,0 values ? ? ?: /mnt/disco3/MODIS/original_images/temp/R_raster_tmp/thiago/raster_tmp_2012-08-16_081343_910828.grd
layer name ?: layer
min value ? : 0.1
max value ? : 6.9?

? Does anyone know why this happens? How can I prevent this from happening?

? Latest R version running with updated packages. See below my session info:
R version 2.15.1 (2012-06-22)
Platform: x86_64-unknown-linux-gnu (64-bit)

locale:
?[1] LC_CTYPE=en_US.UTF-8 ? ? ? LC_NUMERIC=C ? ? ? ? ? ? ? LC_TIME=en_US.UTF-8 ? ? ? ?LC_COLLATE=en_US.UTF-8
?[5] LC_MONETARY=en_US.UTF-8 ? ?LC_MESSAGES=en_US.UTF-8 ? ?LC_PAPER=C ? ? ? ? ? ? ? ? LC_NAME=C
?[9] LC_ADDRESS=C ? ? ? ? ? ? ? LC_TELEPHONE=C ? ? ? ? ? ? LC_MEASUREMENT=en_US.UTF-8 LC_IDENTIFICATION=C ? ? ??

attached base packages:
[1] stats ? ? graphics ?grDevices utils ? ? datasets ?methods ? base ? ??

other attached packages:
[1] ncdf_1.6.6 ? ? ?raster_2.0-08 ? maptools_0.8-16 lattice_0.20-6 ?foreign_0.8-50 ?rgdal_0.7-12 ? ?sp_0.9-99 ? ? ?

loaded via a namespace (and not attached):
[1] grid_2.15.1 ?tools_2.15.1

? Thanks in advance,
? Thiago.


_______________________________________________
R-sig-Geo mailing list
R-sig-Geo at r-project.org
https://stat.ethz.ch/mailman/listinfo/r-sig-geo
#
? Simon,

? Thanks for this valuable discussion. Yes, I do think that having the range changed could be an issue. In the case I showed, mask operation appears to have changed all "0" values of the original raster to "0.1" and all "7" values to "6.9", therefore creating differences in the image. This is my point of view, and please tell me if I am wrong.

? However, it seemed to be an error of the quick stats produced by R. Graphically, by plotting both original and masked images, it looks that the masking operation went ok: no changes in the range and "NA" values in the mask applied to the masked image, which is the result I expected.?To eliminate any remaining doubs, gdalinfo reports that values haven't been touched. See below:

thiago at pocoar:~/Desktop$ gdalinfo original_lai.tif?
Driver: GTiff/GeoTIFF
Files: original_lai.tif
Size is 8289, 5568
Coordinate System is:
GEOGCS["WGS 84",
? ? DATUM["WGS_1984",
? ? ? ? SPHEROID["WGS 84",6378137,298.257223563,
? ? ? ? ? ? AUTHORITY["EPSG","7030"]],
? ? ? ? AUTHORITY["EPSG","6326"]],
? ? PRIMEM["Greenwich",0],
? ? UNIT["degree",0.0174532925199433],
? ? AUTHORITY["EPSG","4326"]]
Origin = (-104.432583131000001,9.999999999000000)
Pixel Size = (0.008980000000000,-0.008980000000000)
Metadata:
? AREA_OR_POINT=Area
Image Structure Metadata:
? COMPRESSION=LZW
? INTERLEAVE=BAND
Corner Coordinates:
Upper Left ?(-104.4325831, ?10.0000000) (104d25'57.30"W, 10d 0' 0.00"N)
Lower Left ?(-104.4325831, -40.0006400) (104d25'57.30"W, 40d 0' 2.30"S)
Upper Right ( -29.9973631, ?10.0000000) ( 29d59'50.51"W, 10d 0' 0.00"N)
Lower Right ( -29.9973631, -40.0006400) ( 29d59'50.51"W, 40d 0' 2.30"S)
Center ? ? ?( -67.2149731, -15.0003200) ( 67d12'53.90"W, 15d 0' 1.15"S)
Band 1 Block=8289x1 Type=Float32, ColorInterp=Gray
? Min=0.000 Max=7.000?
? Minimum=0.000, Maximum=7.000, Mean=0.000, StdDev=0.000
? NoData Value=-999
? Metadata:
? ? STATISTICS_MAXIMUM=7
? ? STATISTICS_MEAN=0
? ? STATISTICS_MINIMUM=0
? ? STATISTICS_STDDEV=0
----------------------------------------------------

thiago at pocoar:~/Desktop$ gdalinfo masked_lai.tif?

Driver: GTiff/GeoTIFF
Files: masked_lai.tif
Size is 8289, 5568
Coordinate System is:
GEOGCS["WGS 84",
? ? DATUM["WGS_1984",
? ? ? ? SPHEROID["WGS 84",6378137,298.257223563,
? ? ? ? ? ? AUTHORITY["EPSG","7030"]],
? ? ? ? AUTHORITY["EPSG","6326"]],
? ? PRIMEM["Greenwich",0],
? ? UNIT["degree",0.0174532925199433],
? ? AUTHORITY["EPSG","4326"]]
Origin = (-104.432583131000001,9.999999999000000)
Pixel Size = (0.008980000000000,-0.008980000000000)
Metadata:
? AREA_OR_POINT=Area
Image Structure Metadata:
? COMPRESSION=LZW
? INTERLEAVE=BAND
Corner Coordinates:
Upper Left ?(-104.4325831, ?10.0000000) (104d25'57.30"W, 10d 0' 0.00"N)
Lower Left ?(-104.4325831, -40.0006400) (104d25'57.30"W, 40d 0' 2.30"S)
Upper Right ( -29.9973631, ?10.0000000) ( 29d59'50.51"W, 10d 0' 0.00"N)
Lower Right ( -29.9973631, -40.0006400) ( 29d59'50.51"W, 40d 0' 2.30"S)
Center ? ? ?( -67.2149731, -15.0003200) ( 67d12'53.90"W, 15d 0' 1.15"S)
Band 1 Block=8289x1 Type=Float32, ColorInterp=Gray
? Min=0.000 Max=7.000?
? Minimum=0.000, Maximum=7.000, Mean=0.000, StdDev=0.000
? NoData Value=-999
? Metadata:
? ? STATISTICS_MAXIMUM=7
? ? STATISTICS_MEAN=0
? ? STATISTICS_MINIMUM=0
? ? STATISTICS_STDDEV=0
----------------------------------------------------


? Regards,
? Thiago.


----- Original Message -----
From: "O'Hanlon, Simon J" <simon.ohanlon at imperial.ac.uk>
To: Thiago Veloso <thi_veloso at yahoo.com.br>; R-SIG list <r-sig-geo at r-project.org>
Cc: 
Sent: Thursday, August 16, 2012 10:47 AM
Subject: RE: [R-sig-Geo] Masking a raster changes its min and max values

Hi Thiago,
mask() simply creates a new raster which sets any cells in the calls first argument (lai.cal) to NA if they are NA in the second argument (qc.cal), the mask raster. As far as I am aware it does not otherwise manipulate values in your original raster. 

I would conclude, that since your rasters have the same extent, resolution and coordinate system, that your mask? raster qc.cal contains NA cells over all cells in your query raster which are 0 and 7. I notice that in your min/max value in qc.cal is 1 not 0 (although this wouldn't necessarily matter, only the location of NA / not NA cells matters).

I do wonder however if you are implying that your data in the lai.cal raster are supposed to be integer values between 0 and 7?

You could just try to plot both rasters to see where the mask non-NA values are and where the 0 and 7 values are in your lai.cal raster. This may show you what is happening?

As it stands it doesn't seem like anything is 'wrong'? as such. Perhaps if this is not what you want, you can outline what output you would like to achieve.

HTH,

Simon

-----Original Message-----
From: r-sig-geo-bounces at r-project.org [mailto:r-sig-geo-bounces at r-project.org] On Behalf Of Thiago Veloso
Sent: 16 August 2012 14:02
To: R-SIG list
Subject: [R-sig-Geo] Masking a raster changes its min and max values

? Dear colleagues,

? To mask this image (note max and min values):
class ? ? ? : RasterLayer
dimensions ?: 5568, 8289, 46153152 ?(nrow, ncol, ncell) resolution ?: 0.00898, 0.00898 ?(x, y) extent ? ? ?: -104.4326, -29.99736, -40.00064, 10 ?(xmin, xmax, ymin, ymax) coord. ref. : +proj=longlat +datum=WGS84 +no_defs +ellps=WGS84 +towgs84=0,0,0 values ? ? ?: /mnt/disco3/MODIS/original_images/temp/R_raster_tmp/thiago/raster_tmp_2012-08-16_081042_8103108.grd
layer name ?: layer
min value ? : 0
max value ? : 7?

? I am using raster's mask function. Here is the raster object I am using to mask (which is composed only of 0's and NA's):
class ? ? ? : RasterLayer
dimensions ?: 5568, 8289, 46153152 ?(nrow, ncol, ncell) resolution ?: 0.00898, 0.00898 ?(x, y) extent ? ? ?: -104.4326, -29.99736, -40.00064, 10 ?(xmin, xmax, ymin, ymax) coord. ref. : +proj=longlat +datum=WGS84 +no_defs +ellps=WGS84 +towgs84=0,0,0 values ? ? ?: /mnt/disco3/MODIS/original_images/temp/R_raster_tmp/thiago/raster_tmp_2012-08-16_080805_76515.grd
layer name ?: layer
min value ? : 1
max value ? : 1?


? However, please see the min and max values of the resulting raster:
class ? ? ? : RasterLayer
dimensions ?: 5568, 8289, 46153152 ?(nrow, ncol, ncell) resolution ?: 0.00898, 0.00898 ?(x, y) extent ? ? ?: -104.4326, -29.99736, -40.00064, 10 ?(xmin, xmax, ymin, ymax) coord. ref. : +proj=longlat +datum=WGS84 +no_defs +ellps=WGS84 +towgs84=0,0,0 values ? ? ?: /mnt/disco3/MODIS/original_images/temp/R_raster_tmp/thiago/raster_tmp_2012-08-16_081343_910828.grd
layer name ?: layer
min value ? : 0.1
max value ? : 6.9?

? Does anyone know why this happens? How can I prevent this from happening?

? Latest R version running with updated packages. See below my session info:
R version 2.15.1 (2012-06-22)
Platform: x86_64-unknown-linux-gnu (64-bit)

locale:
?[1] LC_CTYPE=en_US.UTF-8 ? ? ? LC_NUMERIC=C ? ? ? ? ? ? ? LC_TIME=en_US.UTF-8 ? ? ? ?LC_COLLATE=en_US.UTF-8
?[5] LC_MONETARY=en_US.UTF-8 ? ?LC_MESSAGES=en_US.UTF-8 ? ?LC_PAPER=C ? ? ? ? ? ? ? ? LC_NAME=C
?[9] LC_ADDRESS=C ? ? ? ? ? ? ? LC_TELEPHONE=C ? ? ? ? ? ? LC_MEASUREMENT=en_US.UTF-8 LC_IDENTIFICATION=C ? ? ??

attached base packages:
[1] stats ? ? graphics ?grDevices utils ? ? datasets ?methods ? base ? ??

other attached packages:
[1] ncdf_1.6.6 ? ? ?raster_2.0-08 ? maptools_0.8-16 lattice_0.20-6 ?foreign_0.8-50 ?rgdal_0.7-12 ? ?sp_0.9-99 ? ? ?

loaded via a namespace (and not attached):
[1] grid_2.15.1 ?tools_2.15.1

? Thanks in advance,
? Thiago.


_______________________________________________
R-sig-Geo mailing list
R-sig-Geo at r-project.org
https://stat.ethz.ch/mailman/listinfo/r-sig-geo