[R-sig-Geo] Problem with overlay in sp package

ASANTOS alexandresantosbr at yahoo.com.br
Sat Nov 19 15:38:33 CET 2011


Dear colleges,

         I need a help for selecting a grid inside the area with the 
overlay() function, the idea is create a regular grid for all the area 
and before select only the grid inside the sub area, my script for this are:

require(splancs)
require(sp)
#Create random points
x <- runif(n=500,min=0, max=20)
y <- runif(n=500,min=0, max=20)
xy<-cbind(x,y)
plot(x=x,y=y,axes=F, xlab="", ylab="",
      xlim=c(-1,20),ylim=c(0,21))
pt0 <- c(0.5,0.5)##First point of grid
##Create sub area
limx<-c(2.5,2.5,18,18,2.5)
limy<-c(2.5,18,18,2.5,2.5)
lim=cbind(limx,limy)
lim<-as.matrix(lim)
##Convert in spatial objects of sp package
#Random points
pontos <- SpatialPoints(cbind(x,y))
pontos<- SpatialPoints(list(x,y))
pontos<- SpatialPoints(data.frame(x,y))
#Create the grid
grid0= GridTopology(c(0.5,0.5), c(1,1), c(20,20))
grid<-SpatialGrid(grid = grid0)
##Sub area
limite=SpatialPolygons(list(Polygons(list(Polygon(lim)),"lim")))
##For view
plot(limite)#Sub area
points(pontos)#Random points
points(grid,col='red')#Grid
clip<-overlay(grid,limite)##Select the grid inside the sub area
sel.grid <-grid[!is.na(clip),]##Removing Na's that represents the grid 
outside the sub area

But in this point I have a problem, there are any solution or other 
approach for my problem,
Thanks,

-- 
Alexandre DOS SANTOS
Engenheiro Florestal, Msc.
Laboratório de Entomologia Florestal
Departamento de Entomologia
Universidade Federal de Lavras
Caixa Postal 3037
37200-000 - Lavras - Minas Gerais - Brasil
Tel: +55 35 92230304



More information about the R-sig-Geo mailing list