[R] raster package: merge/mosaic

halim10-fes halim10-fes at sust.edu
Thu Oct 24 07:01:46 CEST 2013


Hi,

With the 'merge' function using different tolerance level, I've come up with a 
solution.

For e.g. consider r1 and r2 as two raster layers, attributes are as follows:

>r1 <- raster(xmx=-150, ymn=60, ncols=30, nrows=20)
>r1[]<-1:ncell(r1)
>r1

class       : RasterLayer 
dimensions  : 20, 30, 600  (nrow, ncol, ncell)
resolution  : 1, 1.5  (x, y)
extent      : -180, -150, 60, 90  (xmin, xmax, ymin, ymax)
coord. ref. : +proj=longlat +datum=WGS84 
data source : in memory
names       : layer 
values      : 1, 600  (min, max)


>origin(r1)
[1] 0 0
 
>r2 <- raster(xmn=-100, xmx=-50, ymx=50, ymn=30,ncols=25,nrows=30)
>res(r2)<-c(xres(r1),yres(r1))
>r2[]<-1:ncell(r2)
> r2

class       : RasterLayer 
dimensions  : 13, 50, 650  (nrow, ncol, ncell)
resolution  : 1, 1.5  (x, y)
extent      : -100, -50, 30.5, 50  (xmin, xmax, ymin, ymax)
coord. ref. : +proj=longlat +datum=WGS84 
data source : in memory
names       : layer 
values      : 1, 650  (min, max)

>origin(r2)
[1] 0.0 0.5

If we try to merge r1 and r2,

>merge(r1,r2)
Error in compareRaster(x, extent = FALSE, rowcol = FALSE, orig = TRUE,  : 
  different origin

So, they have different origins. Here the default tolerance value was = 0.05, 
I've changed it to 0.5 after a few trial and error.

So, finally,

>merge(r1,r2,tolerance=0.5)

class       : RasterLayer 
dimensions  : 40, 130, 5200  (nrow, ncol, ncell)
resolution  : 1, 1.5  (x, y)
extent      : -180, -50, 30.5, 90.5  (xmin, xmax, ymin, ymax)
coord. ref. : +proj=longlat +datum=WGS84 
data source : in memory
names       : layer 
values      : 1, 650  (min, max)

Worked! 

Hope this might help you to solve your problem. And also see what others 
say...

Regards,

Halim

---------------
Md. Abdul Halim
Assistant Professor
Department of Forestry and Environmental Science
Shahjalal University of Science and Technology,Sylhet-3114,
Bangladesh.


On Wed, 23 Oct 2013 12:18:40 -0700 (PDT), Carl Witthoft wrote
> According to ?merge,  you could try adjusting the default value of 
> the "tolerance" argument.
> 
> Nora Ernst wrote
> > I'm working with raster data (satellite imagery) and the raster package.
> > The
> >    idea is to merge two raster files that are partially overlaping, do
> > have the
> >    same coordinate system and resolution but not the same origin. As
> > expected,
> >    the functions mosaic(r1,r2) as well as merge(r1,r2)) give the error
> > message:
> > 
> > Error in compareRaster(x, extent = FALSE, rowcol = FALSE, orig = TRUE,  : 
> >   different origin
> > 
> > 
> >> origin(r1)
> > [1] 5.582522e-05 1.124150e-03
> >> origin(r2)
> > [1]  0.001054868 -0.001124150
> > 
> > 
> >    Can  anyone tell me how it is possible to merge rasters with different
> >    origin?
> 
> --
> View this message in context: http://r.789695.n4.nabble.com/raster-
> package-merge-mosaic-tp4678890p4678899.html Sent from the R help 
> mailing list archive at Nabble.com.
> 
> ______________________________________________
> R-help at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.
> 
> -- 
> This message has been scanned for viruses and
> dangerous content by MailScanner, and is
> believed to be clean.


-- 
This message has been scanned for viruses and
dangerous content by MailScanner, and is
believed to be clean.



More information about the R-help mailing list