hypertidy/geodist

geodist gives different results for single row matrix with cheap ruler

mem48 opened this issue · 3 comments

mem48 commented

A one row matrix of from and to points gives a different distance as a many row matrix with the same coordinates when using the cheap ruler.

# example data
from <- matrix(rep(c(-0.193011, 52.15549),2), ncol = 2, byrow = TRUE)
to <- matrix(rep(c(-0.197722, 52.15395),2), ncol = 2, byrow = TRUE)
colnames(from) <- c("lon","lat")
colnames(to) <- c("lon","lat")

# Just first row of matrix
geodist::geodist(from[1, , drop = FALSE], to[1,, drop = FALSE], paired = TRUE, measure = "cheap")
# Both rows (gives different answer)
geodist::geodist(from, to, paired = TRUE, measure = "cheap")

# This doesn't happen with other measures
geodist::geodist(from[1, , drop = FALSE], to[1,, drop = FALSE], paired = TRUE, measure = "geodesic")
geodist::geodist(from, to, paired = TRUE, measure = "geodesic")
mem48 commented

@mpadge any update on when this might be fixed?

Oh, that had slipped off the radar - thanks @mem48, i'll get on to it asap

library (geodist)
# example data
from <- matrix(rep(c(-0.193011, 52.15549),2), ncol = 2, byrow = TRUE)
to <- matrix(rep(c(-0.197722, 52.15395),2), ncol = 2, byrow = TRUE)
colnames(from) <- c("lon","lat")
colnames(to) <- c("lon","lat")

# Just first row of matrix
geodist::geodist(from[1, , drop = FALSE], to[1,, drop = FALSE], paired = TRUE, measure = "cheap")
#> [1] 363.9617
geodist::geodist(from[1, , drop = TRUE], to[1,, drop = TRUE], measure = "cheap")
#>          [,1]
#> [1,] 363.9617
# Both rows (gives **SAME** answer)
geodist::geodist(from, to, paired = TRUE, measure = "cheap")
#> [1] 363.9617 363.9617

Created on 2020-01-14 by the reprex package (v0.3.0)

It all came from if <...> else if <...> statements used to determine min & max values. If there is only one value, then the clause is only evaluated once, and only generates a min value while the max stays undefined (at an arbitrarily large value). I simply had to remove the else to leave 2 separate if statements in every case, so full ranges are evaluated even for single points. Thanks