Comments (3)
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 -
climate/R/nearest_stations_imgw.R
Line 72 in 1c23d1f
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.
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 ifsf
package is available; - if there's no sf packages - use the default computing engine, but provide warning message about possible inaccuracies
from climate.
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)
- Problem with meteo_imgw function HOT 2
- Error while using meteo_ogimet() for certain stations in Switzerland HOT 3
- HTTP error 502
- Suggestions to `nearest_stations_imgw()` HOT 1
- IMGW hydrological data has different number of columns HOT 2
- Error in another wyoming url HOT 3
- CRAN email HOT 2
- New error message when downloading OGIMET data HOT 2
- IMGW: incorrect results if `station` length > 1 HOT 2
- No internet connection HOT 4
- Problem downloading OPAD data for 2021 HOT 4
- meteo_imgw speed up for a single station
- CRAN policy HOT 2
- two suggestions for sounding_wyoming HOT 1
- hydro_imgw_daily not returning results for failures HOT 1
- Walter diagram with Climate HOT 1
- support for meteorological IMGW datastore dataset
- Error when trying to find the nearest meteorological stations in Spain HOT 6
- HTTP conection error HOT 1
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from climate.