Class: GpsUtils::Point

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(lat, lng) ⇒ Point

Initialize Point.

Parameters:

  • lat (Integer, Float)

    Latitude

  • lng (Integer, Float)

    Langitude


21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/gpsutils.rb', line 21

def initialize(lat, lng)
	unless lat.is_a? Float or lat.is_a? Integer
		raise ArgumentError.new 'lat must be float or integer.'
	end

	unless lng.is_a? Float or lng.is_a? Integer
		raise ArgumentError.new 'lng must be float or integer.'
	end

	@lat = lat
	@lng = lng
end

Instance Attribute Details

#latFloat, Integer (readonly)

Returns Latitude.

Returns:

  • (Float, Integer)

    Latitude


11
12
13
# File 'lib/gpsutils.rb', line 11

def lat
  @lat
end

#lngFloat, Integer (readonly)

Returns Longitude.

Returns:

  • (Float, Integer)

    Longitude


15
16
17
# File 'lib/gpsutils.rb', line 15

def lng
  @lng
end

Instance Method Details

#distance(other) ⇒ Float

Measure the distance between this point and another.

Distance is calculated using equirectangular projection.

Parameters:

Returns:

  • (Float)

Raises:

  • (ArgumentError)

    if other is not a Point

See Also:


52
53
54
55
56
57
58
59
60
61
62
63
64
# File 'lib/gpsutils.rb', line 52

def distance(other)
	unless other.is_a? Point
		raise ArgumentError.new 'other must be a Point.'
	end

	dlng = GpsUtils::to_radians(other.lng - @lng)
	dlat = GpsUtils::to_radians(other.lat - @lat)

	x = dlng * Math.cos(dlat / 2)
	y = GpsUtils::to_radians(other.lat - @lat)

	Math.sqrt(x**2 + y**2) * GpsUtils::R
end

#to_aArray

Returns Array with latitude and longitude as Float or Integer values, as used when object was initialized.

Returns:

  • (Array)

    Array with latitude and longitude as Float or Integer values, as used when object was initialized


36
37
38
# File 'lib/gpsutils.rb', line 36

def to_a
	[@lat, @lng]
end

#to_sObject


40
41
42
# File 'lib/gpsutils.rb', line 40

def to_s
	"#{@lat},#{@lng}"
end