Class: Gem::Resolv::LOC::Coord

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

Overview

A Gem::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.



3302
3303
3304
3305
3306
3307
3308
3309
3310
3311
# File 'lib/rubygems/vendor/resolv/lib/resolv.rb', line 3302

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



3316
3317
3318
# File 'lib/rubygems/vendor/resolv/lib/resolv.rb', line 3316

def coordinates
  @coordinates
end

#orientationObject (readonly)

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



3320
3321
3322
# File 'lib/rubygems/vendor/resolv/lib/resolv.rb', line 3320

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



3281
3282
3283
3284
3285
3286
3287
3288
3289
3290
3291
3292
3293
3294
3295
3296
3297
3298
3299
3300
# File 'lib/rubygems/vendor/resolv/lib/resolv.rb', line 3281

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:



3345
3346
3347
# File 'lib/rubygems/vendor/resolv/lib/resolv.rb', line 3345

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

#eql?(other) ⇒ Boolean

:nodoc:

Returns:

  • (Boolean)


3349
3350
3351
# File 'lib/rubygems/vendor/resolv/lib/resolv.rb', line 3349

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

#hashObject

:nodoc:



3353
3354
3355
# File 'lib/rubygems/vendor/resolv/lib/resolv.rb', line 3353

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

#inspectObject

:nodoc:



3341
3342
3343
# File 'lib/rubygems/vendor/resolv/lib/resolv.rb', line 3341

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

#to_sObject

:nodoc:



3322
3323
3324
3325
3326
3327
3328
3329
3330
3331
3332
3333
3334
3335
3336
3337
3338
3339
# File 'lib/rubygems/vendor/resolv/lib/resolv.rb', line 3322

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