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

Inherits:
Object
  • Object
show all
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.



169
170
171
172
173
174
175
176
177
178
179
# File 'lib/georuby-ext/georuby/locators.rb', line 169

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.



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

def segment
  @segment
end

#targetObject (readonly)

Returns the value of attribute target.



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

def target
  @target
end

Instance Method Details

#angleObject



209
210
211
# File 'lib/georuby-ext/georuby/locators.rb', line 209

def angle
  acos cos_angle
end

#cos_angleObject



217
218
219
# File 'lib/georuby-ext/georuby/locators.rb', line 217

def cos_angle
  [-1, [1, scalar_product / segment_distance / target_distance_from_departure].min].max
end

#distance_from_segmentObject



181
182
183
184
185
# File 'lib/georuby-ext/georuby/locators.rb', line 181

def distance_from_segment
  return 0 if [segment.departure, segment.arrival].include?(target)
    
  sin_angle * target_distance_from_departure
end

#distance_on_segmentObject



187
188
189
190
191
# File 'lib/georuby-ext/georuby/locators.rb', line 187

def distance_on_segment
  # like cos_angle * target_distance_from_departure
  # scalar_product / square_of_segment_distance
  scalar_product / segment_distance
end

#locate_pointObject



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

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



201
202
203
204
205
206
207
# File 'lib/georuby-ext/georuby/locators.rb', line 201

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



225
226
227
# File 'lib/georuby-ext/georuby/locators.rb', line 225

def segment_distance
  segment.distance
end

#sin_angleObject



213
214
215
# File 'lib/georuby-ext/georuby/locators.rb', line 213

def sin_angle
  sin angle
end

#square_of_segment_distanceObject



221
222
223
# File 'lib/georuby-ext/georuby/locators.rb', line 221

def square_of_segment_distance
  segment_distance ** 2
end

#target_distance_from_departureObject



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

def target_distance_from_departure
  departure.spherical_distance target
end