[R-sig-Geo] build polygons from ellipses, then sample points within the polygons
Anthony Fischbach
afischbach at usgs.gov
Wed Mar 19 21:43:39 CET 2014
Thank you Rolf.
Below is how I built it in the end.
I had to take measures to ensure that the ellipse polygon closed back on
itself.
Roger: Would it be appropriate to include this function in sp or an
associated contributed package.
It seems that building spatialPolygons from ellipse specifications would be
a task required of many sp users.
## modified code from Rolf Turner
require(sp)
require(spatstat)
ellipseSP <- function (x0, y0, a, b, phi=pi/2, npts = 100, ...) { ##
Returns SpatialPolygons ellipse
## phi defaults to 90 degrees
## whereby a lies in the N-S and
## b in the E-W directions
theta <- seq(0, 2 * pi, length = npts)
xh <- a * cos(theta)
yh <- b * sin(theta)
co <- cos(phi)
si <- sin(phi)
x <- x0 + co*xh - si*yh
x[npts]<-x[1] ## force closure of polygon
y <- x0 + si*xh + co*yh
y[npts]<-y[1] ## force closure of polygon
return(SpatialPolygons(list(Polygons(list(with(list(x = x, y =
y),Polygon(cbind(x,y)))),1))) )
}
## Example plot
eSP <- ellipseSP(x0=0, y0=0, a=8, b=4)
plot(eSP)
##use sp::spsample to randomly select points from within the ellipse
points(spsample(eSP, n=100, type="random"), col='red', pch=19, cex=.25)
#
-----
Tony Fischbach, Wildlife Biologist
Walrus Research Program
Alaska Science Center
U.S. Geological Survey
4210 University Drive
Anchorage, AK 99508-4650
AFischbach at usgs.gov
http://alaska.usgs.gov/science/biology/walrus
--
View this message in context: http://r-sig-geo.2731867.n2.nabble.com/build-polygons-from-ellipses-then-sample-points-within-the-polygons-tp7585922p7585978.html
Sent from the R-sig-geo mailing list archive at Nabble.com.
More information about the R-sig-Geo
mailing list