Class: Osgb::Ellipsoid

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

Constant Summary collapse

@@instances =
{}

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name, a, b) ⇒ Ellipsoid

Returns a new instance of Ellipsoid.



6
7
8
9
10
11
# File 'lib/osgb/ellipsoid.rb', line 6

def initialize(name, a, b)
  @name = name
  @a = a
  @b = b
  @@instances[name] = self
end

Instance Attribute Details

#aObject

Returns the value of attribute a.



3
4
5
# File 'lib/osgb/ellipsoid.rb', line 3

def a
  @a
end

#bObject

Returns the value of attribute b.



3
4
5
# File 'lib/osgb/ellipsoid.rb', line 3

def b
  @b
end

#nameObject

Returns the value of attribute name.



3
4
5
# File 'lib/osgb/ellipsoid.rb', line 3

def name
  @name
end

Class Method Details

.[](name) ⇒ Object



52
53
54
# File 'lib/osgb/ellipsoid.rb', line 52

def self.[](name)
  @@instances[name]
end

Instance Method Details

#cartesian_to_polar(x, y, z) ⇒ Object



34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/osgb/ellipsoid.rb', line 34

def cartesian_to_polar(x,y,z)
  p = Math.sqrt(x**2 + y**2)
  phi = Math.atan2(z, p*(1-ecc));
  phip = 2 * Math::PI

  count = 0
  while (phi-phip).abs > precision do
    raise RuntimeError "Helmert transformation has not converged. Discrepancy after #{count} cycles is #{phi-phip}" if count >= Osgb::Gridref.iteration_ceiling
    nu = a / Math.sqrt(1 - ecc * Math.sin(phi)**2)
    phip = phi
    phi = Math.atan2(z + ecc * nu * Math.sin(phi), p)
    count += 1
  end 

  lambda = Math.atan2(y, x)
  [phi, lambda]
end

#eccObject



13
14
15
# File 'lib/osgb/ellipsoid.rb', line 13

def ecc
  (a**2 - b**2) / (a**2)
end

#nu_for(phi) ⇒ Object



17
18
19
# File 'lib/osgb/ellipsoid.rb', line 17

def nu_for(phi)
  a / (Math.sqrt(1 - ecc * Math.sin(phi)**2))
end

#polar_to_cartesian(phi, lambda) ⇒ Object



25
26
27
28
29
30
31
32
# File 'lib/osgb/ellipsoid.rb', line 25

def polar_to_cartesian(phi, lambda)
  h = 0
  nu = nu_for(phi)
  x1 = (nu + h) * Math.cos(phi) * Math.cos(lambda)
  y1 = (nu + h) * Math.cos(phi) * Math.sin(lambda)
  z1 = ((1 - ecc) * nu + h) * Math.sin(phi)
  [x1, y1, z1]
end

#precisionObject



21
22
23
# File 'lib/osgb/ellipsoid.rb', line 21

def precision
  4 / a
end