# Aligning Multiple Polygons (Based on centroid possibly) [Spatstat, sf, sp]

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.owin)
`````` 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[])
> 