Ravn::Geo::
Position
class
A representation of a latitude/longitude location pair.
A latitude/longitude point defines a geographic location on or above/below the earth’s surface, measured in degrees from the equator & the International Reference Meridian and in metres above the ellipsoid.
Elements of this class are based from the “geospatial” gem, MIT licensed. Attributions to Samuel G. D. Williams samuel@codeotaku.com. Note: depending on our future requirements, it may make more sense to just use that gem directly at a later date.
- DIRECTIONS
Cardinal directions.
- height R
Height above ellipsoid in metres.
- lat R
Latitude (in degrees)
- latitude R
Latitude (in degrees)
- long R
Longitude (in degrees)
- longitude R
Longitude (in degrees)
new( latitude, longitude, height=0 )
Instance a new Position
object given a latitude
, longitude
, and optional height
.
def initialize( latitude, longitude, height=0 )
@latitude = latitude
@longitude = longitude
@height = height
end
Comparable API: Sort based on array’s defaults.
def <=>( other )
return self.to_a <=> other.to_a
end
Basic equality check.
def ==( other )
return self.to_a == other.to_a
end
Calculate the bearing to position
in degrees.
def bearing_to( position )
lon1 = self.longitude.to_radians
lat1 = self.latitude.to_radians
lon2 = position.longitude.to_radians
lat2 = position.latitude.to_radians
bearing = Math::atan2(
Math::sin(lon2 - lon1) * Math::cos(lat2),
Math::cos(lat1) * Math::sin(lat2) - Math::sin(lat1) * Math::cos(lat2) * Math::cos(lon2-lon1)
).to_degrees
bearing += 360 if bearing < 0
return bearing
end
cardinal_bearing( position )
Returns a cardinal string to position
.
def cardinal_bearing( position )
bearing = self.bearing_to( position )
idx = ( bearing.to_f / ( 360.0 / DIRECTIONS.size ) ).round
return DIRECTIONS[ idx % DIRECTIONS.size ]
end
distance_from( position )
Calculate distance from this position to other position
, in metres.
def distance_to( position )
rlong1 = self.longitude.to_radians
rlat1 = self.latitude.to_radians
rlong2 = position.longitude.to_radians
rlat2 = position.latitude.to_radians
dlon = rlong1 - rlong2
dlat = rlat1 - rlat2
a = Math::sin(dlat/2) ** 2 + Math::cos(rlat1) * Math::cos(rlat2) * Math::sin(dlon/2) ** 2
c = 2 * Math::atan2(Math::sqrt(a), Math::sqrt(1-a))
d = EARTH_RADIUS * c
return d
end
Return a memoized Military Grid Reference System (MGRS/NATO) representation.
def mgrs
return @mgrs ||= Ravn::Geo::MGRS.from_utm( self.utm, self.latitude )
end
Return a coordinate pair.
def to_a
return [ self.lat, self.long ]
end
Return a memoized Universal Transverse Mercator representation.
def utm
return @utm ||= Ravn::Geo::UTM.from_position( self )
end