Class: GeoRuby::SimpleFeatures::LineString::PointLocator

Inherits:
Object
  • Object
show all
Extended by:
ActiveSupport::Memoizable
Includes:
Math
Defined in:
lib/georuby-ext/georuby/locators.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

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

#segmentObject (readonly)

Returns the value of attribute segment.



167
168
169
# File 'lib/georuby-ext/georuby/locators.rb', line 167

def segment
  @segment
end

#targetObject (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

#angleObject



211
212
213
# File 'lib/georuby-ext/georuby/locators.rb', line 211

def angle
  acos cos_angle
end

#cos_angleObject



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_segmentObject



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_segmentObject



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_pointObject



194
195
196
# File 'lib/georuby-ext/georuby/locators.rb', line 194

def locate_point
  scalar_product / square_of_segment_distance
end

#scalar_productObject

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_distanceObject



228
229
230
# File 'lib/georuby-ext/georuby/locators.rb', line 228

def segment_distance
  segment.distance
end

#sin_angleObject



215
216
217
# File 'lib/georuby-ext/georuby/locators.rb', line 215

def sin_angle
  sin angle
end

#square_of_segment_distanceObject



223
224
225
# File 'lib/georuby-ext/georuby/locators.rb', line 223

def square_of_segment_distance
  segment_distance ** 2
end

#target_distance_from_departureObject



232
233
234
# File 'lib/georuby-ext/georuby/locators.rb', line 232

def target_distance_from_departure
  departure.spherical_distance target
end