[R-sig-Geo] Intersection of Polygons (sp)

Edzer Pebesma edzer.pebesma at uni-muenster.de
Wed May 9 15:20:42 CEST 2012



On 05/09/2012 03:07 PM, Matteo Mattiuzzi wrote:
> Dear List,
>  
> I have a table with corner coordinates:
>  
> download.file("http://landweb.nascom.nasa.gov/developers/sn_tiles/sn_gring_10deg.txt", dest=paste(tempdir(),"/sn_gring_10deg.txt",sep=""))
> x <- read.table(paste(tempdir(),"/sn_gring_10deg.txt",sep=""),skip=7,nrows=648)
> colnames(x) <- c("iv","ih","lllon","lllat","ullon","ullat","urlon","urlat","lrlon","lrlat")
> x <- x[x$lllon != -999,] # remove not valid tiles
> x[1:5,]
>  
> # and I wish to create polygons for each of the rows of "x". 
> require(sp)
> tilepolys <- list()
> for (u in 1:nrow(x)){
> attribute <- paste("h", sprintf("%02d", x$ih[u]), "v", sprintf("%02d", x$iv[u]),sep="")
> tilepolys[[u]] <- Polygons(list(Polygon(cbind(c(x$ullon[u],x$urlon[u],x$lrlon[u],x$lllon[u],x$ullon[u]),c(x$ullat[u],x$urlat[u],x$lrlat[u],x$lllat[u],x$ullat[u])),hole=FALSE)),attribute)
> }
> sr=SpatialPolygons(tilepolys)
>  
> plot(sr)
> # some polygons are crossing the lon +-180 limit and they should close the on the right side (crossing that border and not the globe). I don't understand if this happens here? And if there is a way to control it?
> #######################
> # I have a second polygon:
> po  <- Polygon(cbind(c(9.533616,17.16639,17.16639,9.533616,9.533616),c(49.01889,49.01889,46.38557,46.38557,49.01889)),hole=FALSE)
> pos <- Polygons(list(po),"austria")
> spos <- SpatialPolygons(list(pos))
>  
> plot(spos)
>  
> # Now I want to select (or better get the attribute of) every polygon in "sr" that is inside or touches the polygon "spos"?
>  
sr does not have attributes.

> I hope this is possible to do with "sp", in order to avoid further dependencies.

not without rgeos:

library(rgeos)
x = sr[spos,] # you could omit the , as sr has no attributes
plot(x)

>  
> Thanks a lot, Matteo
>  
>  
>  
>  
>  
>  
> 
> 	[[alternative HTML version deleted]]
> 
> _______________________________________________
> R-sig-Geo mailing list
> R-sig-Geo at r-project.org
> https://stat.ethz.ch/mailman/listinfo/r-sig-geo

-- 
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