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.

Constants

DIRECTIONS

Cardinal directions.

Attributes

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)

Public Class Methods

new( latitude, longitude, height=0 )

Instance a new Position object given a latitude, longitude, and optional height.

# File lib/ravn/geo/position.rb, line 29
def initialize( latitude, longitude, height=0 )
        @latitude  = latitude
        @longitude = longitude
        @height    = height
end

Public Instance Methods

<=>( other )

Comparable API: Sort based on array’s defaults.

# File lib/ravn/geo/position.rb, line 49
def <=>( other )
        return self.to_a <=> other.to_a
end
==( other )

Basic equality check.

# File lib/ravn/geo/position.rb, line 56
def ==( other )
        return self.to_a == other.to_a
end
bearing_to( position )

Calculate the bearing to position in degrees.

# File lib/ravn/geo/position.rb, line 107
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.

# File lib/ravn/geo/position.rb, line 125
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 )
Alias for: distance_to
distance_to( position )

Calculate distance from this position to other position, in metres.

# File lib/ravn/geo/position.rb, line 87
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
Also aliased as: distance_from
mgrs()

Return a memoized Military Grid Reference System (MGRS/NATO) representation.

# File lib/ravn/geo/position.rb, line 71
def mgrs
        return @mgrs ||= Ravn::Geo::MGRS.from_utm( self.utm, self.latitude )
end
Also aliased as: to_mgrs
to_a()

Return a coordinate pair.

# File lib/ravn/geo/position.rb, line 63
def to_a
        return [ self.lat, self.long ]
end
to_mgrs()
Alias for: mgrs
to_utm()
Alias for: utm
utm()

Return a memoized Universal Transverse Mercator representation.

# File lib/ravn/geo/position.rb, line 79
def utm
        return @utm ||= Ravn::Geo::UTM.from_position( self )
end
Also aliased as: to_utm