[R-sig-Geo] Minimum bounding circle from cluster of points

Tina Cormier TinaACormier at gmail.com
Mon Jul 11 17:26:25 CEST 2016


Wow! I'm humbled that you all took the time to help me out! You had great
suggestions and sent along some very handy functions. I have the idea now
of what I need to do - find the longest distance between two points in the
cluster, the midpoint of that line is the center of the circle, and there
are numerous ways to create the circle, but once you have the radius, you
can just buffer the center point. Fantastically simple. Someone also
suggested using PostGIS (ST_MinimumBoundingCircle), which I will definitely
try. I have a postgreSQL db that I use for some simple stuff, and I'm not a
guru by any means, but this will help me gain a little more experience.

Thank you again, and I can't wait to pay if forward the next time someone
asks about a topic I know well!

Cheers,
Tina
***********************************************************************
In SUMMARY, functions that solve this problem are listed below:

1. Using a *3-liner* from Robert Hijmans, using optimization:
library(raster)
library(rgeos)

set.seed(7)
n <- 4
xy <- cbind(runif(n), runif(n))




*f <- function(p) { max(pointDistance(rbind(p), xy, lonlat=FALSE)) }p <-
optim(colMeans(xy), f)cc <- buffer(SpatialPoints(rbind(p$par)),
width=p$value, quadsegs=45)*
plot(cc)
points(xy, pch=20, cex=1.5)
points(rbind(p$par), pch='*', col='red', cex=3)
lines(rbind(p$par, c(p$par[1]+p$value, p$par[2])), col='blue', lwd=2, lty=2)
text(p$par[1]+0.5*p$value, p$par[2], paste("radius =", round(p$value,
2)), pos=3, cex=.85)
text(p$par[1], p$par[2], labels=paste0("(",paste(round(p$par, 2),
collapse=", "), ")"), pos=1, cex=.75)
***********************************************************************

2. A function from Adrian Baddeley using spatstat
library(spatstat)

circumcircle <- function(x, ...) {   UseMethod("circumcircle") }

circumcircle.owin <- function(x, ...) {
  d2 <- fardist(x, ..., squared=TRUE)
  z <- where.min(d2)
  r <- sqrt(min(d2))
  w <- disc(centre=z, radius=r)
  return(w)
}

circumcircle.ppp <- function(x, ...) {
  circumcircle(convexhull(x))
}
***********************************************************************

3. A handy link that walks through the process in a bit longer way around,
but still works:
http://www.uni-kiel.de/psychologie/rexrepos/posts/diagBounding.html


On Fri, Jul 8, 2016 at 10:31 AM, Tina Cormier <TinaACormier at gmail.com>
wrote:

> Hi all,
>
> Looking for help aggregating some field data subplots to the plot level
> (data currently in shapefile format). I have clusters of 4 points (4
> subplots = 1 plot). I'd like to create a circle around each cluster that is
> the smallest circle that would encompass all 4 points. Sort of like a
> convex hull, but a circle (convex hull, in this case, would give me a
> triangle because of the layout of the subplots). I am familiar with (and
> use frequently) the typical geo packages in R - with rgdal, sp, raster, and
> maptools being my most frequent flyers. Is there a function I'm missing in
> these (or some other) packages that might be able to help me out? In the
> attribute table, I have a unique ID for each plot/cluster. So for each plot
> ID (in this case, consisting of 4 subplots), I'd like to build a circle
> around all of the subplots.
>
> > str(pts)
> Formal class 'SpatialPointsDataFrame' [package "sp"] with 5 slots
>   ..@ data       :'data.frame': 160 obs. of  12 variables:
>   .. ..$ Plot      : num [1:160] 1 1 1 1 2 2 2 2 3 3 ...
>   .. ..$ Subplot   : num [1:160] 1 2 3 4 1 2 3 4 1 2 ...
>
> I should also mention that it's not always 4 points (subplots), and they
> aren't always covering the same size area on the ground, so buffering by a
> constant distance isn't the answer. I have subset out 3 plots (so 12
> subplots) into a test file here:
> https://dl.dropboxusercontent.com/u/72421241/test_subplotsToPlots.zip
>
> I have R version 3.3.0. I know it's customary to include code for what
> I've tried, buuuut, that would be a blank canvas at this point. Googling
> hasn't really been fruitful for this issue, so I thought you folks might
> have some good ideas!
>
> Thanks,
> Tina
>

	[[alternative HTML version deleted]]



More information about the R-sig-Geo mailing list