Uko
Uko

Reputation: 13396

Geo coordinates operations

As Esteban A. Maringolo asked:

Has anybody implemented basic methods/classes to calculate distance between two points (lat, long) and similar operations?

Upvotes: 1

Views: 134

Answers (2)

Uko
Uko

Reputation: 13396

Simpler solution by Sven Van Caekenberghe:

This is the formula that is being used for years in Pharo, Java[Script], Common Lisp:

distanceBetween: firstPosition and: secondPosition
 "T3GeoTools distanceBetween: [email protected] and: [email protected]"

 | c |
 c := (firstPosition y degreeSin * secondPosition y degreeSin)
      + (firstPosition y degreeCos * secondPosition y degreeCos
         * (secondPosition x degreesToRadians - firstPosition x degreesToRadians) cos).
 c := c >= 0 ifTrue: [ 1 min: c ] ifFalse: [ -1 max: c ].
 ^ c arcCos * 6371000

This is between WGS84 coordinates. Use this page as reference: http://www.movable-type.co.uk/scripts/latlong.html

Upvotes: 1

Uko
Uko

Reputation: 13396

Solution found by Esteban A. Maringolo:

distanceFromLat: lat1 long: long1 toLat: lat2 long: long2
"Answer the distance in meters between two coordinates in float number representation."

  | lat1Rad  lon1Rad lat2Rad lon2Rad earthRadius dLat dLon dLatSinSqrd dLonSinSqrd cosLatLat a c distance |
  lat1Rad := lat1 degreesToRadians.
  lon1Rad := long1  degreesToRadians.
  lat2Rad := lat2 degreesToRadians.
  lon2Rad := long2 degreesToRadians.
  earthRadius := 6371.00.
  dLat := lat2Rad - lat1Rad.
  dLon := lon2Rad - lon1Rad.
  dLatSinSqrd := (dLat / 2) sin squared.
  dLonSinSqrd := (dLon / 2) sin squared.
  cosLatLat := lat2Rad cos * lat1Rad cos.
  a := dLatSinSqrd + (cosLatLat * dLonSinSqrd).
  c := 2 * a sqrt arcSin.
  distance := earthRadius * c.
  ^ distance

Upvotes: 1

Related Questions