Question

Hi im using ruby Geokit::LatLng class to store latitude and longitude and trying to find the midpoint between few points . i noticed a problem with this gem or i may have misunderstood the concepts .

          a = Geokit::LatLng.new(13.0627081,80.274658)
          b = Geokit::LatLng.new(12.8550615,80.2264393)

When i calculated the "midpoint_to" method to calculate the midpoint between these point i noted that they give different results

          a.midpoint_to(b) 
<Geokit::LatLng:0x0000000292f368 @lat=12.95888590853612, @lng=80.25053859693796> 

          b.midpoint_to(a)
<Geokit::LatLng:0x00000002933f80 @lat=12.958885908536992, @lng=80.250538596>

Can some explain me what's happening here. midpoint between a to b should be same as b to a or does it differ in latitude and longitude

Était-ce utile?

La solution

In your example, the two points are the same, to a very close approximation. It is worth identifying the difference - i.e. why you do not get the same object:

a.midpoint_to(b) 
<Geokit::LatLng:0x0000000292f368 @lat=12.95888590853612, @lng=80.25053859693796> 

b.midpoint_to(a)
<Geokit::LatLng:0x00000002933f80 @lat=12.958885908536992, @lng=80.250538596>
  • The first numbers, 0x0000000292f368 and 0x00000002933f80 are the object identities (it is not clear whether you already knew that). They will always be different in any new object instance, and do not measure anything related to distances or locations. There are a few other classes where this number is not shown, or is the same when answers are equal.

  • The difference in @lat and @lng is due to limits of floating-point precision. The results are not the same because the calculation to get the mid-point does not use the two sets of input numbers in exactly equivalent ways. In fact, even with very simple maths in floating point, a + (b - c) may not be the same as (a + b) - c. When you look at the difference in longitude you have between the two calculated mid-points, it is 9.379590437674779e-10 degrees. On the surface of the Earth, that is roughly a distance of 0.0001 metres (0.1 millimetres), whilst your input points are roughly 14.7 kilometres apart.

If you measure the distance using geokit, you get a reassuring 0.0, which you could probably use to confirm that two calculations are close enough to count as exactly the same point:

a = Geokit::LatLng.new(13.0627081,80.274658)
b = Geokit::LatLng.new(12.8550615,80.2264393)
m1 = a.midpoint_to(b)
m2 = b.midpoint_to(a)
if m1.distance_to( m2 ) > 0
  puts "Mid-points are separate"
else
  puts "Mid-points are the same location"
end

Output is:

Mid-points are the same location

If you did more complicated calculations, you might prefer to use a number slightly higher than 0.0, e.g 1e-5 (to nearest centimetre).

Autres conseils

Midpoint between a to b and vice versa will be same.

the midpoint may not be located half-way between latitudes/longitudes;

point a

Latitude Longitude 22.57265 88.36389

point b

Latitude Longitude 27.66483 -81.51575

a.midpoint_to(b) 
=> #<Geokit::LatLng:0xb9219f44 @lat=79.06104152033897, @lng=16.672528167825877

This page shows how to calculate the geographic midpoint

This post mightbe usefull to understand the formula

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top