Class: Resolv::LOC::Coord

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

Overview

A Resolv::LOC::Coord

Constant Summary collapse

Regex =

Regular expression LOC Coord must match.

/\A0*(\d{1,3})\s([0-5]?\d)\s([0-5]?\d(?:\.\d+)?)\s([NESW])\z/
Bias =

Bias for the equator/prime meridian, in thousandths of a second of arc.

1 << 31

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(coordinates, orientation) ⇒ Coord

Internal use; use self.create.



3548
3549
3550
3551
3552
3553
3554
3555
3556
3557
# File 'lib/resolv.rb', line 3548

def initialize(coordinates,orientation)
  unless coordinates.kind_of?(String) and coordinates.bytesize == 4
    raise ArgumentError.new("Coord must be a 32bit unsigned integer in hex format: #{coordinates.inspect}")
  end
  unless orientation == "lon" || orientation == "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

#coordinates ⇒ Object (readonly)

The raw coordinates



3562
3563
3564
# File 'lib/resolv.rb', line 3562

def coordinates
  @coordinates
end

#orientation ⇒ Object (readonly)

The orientation of the hemisphere as 'lat' or 'lon'



3566
3567
3568
# File 'lib/resolv.rb', line 3566

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



3524
3525
3526
3527
3528
3529
3530
3531
3532
3533
3534
3535
3536
3537
3538
3539
3540
3541
3542
3543
3544
3545
# File 'lib/resolv.rb', line 3524

def self.create(arg)
  case arg
  when Coord
    return arg
  when String
    unless m = Regex.match(arg)
      raise ArgumentError.new("not a properly formed Coord string: " + arg)
    end

    arc = (m[1].to_i * 3_600_000) + (m[2].to_i * 60_000) + (m[3].to_f * 1_000).to_i
    dir = m[4]
    lat = dir[/[NS]/]
    unless arc <= (lat ? 324_000_000 : 648_000_000) # (lat ? 90 : 180) * 3_600_000
      raise ArgumentError.new("out of range as Coord: #{arg}")
    end

    hemi = dir[/[NE]/] ? 1 : -1
    return new([arc * hemi + Bias].pack("N"), lat ? "lat" : "lon")
  else
    raise ArgumentError.new("cannot interpret as Coord: #{arg.inspect}")
  end
end

Instance Method Details

#==(other) ⇒ Object

:nodoc:



3586
3587
3588
# File 'lib/resolv.rb', line 3586

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

#eql?(other) ⇒ Boolean

:nodoc:

Returns:

  • (Boolean)


3590
3591
3592
# File 'lib/resolv.rb', line 3590

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

#hash ⇒ Object

:nodoc:



3594
3595
3596
# File 'lib/resolv.rb', line 3594

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

#inspect ⇒ Object

:nodoc:



3582
3583
3584
# File 'lib/resolv.rb', line 3582

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

#to_s ⇒ Object

:nodoc:



3568
3569
3570
3571
3572
3573
3574
3575
3576
3577
3578
3579
3580
# File 'lib/resolv.rb', line 3568

def to_s # :nodoc:
    c, = @coordinates.unpack("N")
    val = (c -= Bias).abs
    val, fracsecs = val.divmod(1000)
    val, secs     = val.divmod(60)
    degs, mins    = val.divmod(60)
    hemi = if c.negative?
      @orientation == "lon" ? "W" : "S"
    else
      @orientation == "lat" ? "N" : "E"
    end
    format("%d %02d %02d.%03d %s", degs, mins, secs, fracsecs, hemi)
end