Re: [R] calculate area between intersecting polygons
Many thanks for your help! Thanks to you guys I manage to solve my problem in an efficient way All the best J On Tue, Oct 26, 2010 at 11:04 PM, Remko Duursma remkoduur...@gmail.comwrote: I don't know why I forgot that you can do this as well : area.poly(intersect(p1,p2)) ... a bit more straightforward. greetings, Remko -- View this message in context: http://r.789695.n4.nabble.com/calculate-area-between-intersecting-polygons-tp3012980p3014581.html Sent from the R help mailing list archive at Nabble.com. __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.htmlhttp://www.r-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code. [[alternative HTML version deleted]] __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
Re: [R] calculate area between intersecting polygons
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 On Tue, Oct 26, 2010 at 3:38 AM, Remko Duursma remkoduur...@gmail.comwrote: Dear Jonas, if you can write the difference in y-values between your polygons as a function, you can use integrate() to get the area between the polygons. It sounds like perhaps your x-values will not match between the polygons because they come from different sources, so you probably have to do some interpolating (with ?approx). hope that helps, Remko -- View this message in context: http://r.789695.n4.nabble.com/calculate-area-between-intersecting-polygons-tp3012980p3013059.html Sent from the R help mailing list archive at Nabble.com. __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.htmlhttp://www.r-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code. [[alternative HTML version deleted]] __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
Re: [R] calculate area between intersecting polygons
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)) } __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
Re: [R] calculate area between intersecting polygons
Here is a different solution: library(gpclib) p1 - as(poly1, gpc.poly) p2 - as(poly2, gpc.poly) area.poly(p2) + area.poly(p1) - area.poly(union(p1,p2)) I.e., take areas of both polygons and subtract the union (check plot(union(p1,p2)) ) to get the area of the intersection. greetings, Remko -- View this message in context: http://r.789695.n4.nabble.com/calculate-area-between-intersecting-polygons-tp3012980p3013588.html Sent from the R help mailing list archive at Nabble.com. __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
Re: [R] calculate area between intersecting polygons
Dear Jonas, I already had to deal with such an issue. Your can use the joinPolys function from the package PBSmapping, with INT as operation. The maptools package has functions SpatialPolygons2PolySet and PolySet2SpatialPolygons to switch between formats suitable for sp or PBSmapping. Hope this helps. Yves On 26 October 2010 10:18, jonas garcia garcia.jona...@googlemail.comwrote: 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 On Tue, Oct 26, 2010 at 3:38 AM, Remko Duursma remkoduur...@gmail.com wrote: Dear Jonas, if you can write the difference in y-values between your polygons as a function, you can use integrate() to get the area between the polygons. It sounds like perhaps your x-values will not match between the polygons because they come from different sources, so you probably have to do some interpolating (with ?approx). hope that helps, Remko -- View this message in context: http://r.789695.n4.nabble.com/calculate-area-between-intersecting-polygons-tp3012980p3013059.html Sent from the R help mailing list archive at Nabble.com. __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html http://www.r-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code. [[alternative HTML version deleted]] __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code. [[alternative HTML version deleted]] __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
Re: [R] calculate area between intersecting polygons
Remko Duursma remkoduursma at gmail.com writes: Here is a different solution: library(gpclib) p1 - as(poly1, gpc.poly) p2 - as(poly2, gpc.poly) area.poly(p2) + area.poly(p1) - area.poly(union(p1,p2)) I.e., take areas of both polygons and subtract the union (check plot(union(p1,p2)) ) to get the area of the intersection. greetings, Remko Thanks for the hint. I didn't know that the polygon clipping library has been packaged for R, I could have used it several time before. There are too many package, though this time I admit it would have been easy to hit upon it by searching CRAN carefully. Hans Werner Hans Werner __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
Re: [R] calculate area between intersecting polygons
I don't know why I forgot that you can do this as well : area.poly(intersect(p1,p2)) ... a bit more straightforward. greetings, Remko -- View this message in context: http://r.789695.n4.nabble.com/calculate-area-between-intersecting-polygons-tp3012980p3014581.html Sent from the R help mailing list archive at Nabble.com. __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
Re: [R] calculate area between intersecting polygons
Dear Jonas, if you can write the difference in y-values between your polygons as a function, you can use integrate() to get the area between the polygons. It sounds like perhaps your x-values will not match between the polygons because they come from different sources, so you probably have to do some interpolating (with ?approx). hope that helps, Remko -- View this message in context: http://r.789695.n4.nabble.com/calculate-area-between-intersecting-polygons-tp3012980p3013059.html Sent from the R help mailing list archive at Nabble.com. __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.