[R-sig-Geo] Setting raster x- and y-resolutions equal

Robert J. Hijmans r.hijmans at gmail.com
Mon Jun 1 21:46:00 CEST 2015


The problem is with  dynatopmodel::upslope.area  that uses
   xres(x)==yres(x)
where
   all.equal(xres(rast), yres(rast))
would be appropriate. One should not compare decimal numbers for
equality using '=='.

rast <- raster(nrows=4072, ncols=3111, xmn=656419.81022503704298,
xmx=730403.88166906696279,

ymn=794177.97668560303282,ymx=891016.01555416302290)
> xres(rast) == yres(rast)
[1] FALSE
> all.equal(xres(rast), yres(rast))
[1] TRUE

Robert

On Thu, May 28, 2015 at 5:37 PM, Gregovich, Dave P (DFG)
<dave.gregovich at alaska.gov> wrote:
> Hello,
> I have a stack of about 40 rasters with about 1 X 10^7 cells in each. I did not realize it, but it turns out the x-resolution and y-resolution differ at about the 13th decimal point. And I would like to feed this stack into a function (dynatopmodel::upslope.area) that requires xres(x)==yres(x).
> Because of the minute difference between the x- and y- resolutions, I am fine just setting the x- and y- resolutions equal to each other. That would save me the time of resampling all of the bands of the stack. However, this does not seem to be working for me when I try to reproduce the problem, as shown below using a single band that mimics a single band from the stack I am working with:
>
> library(raster);library(rgdal)
> options(digits=20)
>
> #Example 1, my situation, which is not working....
> rast <- raster(nrows=4072, ncols=3111, xmn=656419.81022503704298, xmx=730403.88166906696279,
>                                                                 ymn=794177.97668560303282,ymx=891016.01555416302290)
> rast[] <- rnorm(1:ncell(rast))
> proj4string(rast) <- '+init=epsg:26931'
> xres(rast);yres(rast)
> res(rast)<-c(xres(rast),xres(rast))
> #xres and yres differ!!!!....
> res(rast)
>
> #and then two examples that work, Example 2, with a raster of the same size but resetting the resolution to res(rast) <- c(5,5)....
> rast <- raster(nrows=4072, ncols=3111, xmn=656419.81022503704298, xmx=730403.88166906696279,
>                                                                 ymn=794177.97668560303282,ymx=891016.01555416302290)
> rast[] <- rnorm(1:ncell(rast))
> proj4string(rast) <- '+init=epsg:26931'
> xres(rast);yres(rast)
> res(rast)<-c(5,5)
> #new resolution as expected....
> res(rast)
>
> #and example 3, with a raster 100X smaller...
> rast <- raster(nrows=407, ncols=311, xmn=65641.981022503704298, xmx=73040.388166906696279,
>                                                                 ymn=79417.797668560303282,ymx=89101.601555416302290)
> rast[] <- rnorm(1:ncell(rast))
> proj4string(rast) <- '+init=epsg:26931'
> xres(rast);yres(rast)
> res(rast)<-c(xres(rast),xres(rast))
> res(rast)
>
> #salient session info
>
> R version 3.1.3 (2015-03-09)
> Platform: x86_64-w64-mingw32/x64 (64-bit)
> Running under: Windows 7 x64 (build 7601) Service Pack 1
> raster_2.3-33      sp_1.0-17  rgdal_0.9-2
>
> Thanks kindly for any help you can provide, r-sig-geo!
> __________________________________
> Dave Gregovich
> Research Analyst
> Alaska Department of Fish and Game
> Wildlife Conservation Division
> Douglas, AK 99821
> (907) 465-4291
> dave.gregovich at alaska.gov
> __________________________________
>
>
>         [[alternative HTML version deleted]]
>
> _______________________________________________
> R-sig-Geo mailing list
> R-sig-Geo at r-project.org
> https://stat.ethz.ch/mailman/listinfo/r-sig-geo



More information about the R-sig-Geo mailing list