1

I am looking to do the following:

For the image below (see 'data' below for a example of x y points), remove the line linking the two polygons Adapt the code so that if the shape is nearly complete, fill in the gap. Let’s say ‘nearly complete’ means that the distance between the first and last point is less than 5% of the total length of the line: Calculate the area of the polygons Thanks very much for suggestions.

 coor1 <- R_data[1,8]


#Extract x and y coordinates from annonations 
x1 <- str_extract_all(coor1,'([x])[\\\":]+([0-9\\.]+)')
y1 <- str_extract_all(coor1,'([y])[\\\":]+([0-9\\.]+)')

#clean up coordinates 
x1simp <- sapply(x1, FUN = function(x1){substring(x1, regexpr('x',x1) + 1, nchar(x1))}, USE.NAMES=FALSE)  
x1simp2 <- sapply(x1simp, FUN = function(x1simp){substring(x1simp, regexpr(':',x1simp) + 1, nchar(x1simp))}, USE.NAMES=FALSE)

y1simp <- sapply(y1, FUN = function(y1){substring(y1, regexpr('y',y1) + 1, nchar(y1))}, USE.NAMES=FALSE)  
y1simp2 <- sapply(y1simp, FUN = function(y1simp){substring(y1simp, regexpr(':',y1simp) + 1, nchar(y1))}, USE.NAMES=FALSE)  

#convert to numeric
x1_num <- as.numeric(x1simp2)
y1_num <- as.numeric(y1simp2)


#data into tibble format 
data <- tibble(x = x1_num, y = y1_num)


data
    # A tibble: 16 x 2
           x     y
       <dbl> <dbl>
     1  709.  123.
     2  729.  263.
     3  747.  325.
     4  705.  433.
     5  658.  527 
     6  610.  602.
     7  622.  642.
     8  707.  639.
     9  707.  639.
    10  764.  615.
    11  926.  496.
    12  935.  340.
    13  875.  276.
    14  848.  220.
    15  817.  176.
    16  709.  123.][1]][1]

#plot polygon
plot(x1_num,y1_num,type="n")
polygon(x1_num,y1_num)

enter image description here

  • [This answer](https://stackoverflow.com/a/58772823/903061) proposes `splancs::areapl` for finding areas of polygons. So the question then becomes how to tell the difference between the two polygons... might want to edit the question to focus on that, and also talk about any approaches you've thought about for that. – Gregor Thomas May 28 '20 at 15:25

0 Answers0