# [R] SPATIAL QUESTION: HOW TO MAKE POLYGONS AROUND CLUSTERS OF POINTS AND EXTRACT AREAS AND COORDINATES OF THESE POLYGONS?

Rolf Turner rolf.turner at xtra.co.nz
Tue Feb 7 00:51:37 CET 2012

```On 04/02/12 03:59, Bjørn Økland wrote:
> Imagine that I have a large number of points (given by coordinates x and y) that vary in density per space. For the purpose of demonstration it could be generated like this: s<- data.frame(x=runif(10000,0,900),y=runif(10000,0,900)); plot(s)
>
> I want to create polygons around the points where point density is greater than a selected threshold (for example, by using krieging or equivalent method). For these polygons, I want to have the centre coordinates and the size of the area for further use in analyses.
>
> I would be very grateful if I could be shown the R packages and functions I should use to accomplish this, and even some outline of the code. Is it possible?

You can do this using the spatstat package.  E.g.:

require(spatstat)
set.seed(42)
X <- rpoisspp(100)
Y <- density(X)
Z <- levelset(Y,130,">")
U <- connected(Z)
V <- tiles(tess(image=U))
lapply(V,centroid.owin)
lapply(V,area.owin)

You should read the help on the functions invoked above.  It might be
illuminating to do some plotting along the way (of X, Y, Z, U, and V).

cheers,

Rolf Turner

```