Module: GeoDirection

Included in:
DirectionVector
Defined in:
lib/geo_vectors/util/geo_direction.rb

Instance Method Summary collapse

Instance Method Details

#bearing_to_dir(brng) ⇒ Object



24
25
26
27
28
# File 'lib/geo_vectors/util/geo_direction.rb', line 24

def bearing_to_dir brng
  dir = bearing_to_dir_map[:"_#{brng}"]
  raise "No direction could be for the bearing #{brng}" if !dir
  dir
end

#dir_to_bearing(dir) ⇒ Object



18
19
20
21
22
# File 'lib/geo_vectors/util/geo_direction.rb', line 18

def dir_to_bearing dir
  dir = dir_to_bearing_map[dir]
  raise "No bearing for direction #{dir} could be found" if !dir
  dir
end

#directionsObject



6
7
8
# File 'lib/geo_vectors/util/geo_direction.rb', line 6

def directions
  [:N, :S, :E, :W, :NW, :NE, :SW, :SE]
end

#get_valid_direction(dir) ⇒ Object



30
31
32
33
34
# File 'lib/geo_vectors/util/geo_direction.rb', line 30

def get_valid_direction dir
  return dir_map[dir] if dir_map[dir]
  raise "Not a valid direction: #{dir}" if !directions.include? dir
  dir
end

#random_directionObject



10
11
12
# File 'lib/geo_vectors/util/geo_direction.rb', line 10

def random_direction
  directions[rand(directions.size)]
end

#to_bearing_vector(direction, distance) ⇒ Object



36
37
38
39
# File 'lib/geo_vectors/util/geo_direction.rb', line 36

def to_bearing_vector direction, distance
  distance, direction = [direction, distance] if direction.kind_of?(GeoDistance) || distance.kind_of?(Symbol)
  BearingVector.new distance, dir_to_bearing(direction)
end

#to_point_vector(direction, distance) ⇒ Object



41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
# File 'lib/geo_vectors/util/geo_direction.rb', line 41

def to_point_vector direction, distance
  # dist degrees
  dd = distance.to_deg
  
  lng, lat = case direction
  when :N
    [0, -dd]
  when :S
    [0, dd]
  when :W
    [-dd, 0]
  when :E
    [dd, 0]
  when :NW
    [dd, b]
  when :SW
    [dd, -dd]
  when :NE
    [-dd, dd]
  when :SE
    [-dd, -dd]
  end
  [lat, lng].vector
end

#valid_direction?(dir) ⇒ Boolean

Returns:

  • (Boolean)


14
15
16
# File 'lib/geo_vectors/util/geo_direction.rb', line 14

def valid_direction? dir
  valid_directions.include? dir
end

#valid_directionsObject



2
3
4
# File 'lib/geo_vectors/util/geo_direction.rb', line 2

def valid_directions
  [:north, :south, :east, :west, :N, :S, :E, :W, :NW, :NE, :SW, :SE]
end