rspatial / geosphere

R geosphere package
GNU General Public License v3.0
34 stars 7 forks source link

gcIntersectBearing(): Warning in acos((sin(lat2) - sin(lat1) * cos(dist12))/(sin(dist12) * cos(lat1))) : NaNs produced #5

Open casa-henrym opened 3 years ago

casa-henrym commented 3 years ago

The same issue as raised previously - an attempt to calculate acos() of a number (slightly) greater than 1 and (slightly) less than -1:

My work-around:

...
i <- rep(TRUE, length(dist12))

i[dist12 == 0] <- FALSE

foo <- (sin(lat2) - sin(lat1) * cos(dist12))/(sin(dist12) * cos(lat1))
foo[foo > 1] <- 1
foo[foo < -1] <- -1

brngA <- acos(foo)

brngA[is.na(brngA)] <- 0

bar <- (sin(lat1) - sin(lat2) * cos(dist12))/(sin(dist12) * cos(lat2))
bar[bar > 1] <- 1
bar[bar < -1] <- -1

brngB <- acos(bar)

g <- (sin(lon2 - lon1) > 0)
...