Class: Resolv::LOC::Coord

Inherits:
Object
  • Object
show all
Defined in:
lib/logstash/patches/resolv_9270.rb

Overview

A Resolv::LOC::Coord

Constant Summary collapse

Regex =
/^(\d+)\s(\d+)\s(\d+\.\d+)\s([NESW])$/

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(coordinates, orientation) ⇒ Coord

Returns a new instance of Coord.



2764
2765
2766
2767
2768
2769
2770
2771
2772
2773
# File 'lib/logstash/patches/resolv_9270.rb', line 2764

def initialize(coordinates,orientation)
  unless coordinates.kind_of?(String)
    raise ArgumentError.new("Coord must be a 32bit unsigned integer in hex format: #{coordinates.inspect}")
  end
  unless orientation.kind_of?(String) && orientation[/^lon$|^lat$/]
    raise ArgumentError.new('Coord expects orientation to be a String argument of "lat" or "lon"')
  end
  @coordinates = coordinates
  @orientation = orientation
end

Instance Attribute Details

#coordinatesObject (readonly)

The raw coordinates



2778
2779
2780
# File 'lib/logstash/patches/resolv_9270.rb', line 2778

def coordinates
  @coordinates
end

#orientationObject (readonly)

The orientation of the hemisphere as ‘lat’ or ‘lon’



2782
2783
2784
# File 'lib/logstash/patches/resolv_9270.rb', line 2782

def orientation
  @orientation
end

Class Method Details

.create(arg) ⇒ Object

Creates a new LOC::Coord from arg which may be:

LOC::Coord

returns arg.

String

arg must match the LOC::Coord::Regex constant



2743
2744
2745
2746
2747
2748
2749
2750
2751
2752
2753
2754
2755
2756
2757
2758
2759
2760
2761
2762
# File 'lib/logstash/patches/resolv_9270.rb', line 2743

def self.create(arg)
  case arg
  when Coord
    return arg
  when String
    coordinates = ''
    if Regex =~ arg && $1.to_f < 180
      m = $~
      hemi = (m[4][/[NE]/]) || (m[4][/[SW]/]) ? 1 : -1
      coordinates = [ ((m[1].to_i*(36e5)) + (m[2].to_i*(6e4)) +
          (m[3].to_f*(1e3))) * hemi+(2**31) ].pack("N")
      orientation = m[4][/[NS]/] ? 'lat' : 'lon'
    else
      raise ArgumentError.new("not a properly formed Coord string: " + arg)
    end
    return Coord.new(coordinates,orientation)
  else
    raise ArgumentError.new("cannot interpret as Coord: #{arg.inspect}")
  end
end

Instance Method Details

#==(other) ⇒ Object

:nodoc:



2807
2808
2809
# File 'lib/logstash/patches/resolv_9270.rb', line 2807

def ==(other) # :nodoc:
  return @coordinates == other.coordinates
end

#eql?(other) ⇒ Boolean

:nodoc:

Returns:

  • (Boolean)


2811
2812
2813
# File 'lib/logstash/patches/resolv_9270.rb', line 2811

def eql?(other) # :nodoc:
  return self == other
end

#hashObject

:nodoc:



2815
2816
2817
# File 'lib/logstash/patches/resolv_9270.rb', line 2815

def hash # :nodoc:
  return @coordinates.hash
end

#inspectObject

:nodoc:



2803
2804
2805
# File 'lib/logstash/patches/resolv_9270.rb', line 2803

def inspect # :nodoc:
  return "#<#{self.class} #{self}>"
end

#to_sObject

:nodoc:



2784
2785
2786
2787
2788
2789
2790
2791
2792
2793
2794
2795
2796
2797
2798
2799
2800
2801
# File 'lib/logstash/patches/resolv_9270.rb', line 2784

def to_s # :nodoc:
  c = @coordinates.unpack("N").join.to_i
  val      = (c - (2**31)).abs
  fracsecs = (val % 1e3).to_i.to_s
  val      = val / 1e3
  secs     = (val % 60).to_i.to_s
  val      = val / 60
  mins     = (val % 60).to_i.to_s
  degs     = (val / 60).to_i.to_s
  posi = (c >= 2**31)
  case posi
  when true
    hemi = @orientation[/^lat$/] ? "N" : "E"
  else
    hemi = @orientation[/^lon$/] ? "W" : "S"
  end
  return degs << " " << mins << " " << secs << "." << fracsecs << " " << hemi
end