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

Edzer Pebesma edzer.pebesma at uni-muenster.de
Sat Nov 19 15:47:16 CET 2011


Please try, at the end of your script:

fullgrid(grid)=FALSE
sel.grid <-grid[!is.na(clip),]
points(sel.grid, col='blue', pch=3)


On 11/19/2011 03:38 PM, ASANTOS wrote:
> 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,
> 

-- 
Edzer Pebesma
Institute for Geoinformatics (ifgi), University of Münster
Weseler Straße 253, 48151 Münster, Germany. Phone: +49 251
8333081, Fax: +49 251 8339763  http://ifgi.uni-muenster.de
http://www.52north.org/geostatistics      e.pebesma at wwu.de



More information about the R-sig-Geo mailing list