Class: GeoRuby::SimpleFeatures::LineString::PointLocator
- Inherits:
-
Object
- Object
- GeoRuby::SimpleFeatures::LineString::PointLocator
- Extended by:
- ActiveSupport::Memoizable
- Includes:
- Math
- Defined in:
- lib/georuby-ext/georuby/locators.rb
Instance Attribute Summary collapse
-
#segment ⇒ Object
readonly
Returns the value of attribute segment.
-
#target ⇒ Object
readonly
Returns the value of attribute target.
Instance Method Summary collapse
- #angle ⇒ Object
- #cos_angle ⇒ Object
- #distance_from_segment ⇒ Object
- #distance_on_segment ⇒ Object
-
#initialize(target, segment_or_departure, arrival = nil) ⇒ PointLocator
constructor
A new instance of PointLocator.
- #locate_point ⇒ Object
-
#scalar_product ⇒ Object
def scalar_product (target.x-departure.x)*(arrival.x-departure.x) + (target.y-departure.y)*(arrival.y-departure.y).to_f end.
- #segment_distance ⇒ Object
- #sin_angle ⇒ Object
- #square_of_segment_distance ⇒ Object
- #target_distance_from_departure ⇒ Object
Constructor Details
#initialize(target, segment_or_departure, arrival = nil) ⇒ PointLocator
Returns a new instance of PointLocator.
170 171 172 173 174 175 176 177 178 179 180 |
# File 'lib/georuby-ext/georuby/locators.rb', line 170 def initialize(target, segment_or_departure, arrival = nil) @segment = if arrival Segment.new(segment_or_departure, arrival) else segment_or_departure end @target = target raise "Target is not defined" unless target end |
Instance Attribute Details
#segment ⇒ Object (readonly)
Returns the value of attribute segment.
167 168 169 |
# File 'lib/georuby-ext/georuby/locators.rb', line 167 def segment @segment end |
#target ⇒ Object (readonly)
Returns the value of attribute target.
167 168 169 |
# File 'lib/georuby-ext/georuby/locators.rb', line 167 def target @target end |
Instance Method Details
#angle ⇒ Object
211 212 213 |
# File 'lib/georuby-ext/georuby/locators.rb', line 211 def angle acos cos_angle end |
#cos_angle ⇒ Object
219 220 221 |
# File 'lib/georuby-ext/georuby/locators.rb', line 219 def cos_angle [-1, [1, scalar_product / segment_distance / target_distance_from_departure].min].max end |
#distance_from_segment ⇒ Object
182 183 184 185 186 |
# File 'lib/georuby-ext/georuby/locators.rb', line 182 def distance_from_segment return 0 if [segment.departure, segment.arrival].include?(target) sin_angle * target_distance_from_departure end |
#distance_on_segment ⇒ Object
188 189 190 191 192 |
# File 'lib/georuby-ext/georuby/locators.rb', line 188 def distance_on_segment # like cos_angle * target_distance_from_departure # scalar_product / square_of_segment_distance scalar_product / segment_distance end |
#locate_point ⇒ Object
194 195 196 |
# File 'lib/georuby-ext/georuby/locators.rb', line 194 def locate_point scalar_product / square_of_segment_distance end |
#scalar_product ⇒ Object
def scalar_product
(target.x-departure.x)*(arrival.x-departure.x) + (target.y-departure.y)*(arrival.y-departure.y).to_f
end
202 203 204 205 206 207 208 |
# File 'lib/georuby-ext/georuby/locators.rb', line 202 def scalar_product departure_target_metric_delta = departure.metric_delta(target) departure_arrival_metric_delta = departure.metric_delta(arrival) departure_target_metric_delta[0]*departure_arrival_metric_delta[0] + departure_target_metric_delta[1]*departure_arrival_metric_delta[1] end |
#segment_distance ⇒ Object
228 229 230 |
# File 'lib/georuby-ext/georuby/locators.rb', line 228 def segment_distance segment.distance end |
#sin_angle ⇒ Object
215 216 217 |
# File 'lib/georuby-ext/georuby/locators.rb', line 215 def sin_angle sin angle end |
#square_of_segment_distance ⇒ Object
223 224 225 |
# File 'lib/georuby-ext/georuby/locators.rb', line 223 def square_of_segment_distance segment_distance ** 2 end |
#target_distance_from_departure ⇒ Object
232 233 234 |
# File 'lib/georuby-ext/georuby/locators.rb', line 232 def target_distance_from_departure departure.spherical_distance target end |