Class: RubyVolt::Meta::Geography::Point

Inherits:
Object
  • Object
show all
Defined in:
lib/ruby_volt/meta/geography.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(lng, lat) ⇒ Point

Returns a new instance of Point.

Raises:

  • (::ArgumentError)


20
21
22
23
24
25
# File 'lib/ruby_volt/meta/geography.rb', line 20

def initialize(lng, lat)
  raise(::ArgumentError, "Longitude must be in the range -180 ≤ longitude ≤ 180") unless lng >= -180 && lng <= 180
  raise(::ArgumentError, "Latitude must be in the range -90 ≤ latitude ≤ 90") unless lat >= -90 && lat <= 90
  @lng = lng
  @lat = lat
end

Instance Attribute Details

#latObject (readonly)

Returns the value of attribute lat.



5
6
7
# File 'lib/ruby_volt/meta/geography.rb', line 5

def lat
  @lat
end

#lngObject (readonly)

Returns the value of attribute lng.



5
6
7
# File 'lib/ruby_volt/meta/geography.rb', line 5

def lng
  @lng
end

Class Method Details

.from_XYZPoint(x, y, z) ⇒ Object



10
11
12
13
14
15
16
17
# File 'lib/ruby_volt/meta/geography.rb', line 10

def from_XYZPoint(x, y, z)
  degreesPerRadian = 180.0/Math::PI
  lngRadians = Math.atan2(y, x)
  latRadians = Math.atan2(z, Math.sqrt(x * x + y * y))
  lng = (lngRadians * degreesPerRadian).round(6)
  lat = (latRadians * degreesPerRadian).round(6)
  new(lng, lat)
end

Instance Method Details

#==(other) ⇒ Object



50
51
52
# File 'lib/ruby_volt/meta/geography.rb', line 50

def ==(other)
  other.is_a?(Point) && (lng == other.lng) && (lat == other.lat)
end

#inspectObject



38
39
40
# File 'lib/ruby_volt/meta/geography.rb', line 38

def inspect
  to_wkt
end

#to_sObject



42
43
44
# File 'lib/ruby_volt/meta/geography.rb', line 42

def to_s
  "#{lng} #{lat}"
end

#to_wktObject



46
47
48
# File 'lib/ruby_volt/meta/geography.rb', line 46

def to_wkt
  "POINT(#{to_s})"
end

#to_XYZPointObject



27
28
29
30
31
32
33
34
35
36
# File 'lib/ruby_volt/meta/geography.rb', line 27

def to_XYZPoint
  radiansPerDegree = (Math::PI/180.0) # A conversion factor.
  latRadians = lat * radiansPerDegree # latitude is in degrees.
  lngRadians = lng * radiansPerDegree # longitude is in degrees.
  cosPhi = Math.cos(latRadians)
  x = Math.cos(lngRadians) * cosPhi
  y = Math.sin(lngRadians) * cosPhi
  z = Math.sin(latRadians)
  [x, y, z]
end