I'm trying to create a composite/average of multiple similarly shaped polygons. I think the first step is most likely to align them via the centroid of each polygon.

Does anybody know of a tool to align polygons in R? I am using spatstat mainly, but can convert the polygons to sp and sf classes.

Here's an example of what I have so far:

test.verts <- list()
test.verts$x <- c(518, 594, 421, 286, 104, 129, 315)
test.verts$y <- c(273, 608, 1202, 1201, 620, 285, 135)

test.verts2 <- list()
test.verts2$x <- c(900, 922, 693, 471, 207, 305, 628)
test.verts2$y <- c(459, 777, 1265, 1267, 615, 325, 135)

test.verts3 <- list()
test.verts3$x <- c(910, 980, 687, 476, 223, 389, 664)
test.verts3$y <- c(343, 863, 1339, 1340, 594, 185, 65)

test.owin <- spatstat::owin(poly = test.verts)
test.owin2 <- spatstat::owin(poly = test.verts2)
test.owin3 <- spatstat::owin(poly = test.verts3)

plot(test.owin2, add = TRUE)
plot(test.owin3, add = TRUE)

Three polygons, similar in shape, with unequal sizes

The figure cuts of some parts of the polygons due to size. The polygons are roughly the same shape, but vary somewhat in size.


You can use centroid.owin to find the centroid of an owin and shift to translate an owin.

Let's put your polys in a list:

> polys = list(test.owin, test.owin2, test.owin3)

And shift them all to have centroid (0,0):

> zpolys = lapply(polys, function(p){ce=centroid.owin(p);shift(p,c(-ce$x, -ce$y))})

> plot(zpolys[[3]])
> plot(zpolys[[2]],add=TRUE)
> plot(zpolys[[1]],add=TRUE)

enter image description here

If you want to shift them somewhere else then adjust the offset to suit. For example you could work out the mean of the centroids, then shift by the individual centroids minus the mean. Or shift everything to (0,0) as above and then shift everything again by (xmean, ymean). I shifted things to (0,0) because its easier to then scale things when centred at (0,0), which I have a hunch is what you might want to do next...

  • Perfect! Thank you! Do you know of any methods for "averaging" the polygons into one polygon that can then be converted to an owin? Nov 5 '19 at 18:10
  • That's a very different question that probably requires a more precise definition of "average" than sticking it in quote marks and hoping! It all depends on your application... Interesting question though, if you can flesh it out into a new post, please do...
    – Spacedman
    Nov 5 '19 at 20:27

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.