Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Enhancement/use geodist for unprojected coordinates #26

Open
wants to merge 8 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,9 @@ Imports: data.table (>= 1.10.5),
sp,
rgeos,
adehabitatHR,
igraph,
methods
igraph,
methods,
geodist
Suggests: testthat,
knitr,
rmarkdown,
Expand Down
25 changes: 20 additions & 5 deletions R/group_pts.R
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@
#' @param coords Character vector of X coordinate and Y coordinate column names
#' @param timegroup timegroup field in the DT upon which the grouping will be calculated
#' @param splitBy (optional) character string or vector of grouping column name(s) upon which the grouping will be calculated
#' @param lonlat boolean indiciating if coordinates are unprojected
#' @param lonlatMeasure character of "haversine" "vincenty", "geodesic", or "cheap", specifying the method of distance calculation. Ignored if lonlat is FALSE. See \code{\link[geodist:geodist]{geodist::geodist}} for details.
#'
#' @export
#'
Expand All @@ -43,6 +45,7 @@
#'
#' # Temporal grouping
#' group_times(DT, datetime = 'datetime', threshold = '20 minutes')
#'
#' # Spatial grouping with timegroup
#' group_pts(DT, threshold = 5, id = 'ID',
#' coords = c('X', 'Y'), timegroup = 'timegroup')
Expand All @@ -55,7 +58,9 @@ group_pts <- function(DT = NULL,
id = NULL,
coords = NULL,
timegroup,
splitBy = NULL) {
splitBy = NULL,
lonlat = FALSE,
lonlatMeasure = 'geodesic') {
# due to NSE notes in R CMD check
N <- withinGroup <- ..id <- ..coords <- group <- NULL

Expand Down Expand Up @@ -140,11 +145,21 @@ group_pts <- function(DT = NULL,
}

DT[, withinGroup := {
distMatrix <-
as.matrix(stats::dist(cbind(
if (lonlat) {
xy <- cbind(
get(..coords[1]), get(..coords[2])
),
method = 'euclidean'))
)
names(xy) <- c('x', 'y')
distMatrix <-
geodist::geodist(xy,
measure = lonlatMeasure)
} else {
distMatrix <-
as.matrix(stats::dist(cbind(
get(..coords[1]), get(..coords[2])
),
method = 'euclidean'))
}
graphAdj <-
igraph::graph_from_adjacency_matrix(distMatrix <= threshold)
igraph::clusters(graphAdj)$membership
Expand Down
9 changes: 8 additions & 1 deletion man/group_pts.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.