Class: GPSTool::WorldPoint

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

Constant Summary collapse

EARTH_RADIUS =
6371

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(datetime, latitude, longitude, altitude = EARTH_RADIUS) ⇒ WorldPoint

Returns a new instance of WorldPoint.


11
12
13
14
15
16
# File 'lib/gpstool/world-point.rb', line 11

def initialize(datetime, latitude, longitude, altitude = EARTH_RADIUS)
	@datetime = datetime
	@latitude = latitude
	@longitude = longitude
	@altitude = altitude
end

Instance Attribute Details

#altitudeObject (readonly)

Returns the value of attribute altitude


20
21
22
# File 'lib/gpstool/world-point.rb', line 20

def altitude
  @altitude
end

#datetimeObject (readonly)

Returns the value of attribute datetime


21
22
23
# File 'lib/gpstool/world-point.rb', line 21

def datetime
  @datetime
end

#latitudeObject (readonly)

Returns the value of attribute latitude


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

def latitude
  @latitude
end

#longitudeObject (readonly)

Returns the value of attribute longitude


19
20
21
# File 'lib/gpstool/world-point.rb', line 19

def longitude
  @longitude
end

Instance Method Details

#averageSpeedTo(other) ⇒ Object


36
37
38
39
40
41
42
43
44
45
# File 'lib/gpstool/world-point.rb', line 36

def averageSpeedTo(other)
	# In Km
	distance = self.distanceTo(other)

	# In hours
	duration = (other.datetime - @datetime) * 24

	# In Km/hr
	return distance / duration
end

#distanceTo(other) ⇒ Object


23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/gpstool/world-point.rb', line 23

def distanceTo(other)
	dLat = (other.latitude - self.latitude).to_radians
	dLon = (other.longitude - self.longitude).to_radians

	a = Math.sin(dLat/2) * Math.sin(dLat/2) +
		Math.cos(self.latitude.to_radians) * Math.cos(other.latitude.to_radians) * 
		Math.sin(dLon/2) * Math.sin(dLon/2)

	c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1-a))

	d = EARTH_RADIUS * c
end