Skip to content

Instantly share code, notes, and snippets.

@vjcitn
Created February 16, 2025 18:58
Show Gist options
  • Save vjcitn/c2a7d034b84cfc33ee1e9e3b1f9354c7 to your computer and use it in GitHub Desktop.
Save vjcitn/c2a7d034b84cfc33ee1e9e3b1f9354c7 to your computer and use it in GitHub Desktop.
code for producing sf ellipse geometry from data frame with center, lengths of major/minor radii, and orientation angle in degrees
df2ellipses = function(datf, npts=100) {
requireNamespace("sfdep")
requireNamespace("sf")
stopifnot(all(c("X_centroid", "Y_centroid", "maj", "min", "angdeg") %in% names(datf)))
dd = data.matrix(datf[,c("X_centroid", "Y_centroid")])
cens = lapply(seq_len(nrow(datf)), function(x) sf::st_point(dd[x,]))
maj = datf$maj
min = datf$min
angle_in_deg = datf$angdeg # angle_in_rad/0.01745329
dd = data.matrix(cbind(dd, angdeg=datf[,"angdeg"])) #angle_in_deg)
tt = lapply(seq_len(length(cens)), function(x)
sfdep::st_ellipse(cens[[x]], sx=maj[x], sy=min[x], rotation=angle_in_deg[x], n=npts))
tttf = lapply(tt, function(x) matrix(unlist(x), nr=npts+1))
ok = sapply(tttf, function(x) nrow(x)>0)
sf::st_multilinestring(tttf[which(ok)])
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment