Class: Point

Inherits:
Object
  • Object
show all
Defined in:
lib/point.rb

Overview

A Point specifies a latitude, longitude and elevation Note that the elevation is not considered in distance calculations

Constant Summary collapse

AVG_CIRCUMFERANCE_OF_EARTH =
6372795

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(lat, long, elevation = 0.0) ⇒ Point

Returns a new instance of Point.



8
9
10
11
12
13
14
15
16
# File 'lib/point.rb', line 8

def initialize(lat, long, elevation=0.0)
  if(lat.nil? or long.nil?)
    raise ArgumentError
  end      

  @lat       = lat 
  @long      = long
  @elevation = elevation
end

Instance Attribute Details

#elevationObject (readonly)

Returns the value of attribute elevation.



6
7
8
# File 'lib/point.rb', line 6

def elevation
  @elevation
end

#latObject (readonly)

Returns the value of attribute lat.



6
7
8
# File 'lib/point.rb', line 6

def lat
  @lat
end

#longObject (readonly)

Returns the value of attribute long.



6
7
8
# File 'lib/point.rb', line 6

def long
  @long
end

Class Method Details

.degrees_to_radians(degrees) ⇒ Object



47
48
49
# File 'lib/point.rb', line 47

def Point.degrees_to_radians(degrees)
  degrees * ((2*Math::PI)/360)
end

Instance Method Details

#distance_from(other) ⇒ Object

Return the distance between one point and another in metres. Note that distance_from does not take elevation into account. The formula used is based on great circle distance formula from spherical goemetry, but as the Earth is not a sphere a small error may result (up to 0.5%). The formula used is that listed on en.wikipedia.org/wiki/Great-circle_distance



27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/point.rb', line 27

def distance_from(other)
  lat1, lat2 = Point.degrees_to_radians(@lat), Point.degrees_to_radians(other.lat)  
  long1, long2 = Point.degrees_to_radians(@long), Point.degrees_to_radians(other.long)   
  long_diff = (long1 - long2).abs

  dvd1 = (Math.cos(lat2) * Math.sin(long_diff))**2
  dvd2 = ((Math.cos(lat1)*Math.sin(lat2)) - (Math.sin(lat1)*Math.cos(lat2)*Math.cos(long_diff)))**2
  dividend = Math.sqrt(dvd1 + dvd2)
  
  dvr1 = Math.sin(lat1)*Math.sin(lat2)
  dvr2 = Math.cos(lat1)*Math.cos(lat2)*Math.cos(long_diff)
  divisor = dvr1 + dvr2
  
  arctan_arg = dividend / divisor
  
  angular_diff = Math.atan(arctan_arg)
  
  angular_diff * AVG_CIRCUMFERANCE_OF_EARTH  
end

#to_sObject



18
19
20
# File 'lib/point.rb', line 18

def to_s
  "Lat: #@lat Long: #@long Elevation: #@elevation"
end