Skip to content

merge large number of raster objects

5 messages · Henrik Bengtsson, Rolf Turner, Ben Zaitchik

#
Hello,

I have a large number of raster objects in memory, with names RH100, 
RH101, RH102, etc. (myobjects <- ls(pattern='^RH')).
These rasters are sections of a continuous map, and I would like to 
combine them using the RasterObject merge tool in package 'raster.'

Merge expects an input list of raster objects (outmap<-merge(x, y, ...), 
where x, y, and ... are raster objects).  I can run the command 
successfully if I type in every raster object that I want to merge, but 
this is impractical for the large number of objects I'm combining.

I would like to apply merge to a list of object names defined using some 
kind of wildcard-based list command, but I'm struggling to find the 
right data types in R.

Is there some way to convert a vector of strings (e.g., 
as.vector(ls(pattern='^RH'))) to a vector of object names (as.names??) 
that could be specified as the input to the merge function?  What I'd 
really like to do is something like outmap<-merge(myobjects[1:40]), in 
order to merge the 40 raster objects, but I recognize that it might not 
be so simple.

Advance apologies if this is already dealt with on the help list . . . 
I've gone in circles on wildcard, lapply, and names threads and haven't 
managed to get anything to work.

Thank you,
Ben
#
See ?get and ?do.call.  That should be enough.

?Reduce may be an alternative for do.call(), but could also be less
memory efficient.

My $.02

/Henrik
On Thu, Jun 9, 2011 at 7:57 PM, Ben Zaitchik <zaitchik at jhu.edu> wrote:
#
...and ?mget for retrieving multiple objects as a list.  /Henrik
On Thu, Jun 9, 2011 at 10:32 PM, Henrik Bengtsson <hb at biostat.ucsf.edu> wrote:
#
On 10/06/11 14:57, Ben Zaitchik wrote:
I think that

     outmap <- do.call(merge,myobjects)

should work for you.

     cheers,

         Rolf Turner
#
Thank you Henrik and Rolf.  My migraine just disappeared. : )

  myobjects = ls(pattern='^RH')
  dataobj = mget(myTemp,.GlobalEnv)
  map = Reduce(merge,dataobj)

did the trick.  do.call() didn't cut it for some reason . . . it 
returned an " 'x' is missing " error.  But Reduce works quite nicely for 
the rasters I'm working with.

Thanks again,
Ben
On 6/10/2011 2:06 AM, Henrik Bengtsson wrote: