[R-sig-Geo] using lapply() with Spatial Objects

Remi Genevest rgenevest at free.fr
Mon Aug 11 19:00:04 CEST 2014


I have a list of coordinates that each corresponds to the center of polygon.
My purpose is to create the polygons using lapply() and plot them on the
same device.

And actually I have 2 questions : 
  - How can I create some polygons using lapply() with a
SpatialPointsDataFrame ?
  - How can I plot this list of polygons on the same device ? (still using
lapply())

Here a little example of I want to do (based on a simple dataframe) : 

## Setting a dataframe with coordinates of the centers of the polygons I
want to plot : 
df<-data.frame(Lat=c(3,4),Lon=2,value=0.1)

## Using lists for creating polygons : 
cc<-list()
disc<-list()
for (i in 1:nrow(df)) 
{
  cc[[i]]<-cbind(df[i,1],df[i,2])  
  disc[[i]]<-disc(radius=0.1,centre=cc[[i]])
}

## plot the polygons
lapply(disc,FUN=plot)

This works, but do not plot the polygones on the same device.
So I thought about doing this, without success :

lapply(disc,function(x) {plot(x,add=TRUE)})

Error in (function (x, y = NULL, density = NULL, angle = 45, border = NULL, 
: 
plot.new has not been called yet  

So, what would you do to simplify this code and make it possible to use the
'add' parameters of the plot() function in a lapply() ?

As I am not used to handle apply() functions, I do appreciate your help...
Just to let you know, my original idea was to do something like this,
considering 'spdf' as a SpatialPointsDataframe :

lapply(spdf, function(x) {disc(radius=0.1,centre=coordinates(x))})
Error in as.list.default(X) : 
  no method for coercing this S4 class to a vector

Thanks for help



--
View this message in context: http://r-sig-geo.2731867.n2.nabble.com/using-lapply-with-Spatial-Objects-tp7586911.html
Sent from the R-sig-geo mailing list archive at Nabble.com.



More information about the R-sig-Geo mailing list