[R] calculate area between intersecting polygons
Hans W Borchers
hwborchers at googlemail.com
Tue Oct 26 12:29:45 CEST 2010
jonas garcia <garcia.jonas80 <at> googlemail.com> writes:
>
> Thanks for your reply,
>
> My main issue is that I don't have any equations to generate the data, just
> a bunch of points, each corresponding to a polygon.
>
> I was looking in package "sp" and there is a function to calculate areas (
> areapl()), but not for intersecting polygons. Is there any other package
> that does this?
> Thanks
There is a formula for calculating the area of a polygon from its points,
A = 1/2 * sum_{i=1}^{n}{x_i*y_{i+1} - x_{i+1}*y_i},
where (x_{n+1}, y_{n+1}) = (x_0, y_0),
n the number of points (non-closed),
i.e.
polygonArea(poly1) # 6.39605
polygonArea(poly2) # 9.35967
You will need to identify the intersection points between the polygons
and merge the appropriate parts of the polygon lines.
Hans Werner
----
polygonArea <- function(P) {
n <- nrow(P)
x <- P[, 1]; y <- P[, 2]
p1 <- sum(x[1:(n-1)]*y[2:n]) + x[n]*y[1]
p2 <- sum(x[2:n]*y[1:(n-1)]) + x[1]*y[n]
return(0.5*(p1-p2))
}
----
More information about the R-help
mailing list