[R-sig-Geo] calculate fraction of a polygon that is intersected by another polygon
satishr
satish.regonda at gmail.com
Fri Dec 23 21:00:35 CET 2011
Robert,
Thanks for all the detailed information. I am able to work around it with
the help of your email and Roger Bivand's reply from archives. For others
benefit, I am putting the final version code here.
Thanks,
Satish
rm(list=ls())
library(maptools);library(raster);library(rgeos);library(rgdal);
grid=readOGR("C:/test","Gaussian_Poylgon_ExtendedCONUS")
climdiv <- readOGR("C:/test","US344ClimateDivisions1928Polys")
# climdiv has several polygons with the same attributes, need to do be
"aggregated" first
climagdiv <- aggregate(climdiv, 'PopupInfo')
iclimdiv=256 # say, we are interested in this polygon
int = gIntersects(climagdiv[iclimdiv,],grid,byid=TRUE)
vec = vector(mode="list", length=dim(int)[2])
for (i in seq(along=vec)) vec[[i]] =
try(gIntersection(climagdiv[iclimdiv,],grid[int[,i],], byid=TRUE))
out = do.call("rbind", vec)
rn = row.names(out)
nrn = do.call("rbind", strsplit(rn, " ")) # nrn[,2] --> grid index
gridindex=as.numeric(nrn[,2])+1 # gridindex, need to add "1" because row
names start from "0"
fracgridarea=sapply(slot(out,"polygons"), slot, "area") # fraction of a
grid in a polygon
# "gridindex" corresponds to grid number whereas "fractgridarea"
corresponds to fraction of a grid in a polygon/climate division
--
View this message in context: http://r-sig-geo.2731867.n2.nabble.com/Re-calculate-fraction-of-a-polygon-that-is-intersected-by-another-polygon-tp7119738p7122457.html
Sent from the R-sig-geo mailing list archive at Nabble.com.
More information about the R-sig-Geo
mailing list