spatsoc
spatsoc copied to clipboard
Unprojected distance?
Hey guys,
would it not make sense to utilize unprojected distance calculation methods? (e.g., Haversine, geodesic, ... as implemented in geosphere)
Right now you have to project the coordinates. This makes sense for localized analyses but falls short when you work with larger areas and thresholds which cross UTM zones.
E.g., something along the lines of the code below
DT[, withinGroup := {
distMatrix <-
# Geodesic distance via geosphere
as.matrix(geosphere::distm(cbind(
get(..coords[1]), get(..coords[2])
),
fun = distGeo))
graphAdj <-
igraph::graph_from_adjacency_matrix(distMatrix <= threshold)
igraph::clusters(graphAdj)$membership
},
by = splitBy, .SDcols = c(coords, id)]
DT[, group := .GRP,
by = c(splitBy, 'withinGroup')]
set(DT, j = 'withinGroup', value = NULL)
return(DT[])
Hey @RomanAbashin, Yes - great point. The original functions were written with a relatively local (within one UTM zone), animal social network context in mind.
And thanks for the code chunk, that'd be the only spot to change it in group_pts along with some of the if checks above potentially.
I'll look into this and update here. Thanks!
@RomanAbashin I've started implementing this in #26.
I started testing this with the package's example data, with original UTM coordinates back in WGS84 but if you had some of your own data you wanted to try it out with - please let me know what you think.
I've opted for geodist because it doesn't come with a pile of dependencies and is fast - but let me know if you have any alternatives.
I'll need to add some checks in the function to warn if unprojected coordinates were provided without turning the "lonlat" switch on, add some tests, and update the vignettes that state throughout UTM coordinates are expected.