I have two sets of lat and long coordinates. I would like to find the midpoint between them. Not sure how to calculate this in R.
$\begingroup$
$\endgroup$
6
-
1$\begingroup$ Why doesn't the average of the coordinates work? Also, please ask your code-related questions at stackoverflow.com $\endgroup$– DavidCommented Jun 28, 2019 at 11:37
-
$\begingroup$ If your question is about R, then it is off topic here. If your question is about what calculations should be done, then please clarify. $\endgroup$– Peter FlomCommented Jun 28, 2019 at 12:58
-
$\begingroup$ See gis.stackexchange.com/questions/18584. $\endgroup$– whuber ♦Commented Jun 28, 2019 at 14:12
-
$\begingroup$ This question is about how to average points in spherical coordinates and it's clearly on topic, here. Performing the calculation in R or using an abacus is incidental and doesn't make it a coding question. The question should be reopened. $\endgroup$– PereCommented Jun 29, 2019 at 9:19
-
1$\begingroup$ @whuber I must admit that as long as the question remain redistricted to just two points, it is just geometry and not statistics. $\endgroup$– PereCommented Jun 29, 2019 at 19:16
|
Show 1 more comment
1 Answer
$\begingroup$
$\endgroup$
3
Does this one do the job?
mid_point <- function(long1, lat1, long2, lat2) {
if(abs(long1-long2 < 180)) {
long <- (long1+long2)/2
} else {
long <- 180 + (long1+long2)/2
}
if(long > 180)
long <- long - 180
lat <- (lat1+lat2)/2
return(c(long, lat))
}
```
-
1$\begingroup$ That method ignores the fact that the Earth is (roughly) an sphere and that the segment between two points in a sphere is an arc of maximal circumference, which generally doesn't map to a segment in the lat-long plane. For example, for two points in the same latitude, this procedure would find their midpoint on their parallel, but the actual midpoint is closer to the nearest pole than that. $\endgroup$– PereCommented Jun 29, 2019 at 9:22
-
$\begingroup$ Is there a formula somewhere for calculating that midpoint? By the way, the validity of this approximation depends on the scale. At short distances, it's fine. No information was provided about whether the points would be 8 or 8.000 km apart $\endgroup$– DavidCommented Jun 29, 2019 at 12:50
-
2$\begingroup$ You are right that if distances are small, accuracy requirements aren't very strict and we are far enough from the poles, averaging coordinates works fine. For two points, a more exact but simple way to compute is convert them to 3D Cartesian coordinates and project the average on the sphere. If more accuracy was needed, we would need use an ellipsoid. To average more than two points, it's not obvious what does averaging points on a sphere means (definition needed), although we can resort to the simple definition of averaging points in Cartesian coordinates and project. $\endgroup$– PereCommented Jun 29, 2019 at 13:19