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

Remove geosphere dependency #236

Merged
merged 10 commits into from
Aug 16, 2024
Merged
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
1 change: 0 additions & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ Imports:
gdalUtilities,
geodist,
gdistance,
geosphere,
jsonlite,
lubridate,
plotrix,
Expand Down
33 changes: 27 additions & 6 deletions R/util-point_offset.r
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
#' @title Identify new location based on distance and bearing from another
#'
#' @description Calculates latitude and longitude for new point that is x meters
#' away at bearing y from a geographic location (Longitude, Latitude). uses
#' "destPoint" function from "geosphere" package and calculations are based on
#' away at bearing y from a geographic location (Longitude, Latitude) using
#' great circle distances.
#'
#' @param lon vector of longitudes (dd) to calculate offset points
Expand Down Expand Up @@ -32,9 +31,7 @@

point_offset <- function(lon = NA, lat = NA, offsetDist = NA, offsetDir = NA,
distUnit = "m") {
# require(geosphere) #for spatialPoints

if (distUnit == "ft") offsetDist <- 0.3048 * offsetDist # conver to m if needed
if (distUnit == "ft") offsetDist <- 0.3048 * offsetDist # convert to m if needed
if (!(distUnit) %in% c("ft", "m")) {
stop("Input attribute 'dirUnit' must be 'm' (meters) or 'ft' (feet).")
}
Expand All @@ -53,6 +50,30 @@ point_offset <- function(lon = NA, lat = NA, offsetDist = NA, offsetDir = NA,

bearing <- dirKey$deg[match(offsetDir, dirKey$txt)]

pos <- geosphere::destPoint(cbind(lon, lat), bearing, offsetDist)
haversine <- function(lon, lat, bearing, offsetDist) {
lat <- lat * pi / 180
lon <- lon * pi / 180
R <- 6378137
bearing <- bearing * pi / 180
lat2 <- asin(sin(lat) * cos(offsetDist / R) + cos(lat) * sin(offsetDist / R) * cos(bearing))
lon2 <- 180 / pi * (
lon + atan2(
sin(bearing) * sin(offsetDist / R) * cos(lat),
cos(offsetDist / R) - sin(lat) * sin(lat2)
)
)

lat2 <- 180 / pi * lat2

coords <- matrix(
c(lon2, lat2),
ncol = 2,
dimnames = list(NULL, c("lon", "lat"))
)

return(coords)
}

pos <- haversine(lon, lat, bearing, offsetDist)
return(pos)
}
28 changes: 23 additions & 5 deletions R/vis-interpolate_path.r
Original file line number Diff line number Diff line change
Expand Up @@ -380,7 +380,15 @@ interpolate_path <- function(det, trans = NULL, start_time = NULL,
# as.matrix(.SD[.N, c("deploy_long", "deploy_lat")])), by = i.start]

# remove geosphere dependency and use geodist. geosphere relies on sp and raster.
dtc[, gcd := geodist::geodist_vec(x1 = .SD[[1, "deploy_long"]], y1 = .SD[[1, "deploy_lat"]], x2 = .SD[[.N, "deploy_long"]], y2 = .SD[[.N, "deploy_lat"]], measure = "haversine"), by = i.start]
dtc[, gcd := geodist::geodist_vec(
x1 = .SD[[1, "deploy_long"]],
y1 = .SD[[1, "deploy_lat"]],
x2 = .SD[[.N, "deploy_long"]],
y2 = .SD[[.N, "deploy_lat"]],
measure = "haversine"
),
by = i.start
]

# calculate least cost (non-linear) distance between points
message("Calculating least-cost (non-linear) distances... (step 1 of 3)")
Expand Down Expand Up @@ -577,10 +585,20 @@ interpolate_path <- function(det, trans = NULL, start_time = NULL,
nln_small[, latitude_lead := data.table::shift(nln_latitude, type = "lag", fill = NA), by = i.start]
nln_small[, longitude_lead := data.table::shift(nln_longitude, type = "lag", fill = NA), by = i.start]

nln_small[, cumdist := geosphere::distGeo(
.SD[, c("nln_longitude", "nln_latitude")],
.SD[, c("longitude_lead", "latitude_lead")]
), by = i.start]
# Switch from geosphere to geodist
# nln_small[, cumdist := geosphere::distGeo(
# .SD[, c("nln_longitude", "nln_latitude")],
# .SD[,c("longitude_lead", "latitude_lead")]), by = i.start]

nln_small[, cumdist := geodist::geodist_vec(
x1 = .SD[["nln_longitude"]],
y1 = .SD[["nln_latitude"]],
x2 = .SD[["longitude_lead"]],
y2 = .SD[["latitude_lead"]],
measure = "geodesic"
),
by = .I
]

nln_small[is.na(cumdist), cumdist := 0]
nln_small[, cumdist := cumsum(cumdist), by = i.start]
Expand Down
6 changes: 2 additions & 4 deletions inst/supplemental_docs/detection_range_handout.Rmd
Original file line number Diff line number Diff line change
Expand Up @@ -167,7 +167,6 @@ https://github.com/ocean-tracking-network/glatos.
# load libraries
library(glatos)
library(data.table)
library(geosphere)
library(ggplot2)

# load detection data from glatos package
Expand All @@ -188,7 +187,7 @@ of columns.
Many of the columns in the dtc object are not needed for this
analysis. In the next code chunk, we will extract only the necessary
columns from the `dtc` object and calculate the geographic distance
between each receiver using the `geosphere::distm` function. The
between each receiver using the `geodist::geodist_vec` function. The
internal tag on 4 receivers was used in this study so distances among
receivers is the same as distances between each tag and receiver.

Expand All @@ -202,8 +201,7 @@ dtc <- dtc[, c("detection_timestamp_utc", "transmitter_id", "receiver_sn",
rec_dist <- unique(dtc[,c("receiver_sn", "deploy_lat", "deploy_long")])

# calculate distance matrix
dist_matrix <- distm(rec_dist[, c("deploy_long", "deploy_lat")],
rec_dist[, c("deploy_long", "deploy_lat")])
dist_matrix <- geodist::geodist_vec(rec_dist$deploy_long, rec_dist$deploy_lat)

# rename columns and rows of matrix
colnames(dist_matrix) <- rec_dist[,get("receiver_sn")]
Expand Down
3 changes: 1 addition & 2 deletions man/point_offset.Rd

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

136 changes: 136 additions & 0 deletions tests/testthat/_snaps/interpolate_path.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,136 @@
# linear interpolation works

Code
linear_interp
Output
animal_id bin_timestamp latitude longitude record_type
1 1 2000-01-01 44.00000 -87.00000 detection
2 1 2000-01-02 44.01613 -86.85484 interpolated
3 1 2000-01-03 44.03226 -86.70968 interpolated
4 1 2000-01-04 44.04839 -86.56452 interpolated
5 1 2000-01-05 44.06452 -86.41935 interpolated
6 1 2000-01-06 44.08065 -86.27419 interpolated
7 1 2000-01-07 44.09677 -86.12903 interpolated
8 1 2000-01-08 44.11290 -85.98387 interpolated
9 1 2000-01-09 44.12903 -85.83871 interpolated
10 1 2000-01-10 44.14516 -85.69355 interpolated
11 1 2000-01-11 44.16129 -85.54839 interpolated
12 1 2000-01-12 44.17742 -85.40323 interpolated
13 1 2000-01-13 44.19355 -85.25806 interpolated
14 1 2000-01-14 44.20968 -85.11290 interpolated
15 1 2000-01-15 44.22581 -84.96774 interpolated
16 1 2000-01-16 44.24194 -84.82258 interpolated
17 1 2000-01-17 44.25806 -84.67742 interpolated
18 1 2000-01-18 44.27419 -84.53226 interpolated
19 1 2000-01-19 44.29032 -84.38710 interpolated
20 1 2000-01-20 44.30645 -84.24194 interpolated
21 1 2000-01-21 44.32258 -84.09677 interpolated
22 1 2000-01-22 44.33871 -83.95161 interpolated
23 1 2000-01-23 44.35484 -83.80645 interpolated
24 1 2000-01-24 44.37097 -83.66129 interpolated
25 1 2000-01-25 44.38710 -83.51613 interpolated
26 1 2000-01-26 44.40323 -83.37097 interpolated
27 1 2000-01-27 44.41935 -83.22581 interpolated
28 1 2000-01-28 44.43548 -83.08065 interpolated
29 1 2000-01-29 44.45161 -82.93548 interpolated
30 1 2000-01-30 44.46774 -82.79032 interpolated
31 1 2000-01-31 44.48387 -82.64516 interpolated
32 1 2000-02-01 44.50000 -82.50000 detection
33 1 2000-02-02 44.46552 -82.34483 interpolated
34 1 2000-02-03 44.43103 -82.18966 interpolated
35 1 2000-02-04 44.39655 -82.03448 interpolated
36 1 2000-02-05 44.36207 -81.87931 interpolated
37 1 2000-02-06 44.32759 -81.72414 interpolated
38 1 2000-02-07 44.29310 -81.56897 interpolated
39 1 2000-02-08 44.25862 -81.41379 interpolated
40 1 2000-02-09 44.22414 -81.25862 interpolated
41 1 2000-02-10 44.18966 -81.10345 interpolated
42 1 2000-02-11 44.15517 -80.94828 interpolated
43 1 2000-02-12 44.12069 -80.79310 interpolated
44 1 2000-02-13 44.08621 -80.63793 interpolated
45 1 2000-02-14 44.05172 -80.48276 interpolated
46 1 2000-02-15 44.01724 -80.32759 interpolated
47 1 2000-02-16 43.98276 -80.17241 interpolated
48 1 2000-02-17 43.94828 -80.01724 interpolated
49 1 2000-02-18 43.91379 -79.86207 interpolated
50 1 2000-02-19 43.87931 -79.70690 interpolated
51 1 2000-02-20 43.84483 -79.55172 interpolated
52 1 2000-02-21 43.81034 -79.39655 interpolated
53 1 2000-02-22 43.77586 -79.24138 interpolated
54 1 2000-02-23 43.74138 -79.08621 interpolated
55 1 2000-02-24 43.70690 -78.93103 interpolated
56 1 2000-02-25 43.67241 -78.77586 interpolated
57 1 2000-02-26 43.63793 -78.62069 interpolated
58 1 2000-02-27 43.60345 -78.46552 interpolated
59 1 2000-02-28 43.56897 -78.31034 interpolated
60 1 2000-02-29 43.53448 -78.15517 interpolated
61 1 2000-03-01 43.50000 -78.00000 detection

# Non-linear interpolation works

Code
nonlinear_interp
Output
animal_id bin_timestamp latitude longitude record_type
1 1 2000-01-01 44.00000 -87.00000 detection
2 1 2000-01-02 44.14456 -87.01280 interpolated
3 1 2000-01-03 44.28736 -86.99353 interpolated
4 1 2000-01-04 44.39165 -86.84604 interpolated
5 1 2000-01-05 44.49603 -86.69840 interpolated
6 1 2000-01-06 44.60050 -86.55064 interpolated
7 1 2000-01-07 44.70507 -86.40275 interpolated
8 1 2000-01-08 44.80973 -86.25472 interpolated
9 1 2000-01-09 44.91448 -86.10656 interpolated
10 1 2000-01-10 45.01933 -85.95826 interpolated
11 1 2000-01-11 45.12427 -85.80983 interpolated
12 1 2000-01-12 45.22932 -85.66127 interpolated
13 1 2000-01-13 45.33445 -85.51257 interpolated
14 1 2000-01-14 45.43968 -85.36373 interpolated
15 1 2000-01-15 45.54501 -85.21476 interpolated
16 1 2000-01-16 45.65044 -85.06565 interpolated
17 1 2000-01-17 45.74961 -84.91262 interpolated
18 1 2000-01-18 45.75648 -84.70329 interpolated
19 1 2000-01-19 45.72012 -84.49758 interpolated
20 1 2000-01-20 45.65750 -84.30603 interpolated
21 1 2000-01-21 45.59065 -84.11694 interpolated
22 1 2000-01-22 45.52387 -83.92803 interpolated
23 1 2000-01-23 45.45715 -83.73930 interpolated
24 1 2000-01-24 45.37617 -83.56547 interpolated
25 1 2000-01-25 45.27099 -83.41672 interpolated
26 1 2000-01-26 45.16591 -83.26810 interpolated
27 1 2000-01-27 45.06093 -83.11961 interpolated
28 1 2000-01-28 44.95605 -82.97127 interpolated
29 1 2000-01-29 44.85125 -82.82305 interpolated
30 1 2000-01-30 44.74656 -82.67497 interpolated
31 1 2000-01-31 44.64195 -82.52702 interpolated
32 1 2000-02-01 44.50000 -82.50000 detection
33 1 2000-02-02 44.27598 -82.41394 interpolated
34 1 2000-02-03 44.02721 -82.41394 interpolated
35 1 2000-02-04 43.77843 -82.41394 interpolated
36 1 2000-02-05 43.52964 -82.41394 interpolated
37 1 2000-02-06 43.28084 -82.41394 interpolated
38 1 2000-02-07 43.03202 -82.41394 interpolated
39 1 2000-02-08 42.80359 -82.47917 interpolated
40 1 2000-02-09 42.60977 -82.59843 interpolated
41 1 2000-02-10 42.43783 -82.84162 interpolated
42 1 2000-02-11 42.27309 -83.09093 interpolated
43 1 2000-02-12 42.03710 -83.06659 interpolated
44 1 2000-02-13 41.99075 -82.75061 interpolated
45 1 2000-02-14 42.02156 -82.42462 interpolated
46 1 2000-02-15 42.12825 -82.12284 interpolated
47 1 2000-02-16 42.23508 -81.82064 interpolated
48 1 2000-02-17 42.34205 -81.51804 interpolated
49 1 2000-02-18 42.43982 -81.21055 interpolated
50 1 2000-02-19 42.49761 -80.88362 interpolated
51 1 2000-02-20 42.55545 -80.55641 interpolated
52 1 2000-02-21 42.64289 -80.24305 interpolated
53 1 2000-02-22 42.71383 -79.92129 interpolated
54 1 2000-02-23 42.77186 -79.59301 interpolated
55 1 2000-02-24 42.82994 -79.26443 interpolated
56 1 2000-02-25 42.88807 -78.93556 interpolated
57 1 2000-02-26 43.07781 -79.02641 interpolated
58 1 2000-02-27 43.28679 -78.97607 interpolated
59 1 2000-02-28 43.38565 -78.66456 interpolated
60 1 2000-02-29 43.44428 -78.33286 interpolated
61 1 2000-03-01 43.50000 -78.00000 detection

Loading