0

Consider two closed curves without hole

curve 1:

 x=c(0.01813766,0.021240708, 0.006101686, -0.01813648, -0.021246640,-0.006100095,0.01813766)
 y=c(-0.01795111,0.002401427, 0.027535572,  0.01795129, -0.002396452, -0.027526064,-0.01795111)

curve 2

x=c(0.006476347,  0.027730840, 0.01291382, -0.006475285, -0.027740463,-0.01291557,0.006476347)
y=c(-0.023862392, -0.008557378, 0.01629330,  0.023863672,  0.008560437, -0.01628554, -0.023862392)

Here is the plot: enter image description here Want to compute the area of the non-overlapping area.

Note that: In our case if we rotate the red ploygon (for example) the area of the non-overlapping area will change unlike in the case where one consider the the difference between area of the two ploygon as symmetric difference.

Question: I am looking for any package/ code in R which can be used to compute the area of the non-overlapping area between the above two polygon and this non overlapping area is sensible to the rotation of any one polygon.

Any helps. Thanks in advance.

Janak
  • 653
  • 7
  • 25
  • 1
    take some popcorn and watch simpson's : http://stackoverflow.com/questions/7868382/calculate-area-between-2-curves – Colonel Beauvel Apr 27 '16 at 14:08
  • @ColonelBeauvel. I looked into the referred link. And found the distance measure which they are considering is different from my distance measure. I am looking for distance which is sensitive to orientation of the the polygons. I clarified this point in my question. – Janak Apr 27 '16 at 15:01

1 Answers1

1

The packages sp and rgeos are great for working with spatial objects in R.

Here's an example:

library(rgeos)
library(sp)

# Polygon 1

poly1 <- readWKT("POLYGON ((0.01813766 -0.01795111, 0.021240708 0.002401427,0.006101686 0.027535572, -0.01813648 0.01795129, -0.021246640 -0.002396452,-0.006100095 -0.027526064,0.01813766 -0.01795111))")


# Polygon 2
poly2 <- readWKT("POLYGON ((0.006476347 -0.023862392,  0.027730840 -0.008557378, 0.01291382 0.01629330, -0.006475285 0.023863672, -0.027740463 0.008560437,-0.01291557 -0.01628554,0.006476347 -0.023862392))")

d <- gSymdifference(poly1, poly2)

> gArea(d)

[1] 0.0003226174

> plot(d, col = "red")

enter image description here

Michael Gao
  • 125
  • 6
  • Great. This is what I looking for. Thank you very much. – Janak Apr 27 '16 at 16:42
  • You're welcome! If you are satisfied with the answer, you can mark it as correct by clicking the green checkmark next to the answer. Happy to help. – Michael Gao Apr 27 '16 at 16:55
  • If we consider a ploygon with 100 vertices then the coordinate of the polygon will be 100\times 2 matrix. Then the argument of the POLYGON function would be a matrix. In this case it is showing an error. How to deal with such a large polygon? – Janak Apr 27 '16 at 17:38
  • I am trying to use the argument of the POLYGON function as a matrix. Is it possible? – Janak Apr 27 '16 at 17:45
  • Could you please suggest me here!! Is it necessary that we have to write the co-ordinates manually as in the example? If yes then it is of no use when we have a polygon with large number of vertices or large number polygon. – Janak Apr 28 '16 at 08:23
  • The function readWKT reads from WKT format. You can use writeWKT from any spatial object in R and then read that back in as a temporary workaround. If you have a matrix of points, you should be able to convert that to a polygon by using the `polygon` function from sp and then use writeWKT on that object. Then read that object back in. – Michael Gao Apr 28 '16 at 19:02