Class: Eg::Net::GeoCoordinate

Inherits:
Object
  • Object
show all
Defined in:
lib/eg/net/simulator.rb

Constant Summary collapse

@@precision =
0.00001

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(lat, lon) ⇒ GeoCoordinate

Returns a new instance of GeoCoordinate.



12
13
14
15
# File 'lib/eg/net/simulator.rb', line 12

def initialize lat, lon
  @lat = lat
  @lon = lon
end

Instance Attribute Details

#latObject

Returns the value of attribute lat.



10
11
12
# File 'lib/eg/net/simulator.rb', line 10

def lat
  @lat
end

#lonObject

Returns the value of attribute lon.



10
11
12
# File 'lib/eg/net/simulator.rb', line 10

def lon
  @lon
end

Class Method Details

.parse(string) ⇒ Object



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/eg/net/simulator.rb', line 16

def GeoCoordinate.parse string
  coordinates = string.downcase.split(/\s*([wWnNsSeE])\s*|\s/).delete_if {|it| it.size.zero?}
  n = [0, 0, 0, 0, 0, 0]
  i = 0
  north = true; east = true
  coordinates.each do |data|
    c = data[0, 1] # pick the first character
    if c.between?('0', '9') or c == '-'
      n[i] = data.to_f
      i += 1
    end
    north = false if c == 's'
    east = false if c == 'w'
    i = 3 if i > 0 and 'nsew'.index(c)
  end
  lat = n[0] + n[1]/60 + n[2]/3600
  lon = n[3] + n[4]/60 + n[5]/3600
  GeoCoordinate.new(north ? lat : -lat, east ? lon : -lon)
end

Instance Method Details

#==(object) ⇒ Object



35
36
37
38
39
# File 'lib/eg/net/simulator.rb', line 35

def == object
  return false unless object.kind_of? GeoCoordinate
  return (@lat / @@precision).to_i == (object.lat / @@precision).to_i &&
         (@lon / @@precision).to_i == (object.lon / @@precision).to_i
end

#to_sObject



40
41
42
# File 'lib/eg/net/simulator.rb', line 40

def to_s
  "#{@lat > 0 ? 'N' : 'S'}#{@lat.abs} #{@lon > 0 ? 'E' : 'W'}#{@lon.abs}"
end