# [R-sig-Geo] Merging raster does not work properly

Michael Sumner mdsumner at gmail.com
Wed Sep 25 01:28:54 CEST 2013

```It seems you have divided the maximum x value by two, rather than the
x extent?  See the source for rotate which shows how to do this, but
it will probably work straight out anyway, (albeit with a warning for
data that's not in the lonlat range):

rotate(r)

Source, this uses internal structures but you could recreate with
functions xmin()/xmax()/ymin()/ymax():
findMethods("rotate")[["Raster"]]

On Wed, Sep 25, 2013 at 9:05 AM, Alejandro Pietrek
<alepietrek at yahoo.com.ar> wrote:
> Hi all,
>           I have a raster r I want to cut  in two, then place the right side to the left, and the left side to the right, and finally merge these two rasters again with the orginal raster x.
> I wrote the following piece of code to perform this task:
>
>
> r=raster(r)
> extent(r)<-extent(xmin,xmax,ymin, ymax)
> e1=extent(xmin,xmax/2,ymin,ymax)
> e2=extent(xmax/2,xmax,ymin,ymax)
> r1=crop(r,e1)
> r1=shift(r1,(-xmax/2))
> r2=crop(r,e2)
> r2=shift(r2,xmax)
> newr=merge(r,r1)
>
> When I try to merge these two rasters first I get the following message:
>
> "Error in compareRaster(x, extent = FALSE, rowcol = FALSE, orig = TRUE,  :
>   different origin"
> Looking at the extent it seems that the crop function is cropping 45 more meters than xmax/2 and that causes the differences in the origin. Actually when I shift r1, the origin is at 45 m, confirming the differences in cropping.  Any idea of how to solve this?
>
>
> Thanks!
>         [[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
>

--
Michael Sumner
Hobart, Australia
e-mail: mdsumner at gmail.com

```