GithubHelp home page GithubHelp logo

Comments (3)

Nowosad avatar Nowosad commented on June 26, 2024 1

Is not there a 3rd choice - just to use a better approximation of the distances? If I understand the underlining code, it uses the Euclidean distance -

distance_points = stats::dist(distmatrix, method = "euclidean")[1:dim(result)[1]]
.
However, we could use https://pl.wikibooks.org/wiki/Astronomiczne_podstawy_geografii/Odleg%C5%82o%C5%9Bci - it would require implementing a new dist function.

from climate.

bczernecki avatar bczernecki commented on June 26, 2024

Thanks Krzysztof for the valuable comment!
I was considering to use GIS standards in the package, but since sf and sp are based on quite heavy system dependencies (which are causing sometimes lot of problems with installation) it was decided to use as simple approach as possible for the price of lower accurracy.
Please also note that we have nearest_stations_* family of functions where EPSG 2180 might be not the best choice.
To sum it up I think it might be worthy adding following items:

  • extra argument for nearest_stations* (e.g. use_sf = TRUE) to calculate distances based on CRS projections if sf package is available;
  • if there's no sf packages - use the default computing engine, but provide warning message about possible inaccuracies

from climate.

kadyb avatar kadyb commented on June 26, 2024

Here I implemented formulae to calculate the distance between points on a sphere. The simple formula is the first formula from the source from Jakub. Two more (haversine and Vincenty) come from here. I haven't done any accuracy and speed benchmarks, but referring to Wikipedia, the Vincenty formula is slower, but more accurate.

spherical_dist = function(p1, p2, formula = NULL) {

  if (is.null(formula))
    stop("formula should be: 'simple', 'haversine' or 'Vincenty'")

  if (p1[1] > 180 || p2[1] > 180) stop("x should be longitude")
  if (p1[2] > 90 || p2[2] > 90) stop("y should be latitude")

  r = 6371009 # mean earth radius in meters
  deg2rad = function(deg) {deg * pi / 180}

  vec = c(p1, p2)
  vec = sapply(vec, FUN = deg2rad)
  diff_long = vec[3] - vec[1]
  diff_lat = vec[4] - vec[2]

  if (formula == "simple") {
    d = sqrt(diff_lat^2 + (cos((vec[2] + vec[4]) / 2) * diff_long)^2)
  }

  if (formula == "haversine") {
    d = (sin(diff_lat / 2))^2 + (1 - (sin(diff_lat / 2))^2 - (sin((vec[2] + vec[4]) / 2))^2) * (sin(diff_long / 2))^2
    d = 2 * asin(sqrt(d))
  }

  if (formula == "Vincenty") {
    num = (cos(vec[4]) * sin(diff_long))^2 + (cos(vec[2]) * sin(vec[4]) - sin(vec[2]) * cos(vec[4]) * cos(diff_long))^2
    denom = sin(vec[2]) * sin(vec[4]) + cos(vec[2]) * cos(vec[4]) * cos(diff_long)
    d = atan(sqrt(num) / denom)
  }

  d = d * r
  return(d/1000) # output in km

}


# correct: 105.4737 km
p1 = c(18.633333, 54.366667) # Gdańsk
p2 = c(17.016667, 54.466667) # Słupsk
spherical_dist(p1, p2, formula = "simple") #> 105.1923
spherical_dist(p1, p2, formula = "haversine") #> 105.19
spherical_dist(p1, p2, formula = "Vincenty") #> 105.19

# correct: 21.01992 km
p1 = c(17.16716, 54.64006) # GARDNA WIELKA
p2 = c(16.85410, 54.58831) # USTKA
spherical_dist(p1, p2, formula = "simple") #> 20.96341
spherical_dist(p1, p2, formula = "haversine") #> 20.96339
spherical_dist(p1, p2, formula = "Vincenty") #> 20.96339

Correct distances in comments are calculated in the planar coordinate system (EPSG 2180), but there are also some distortions.

from climate.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.