Class: ActiveRoad::Path

Inherits:
Object
  • Object
show all
Defined in:
app/models/active_road/path.rb

Overview

A path is a link between differents objects :

- Departure
- Arrival

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(attributes = {}) ⇒ Path

Returns a new instance of Path.



9
10
11
12
13
# File 'app/models/active_road/path.rb', line 9

def initialize(attributes = {})
  attributes.each do |k, v|
    send("#{k}=", v)
  end
end

Instance Attribute Details

#arrivalObject

Returns the value of attribute arrival.



6
7
8
# File 'app/models/active_road/path.rb', line 6

def arrival
  @arrival
end

#departureObject

Returns the value of attribute departure.



6
7
8
# File 'app/models/active_road/path.rb', line 6

def departure
  @departure
end

#physical_roadObject Also known as: road

Returns the value of attribute physical_road.



6
7
8
# File 'app/models/active_road/path.rb', line 6

def physical_road
  @physical_road
end

Class Method Details

.all(departure, arrivals, physical_road) ⇒ Object



40
41
42
43
44
# File 'app/models/active_road/path.rb', line 40

def self.all(departure, arrivals, physical_road)
  Array(arrivals).collect do |arrival|
    new :departure => departure, :arrival => arrival, :physical_road => physical_road
  end
end

Instance Method Details

#==(other) ⇒ Object Also known as: eql?



105
106
107
108
109
# File 'app/models/active_road/path.rb', line 105

def ==(other)
  [:departure, :arrival, :physical_road].all? do |attribute|
    other.respond_to?(attribute) and send(attribute) == other.send(attribute)
  end
end

#geometry_with_cacheObject Also known as: geometry



95
96
97
# File 'app/models/active_road/path.rb', line 95

def geometry_with_cache
  @geometry ||= geometry_without_cache
end

#geometry_without_cacheObject



56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
# File 'app/models/active_road/path.rb', line 56

def geometry_without_cache
  # sorted_locations_on_road = locations_on_road.sort
  # reverse = (sorted_locations_on_road != locations_on_road)
  # geometry = road.line_substring(sorted_locations_on_road.first, sorted_locations_on_road.last)
  # geometry = geometry.reverse if reverse
  # geometry

  points =  physical_road.geometry.points
  points_selected = []

  if ActiveRoad::AccessPoint === departure || ActiveRoad::AccessPoint === arrival
    sorted_locations_on_road = locations_on_road.sort    
    reverse = (sorted_locations_on_road != locations_on_road)
    
    if sorted_locations_on_road == [0, 1]
      if reverse
        return physical_road.geometry.reverse
      else
        return physical_road.geometry
      end
    end
    
    return road.line_substring(sorted_locations_on_road.first, sorted_locations_on_road.last)
  end

  departure_index = points.index(departure.geometry)
  arrival_index = points.index(arrival.geometry)
  
  if departure_index < arrival_index
    points_selected = points[arrival_index..departure_index]
  elsif arrival_index < departure_index
    points_selected = points.reverse![arrival_index..departure_index]
  else
    raise StandardError, "Junction is not on the physical road"
  end

  GeoRuby::SimpleFeatures::LineString.from_points(points)
end

#hashObject



112
113
114
# File 'app/models/active_road/path.rb', line 112

def hash
  [departure, arrival, physical_road].hash
end

#length_in_meterObject



31
32
33
# File 'app/models/active_road/path.rb', line 31

def length_in_meter
  length_on_road * road.length_in_meter
end

#length_on_roadObject



35
36
37
38
# File 'app/models/active_road/path.rb', line 35

def length_on_road
  begin_on_road, end_on_road = locations_on_road.sort
  end_on_road - begin_on_road
end

#locations_on_roadObject



19
20
21
22
23
24
25
26
27
28
29
# File 'app/models/active_road/path.rb', line 19

def locations_on_road
  [departure, arrival].collect do |endpoint|
    location =
      if ActiveRoad::AccessPoint === endpoint
        road.locate_point endpoint.geometry
      else
        endpoint.location_on_road road
      end
    location = [0, [location, 1].min].max
  end
end

#nameObject



15
16
17
# File 'app/models/active_road/path.rb', line 15

def name
  "Path : #{departure} -> #{arrival}"
end

#pathsObject



48
49
50
# File 'app/models/active_road/path.rb', line 48

def paths
  arrival.paths - [reverse]
end

#reverseObject



52
53
54
# File 'app/models/active_road/path.rb', line 52

def reverse
  self.class.new :departure => arrival, :arrival => departure, :physical_road => physical_road
end

#to_sObject



101
102
103
# File 'app/models/active_road/path.rb', line 101

def to_s
  name
end