Satish,

I cannot help you much as you provide no data.

> The function 'extract' with 'weights=TRUE' was able to calculate
> fractions but it has following issues: (a) the function did not consider a
> grid(s) although there is a overlap with a climate division,

Perhaps because of incorrect rasterizing. Also, overlaps < 1% of the grid
cell are ignored

> and (b)
> fractional amounts are changed when I changed the rectangular grid size.

of course. Isn't that what you would expect.

It seems that your script can be reduced a lot to:

spatGP=readShapeSpatial("Gaussian_Poylgon_ExtendedCONUS.shp")
spatras <- raster(ncol=180, nrow=180)
extent(spatras)<- extent(spatGP);
spatras <- rasterize(spatGP, spatras)
cddata=readShapeSpatial("divisions.shp")
ip=256   # say, I am interested in this polygon
weights.tmp = extract(spatras, cddata[ip,], weights=TRUE)

rasterizing the rectangles is somewhat awkward. Are you sure that the
raster and polygons align perfectly?

Anyway, the more obvious approach, as you have two sets of polygons, would
be to use rgeos (gIntersection) and then something like sapply(x@polygons,
function(x) slot(x, 'area')) to get the areas.

Robert





On Thu, Dec 22, 2011 at 11:06 AM, satishr <satish.regonda@gmail.com> wrote:

> Hi,
>   I need to calculate the fraction of a polygon that is intersected by
> another polygon. details of the problem: I have data in two shape files;
> the
> first file corresponds to a rectangular grid with uniform intervals whereas
> the second file corresponds to US climate divisions, which are polygons.
> The
> polygons representing climate divisions vary in size, some times these
> climate divisions/polygons are so small such that they fit in one
> rectangular grid and some times require more than 20 rectangular grids.
> What
> I need is: (a) identify rectangular grids that intersect with a polygon and
> (b) calculate the fraction of the intersection between the rectangular grid
> and polygon.
>    The function 'extract' with 'weights=TRUE' was able to calculate
> fractions but it has following issues: (a) the function did not consider a
> grid(s) although there is a overlap with a climate division, and (b)
> fractional amounts are changed when I changed the rectangular grid size.
> Here is my code. Any help is greatly appreciated.
> Thanks,
> Satish
>
> #          Read two shape files, rasterize rectangular grid and use it with
> 'extract'
>
> #          First shape file, rectangular grid;
>
>
>
>            spatGP=readShapeSpatial("Gaussian_Poylgon_ExtendedCONUS.shp")
>
>
>
>            spatGP1=as(spatGP,"SpatialPolygons")
>
>
>
>            spatGP2=slot(spatGP1,"polygons")
>
>
>
> #          rasterization
>
>
>
>            spatras0 <- raster(ncol=180, nrow=180)
>
>
>
>            extent(spatras0)<- extent(spatGP);
>
>
>
>            spatras <- rasterize(spatGP, spatras0)
>
>
>
>
>
> #          second shapefile, polygons, i.e., climate divisions
>
>
>
>            cddata=readShapeSpatial("divisions.shp")
>
>
>
>            spcddata=as(cddata,"SpatialPolygons")
>
>
>
>            regionscddata=slot(spcddata,"polygons")
>
>
>
>            ipolygon=256   # say, I am interested in this polygon
>
>
>
>            sub.tmp = slot(regionscddata[[ipolygon]],'Polygons')
>
>
>
> #          create a polygon
>
>
>
>            poly.tmp=cbind(sub.tmp[[1]]@coords[,1],sub.tmp[[1]]@coords[,2])
>
>
>
>            polys = SpatialPolygons(list(Polygons(list(Polygon(poly.tmp)),
> 1)))
>
>
>
>            weights.tmp = extract(spatras, polys, weights=TRUE)
>
>
>
> ‘weights.tmp’ is a matrix – provides information of cells that are covered
> by the polygon, i.e., first column cell positions and second column
> fraction
> of cell.
>
>
> http://r-sig-geo.2731867.n2.nabble.com/file/n7119672/USClimateDivsions_RectangularGrid.jpeg
>
> --
> View this message in context:
> http://r-sig-geo.2731867.n2.nabble.com/calculate-fraction-of-a-polygon-that-is-intersected-by-another-polygon-tp7119672p7119672.html
> Sent from the R-sig-geo mailing list archive at Nabble.com.
>
> _______________________________________________
> R-sig-Geo mailing list
> R-sig-Geo@r-project.org
> https://stat.ethz.ch/mailman/listinfo/r-sig-geo
>

	[[alternative HTML version deleted]]

