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.

/^(\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

Internal use; use self.create.



3445
3446
3447
3448
3449
3450
3451
3452
3453
3454
# File 'lib/resolv.rb', line 3445

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



3459
3460
3461
# File 'lib/resolv.rb', line 3459

def coordinates
  @coordinates
end

#orientationObject (readonly)

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



3463
3464
3465
# File 'lib/resolv.rb', line 3463

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



3423
3424
3425
3426
3427
3428
3429
3430
3431
3432
3433
3434
3435
3436
3437
3438
3439
3440
3441
3442
# File 'lib/resolv.rb', line 3423

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:



3488
3489
3490
# File 'lib/resolv.rb', line 3488

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

#eql?(other) ⇒ Boolean

:nodoc:

Returns:

  • (Boolean)


3492
3493
3494
# File 'lib/resolv.rb', line 3492

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

#hashObject

:nodoc:



3496
3497
3498
# File 'lib/resolv.rb', line 3496

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

#inspectObject

:nodoc:



3484
3485
3486
# File 'lib/resolv.rb', line 3484

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

#to_sObject

:nodoc:



3465
3466
3467
3468
3469
3470
3471
3472
3473
3474
3475
3476
3477
3478
3479
3480
3481
3482
# File 'lib/resolv.rb', line 3465

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