Thanks for the response Oscar and Pascal...

The idea of Pascal I had tried already and it doesn't work. At the end, I
had found out a way out using this:

    print(levelplot(wstr$r,col.regions=wstr$clrs,margin=FALSE,main=paste('t
= ',FootPrint), colorkey = T, scales=list(draw=FALSE),
              panel=function(...){
                panel.levelplot(...)
                panel.polygon(wstr$spbox,lwd=2)
                panel.polygon(lncoords,lwd=2, border='red')}))


Oscar: I suppose this is your third function you had proposed... the others
still do not seem to work... I must admit it is not too clear to me why,
but at least the last one works...

greg





On 21 March 2014 12:45, Oscar Perpiñan <oscar.perpinan@upm.es> wrote:

> Hello,
>
>
> The key point is that "the evaluation used in 'layer' is non-standard,
> and can be confusing at first: you typically refer to variables as if
> inside the panel function ('x', 'y', etc); you can usually refer to
> objects which exist in the global environment (workspace), but it is
> safer to pass them in by name in the 'data' argument to 'layer'. (And
> this should not to be confused with the 'data' argument to the
> original 'xyplot'.)"
>
> Try these three examples:
>
> library(rasterVis)
>
> r <- raster()
> r <- init(r, runif)
>
> foo1 <- function(r){
>     rBox <- as(extent(r), 'SpatialPolygons')
>     levelplot(r) +
>         layer(sp.polygons(rBox, fill='black', alpha=0.2),
>               data=list(rBox=rBox))
>     }
>
> foo2 <- function(r){
>     levelplot(r) +
>         layer({
>             rBox <- as(extent(r), 'SpatialPolygons')
>             sp.polygons(rBox, fill='black', alpha=0.2)
>         })
> }
>
> foo3 <- function(r){
>     rBox <- as(extent(r), 'SpatialPolygons')
>     levelplot(r, panel=function(...){
>         panel.levelplot.raster(...)
>         sp.polygons(rBox, fill='black', alpha=0.2)
>     })
>     }
>
>
> This same problem was the subject of a recent discussion in this post:
>
> http://procomun.wordpress.com/2013/04/24/stamen-maps-with-spplot/comment-page-1/#comment-881
> .
> There are more examples of using layer inside a function in this gist:
> https://gist.github.com/oscarperpinan/7482848
>
> Best,
>
> Oscar.
> -----------------------------------------------------------------
> Oscar Perpiñán Lamigueiro
> Dpto. Ingeniería Eléctrica (ETSIDI-UPM)
> Grupo de Sistemas Fotovoltaicos (IES-UPM)
> URL: http://oscarperpinan.github.io
> Twitter: @oscarperpinan
>
>
> 2014-03-20 22:52 GMT+01:00 Gregory Duveiller <gduveiller@gmail.com>:
> > Dear All...
> >
> > I am trying to make figures overlaying raster and vectors using the
> package rasterVis and with the functions levelplot() + layer(). All works
> well except when I am trying to embed those lines in a function. When I do
> so, only the raster images come out, not the overlaid layers. Anyone knows
> what I need to do? The function I have written looks like this:
> >
> >  makeFootprint=function(wstr,snr,FootPrint,flt,phi,scl,shf){
> >
> >
> elps=scl*ellipse(flt)%*%matrix(c(cos(phi),-sin(phi),sin(phi),cos(phi)),nrow=2)
> >
> elps=elps+c(t(rep(shf[1]+wstr$cellctr$x,100)),t(rep(shf[2]+wstr$cellctr$y,100)))
> >     lncoords=data.frame(x=elps[,1],y=elps[,2])
> >
> footprint=SpatialLines(list(Lines(list(Line(lncoords)),ID='pix')),snr@crs)
> >
> >
> pdf(file=paste(fpath,siteName,'_window',wstr$winID,'_footprint',FootPrint,'.pdf',sep=""),width=4,height=4)
> >
> print(levelplot(wstr$r,col.regions=wstr$clrs,margin=FALSE,main=paste('t =
> ',FootPrint), colorkey = FALSE, scales=list(draw=FALSE)) +
> >       layer(sp.lines(wstr$spbox, lwd=2, col='black')) +
> >       layer(sp.lines(footprint, lwd=2, col='red')))
> >     dev.off()
> >   }
> >
> > The resulting PDF shows the desired raster background but a text
> replaces the 'layers'... mentioning "Error using packet 1 object 'wstr' not
> found". But the object in question is there correctly (it is actually a
> list with data used also to do the levelplot, which is rendered correctly).
> >
> > Any ideas?
> >
> > Is there perhaps a way to include the layers within the levelplot
> function directly (and avoid 'adding' them?)
> >
> >
> > sessionInfo()
> > R version 3.0.2 (2013-09-25)
> > Platform: x86_64-apple-darwin10.8.0 (64-bit)
> >
> > locale:
> > [1] C
> >
> > attached base packages:
> > [1] grid      stats     graphics  grDevices utils     datasets  methods
> > [8] base
> >
> > other attached packages:
> > [1] ellipse_0.3-8       rasterVis_0.27      hexbin_1.26.3
> > [4] latticeExtra_0.6-26 lattice_0.20-27     raster_2.2-31
> > [7] rgdal_0.8-16        sp_1.0-14           RColorBrewer_1.0-5
> >
> > loaded via a namespace (and not attached):
> > [1] zoo_1.7-11
> >
> >
> >
> > Thanks
> >
> > Gregory
> > _______________________________________________
> > R-sig-Geo mailing list
> > R-sig-Geo@r-project.org
> > https://stat.ethz.ch/mailman/listinfo/r-sig-geo
>

	[[alternative HTML version deleted]]

