1

I'm trying to merge bunch of rasters I created to represent a lettuce farm. I don't want to go into detail, but I needed to create an individual raster for each produce bed and furrow (the area between two produce beds). So, my farm has 60 produce bed (represented by 60 raster layers) as well as 61 furrows (representing by 61 raster layers) that are placed side-by-side, creating a rectangular farm with a size (65 x 65m). Each produce bed has a size of 0.6 x 65m, while each furrow has a size of 0.4 x 65m. I'm using different cell sizes for produce beds versus furrow rater layers (i.e., 0.2m, and 0.3m for produce bed and furrow, respectively).

Now, I'm doing some fate-transport modeling and save soil contamination in each grid cells for these layers in time. At the end, I'm trying to merge these layers next to each other and create the actual farm (65x 65m square farm).

Here is what I do. Let's say I try to merge the produce bed layers:

  1. For each layer, I first adjust its extent to the overall farm extent (i.e., [0,65,0,65]) using the "extent" function.
  2. I create a loop, and merge these layers that now have the same extent together using a tolerance level of 1.0.

Now, when I try to plot the final merged layer, I see a figure that doesn't make sense. Some of the produce beds are out of shape (see the figure below). The actual layer, doesnt seem to have this issue because when I use xyFromCells and then plot the xy coordinates, seems like the rows are properly placed. I wonder if this is the "plot" issue rather than how I'm merging the layers?

enter image description here

Here is the simplified code:

# create raster layers
myRaster<-list()
for (i in 1:65){
  x.min<-(i-1)
  x.max<-x.min+0.4
  y.min<-0
  y.max<-65
  n.rows<-round(65/0.2)+1
  n.cols<-3
  myRaster[[i]]<-raster(nrow=n.rows,ncol=n.cols,xmn=x.min,xmx=x.max,ymn=y.min,ymx=y.max)
  values(myRaster[[i]])<-runif(ncell(myRaster[[i]]),0,100)
  proj4string(myRaster[[i]])<-"+proj=utm +zone=36 +ellps=WGS84 +units=m +no_defs"
}


# merge raster layers
farmExtent<-extent(0,65,0,65)
merged.layers<-myRaster[[1]]
merged.layers<-extend(merged.layers,farmExtent)
for (i in 2:65){
  temp.layer<-extend(myRaster[[i]],farmExtent)
  x<-merge(x=merged.layers,y=temp.layer,tolerance=1.0)
  merged.layers<-x
}
plot(merged.layers)
Adriaan
  • 17,741
  • 7
  • 42
  • 75
Amir M.
  • 71
  • 7

0 Answers0