I have vectorized raster as in the image. This is actually 1419 polygons in SpatialPolygons class object.


I'm looking for a method which would allow me to extract the one most external polygon (boundary shape). I've found concave method which works for 95% of cases but not in this particular one. As follows the code:

sfn <- sf::st_as_sf(the_polygon)
sfu = sf::st_union(sfn)
gpts = sf::st_coordinates(sfu)
gmat <- matrix(c(gpts[,1], gpts[,2]), ncol = 2)

I can adjust parameters:

cm = concaveman::concaveman(gmat, 0.5, 1)
chp = SpatialPolygons(list(Polygons(list(Polygon(cm)), ID=n)))

this gives such vector: example2 but choosing the first parameter as 0.1 takes: example3

So there's no optimal shape, I've tested values between those 2 and further as well. The second parameter doesn't make much - it just smoothes the lines.

I'd like to ask from your experience what kind of approach I'd take? For example, if I had a function which fills the outside area, then selects "negative" and makes borderline and that border would be good approximate of what I need. Notice it's a vector(s). Have anyone some idea? In terms of fill the vectors area what libraries I'd choose?


Incredible things happen: I've fit this almost ideally using concaveman(gmat, 0.01, 2.2) - just tested with "bruteforce plot". So, in this case, answer been partially found.


You can try the chull function, which calculates the convex hull of the coordinates of your polygon(s). Try

cv.id <- chull(gmat)
cv.id <- c(cv.id, cv.id[1])
plot(gmat, cex = 0)
lines(gmat[cv.id, ])
| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.