[R-sig-Geo] calculate fraction of a polygon that is intersected by another polygon

satishr satish.regonda at gmail.com
Fri Dec 23 21:04:44 CET 2011


Robert, Thanks for all the detailed information, it was very helpful and am
able to find solutions. I update the thread with my final version of code.

Thanks,
Satish


On Thu, Dec 22, 2011 at 9:44 PM, Robert Hijmans [via R-sig-geo] <
ml-node+s2731867n7120634h34 at n2.nabble.com> wrote:

> Satish,
>
> Here are some suggestions:
>
> library(rgeos)
> library(raster)
> # you need the latests versions of the above packages
> library(rgdal)
>
> grid <- readOGR("c:/satish", "Gaussian_Poylgon_ExtendedCONUS")
> clim <- readOGR("c:/satish", "US344ClimateDivisions")
>
> # climag has several polygons with the same attributes
> # you should probably "aggregate" first
> climag <- aggregate(clim, 'Name')
>
> #rather than creating a polygon grid, I would create a raster grid
> # raster(xmn=, , , , ) etc.
> # for now this recreates your polygon grid as a raster
> r <- raster(grid)
> dim(r) <- c(38, 92)
> r[] <- 1:ncell(r)
>
> ex <- extract(r, climag, weights=TRUE)
> ex[1]
> sapply(ex, nrow)  # each multi-polygon has at least one cell
>
> # you would be able to compute area were it not that this is lon/lat data.
> # either first project your data, or use a function like 'area' to
> determine the area of each grid cell
> # and then take the fractions from "ex"
>
> #This fails
> x <- gIntersection(grid, climag, byid=TRUE)
>
> # but you can do this for most polygons
>
> x <- gIntersection(grid, climag[1,])
> plot(x)
> row.names(x)
> sapply(x at polygons, function(x) slot(x, 'area'))
>
> # and that is more precise than the raster method
>
> Hope this helps, Robert
>
> On Thu, Dec 22, 2011 at 1:54 PM, satishr <[hidden email]<http://user/SendEmail.jtp?type=node&node=7120634&i=0>>
> wrote:
>
> > Robert,
> >   Thanks for the information. I am also not sure whether rasterizing a
> > rectangular grid is good or not. I tried 'gIntersection' but got an
> error.
> > Anyway, here are the data sets. As you see the climate division file
> name
> > changed, so you may want to change the name in the code.
> > Thanks,
> > Satish
> >
> > 1. Rectangular Gaussian Grid
> >
> >
> http://r-sig-geo.2731867.n2.nabble.com/file/n7120086/GaussianPolygonExtendedCONUS.zip
> > GaussianPolygonExtendedCONUS.zip
> >
> >
> > 2. US climate divisions
> >
> >
> >
> http://r-sig-geo.2731867.n2.nabble.com/file/n7120086/US344ClimateDivisions.zip
> > US344ClimateDivisions.zip
> >
> >
> >
> > --
> > 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-tp7119738p7120086.html
> > Sent from the R-sig-geo mailing list archive at Nabble.com.
> >
> > _______________________________________________
> > R-sig-Geo mailing list
> > [hidden email] <http://user/SendEmail.jtp?type=node&node=7120634&i=1>
> > https://stat.ethz.ch/mailman/listinfo/r-sig-geo
> >
>
>         [[alternative HTML version deleted]]
>
> _______________________________________________
> R-sig-Geo mailing list
> [hidden email] <http://user/SendEmail.jtp?type=node&node=7120634&i=2>
> https://stat.ethz.ch/mailman/listinfo/r-sig-geo
>
>
> ------------------------------
>  If you reply to this email, your message will be added to the discussion
> below:
>
> http://r-sig-geo.2731867.n2.nabble.com/Re-calculate-fraction-of-a-polygon-that-is-intersected-by-another-polygon-tp7119738p7120634.html
>  To unsubscribe from Re: calculate fraction of a polygon that is
> intersected by another polygon, click here<http://r-sig-geo.2731867.n2.nabble.com/template/NamlServlet.jtp?macro=unsubscribe_by_code&node=7119738&code=c2F0aXNoLnJlZ29uZGFAZ21haWwuY29tfDcxMTk3Mzh8LTEwNDk4OTYwNDc=>
> .
> NAML<http://r-sig-geo.2731867.n2.nabble.com/template/NamlServlet.jtp?macro=macro_viewer&id=instant_html%21nabble%3Aemail.naml&base=nabble.naml.namespaces.BasicNamespace-nabble.view.web.template.NabbleNamespace-nabble.naml.namespaces.BasicNamespace-nabble.view.web.template.NabbleNamespace-nabble.view.web.template.InstantMailNamespace&breadcrumbs=instant+emails%21nabble%3Aemail.naml-instant_emails%21nabble%3Aemail.naml-send_instant_email%21nabble%3Aemail.naml>
>


--
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-tp7119738p7122468.html
Sent from the R-sig-geo mailing list archive at Nabble.com.



More information about the R-sig-Geo mailing list