Class: GeoRuby::SimpleFeatures::LineString::PointLocator
- Inherits:
-
Object
- Object
- GeoRuby::SimpleFeatures::LineString::PointLocator
- 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.
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
#segment ⇒ Object (readonly)
Returns the value of attribute segment.
166 167 168 |
# File 'lib/georuby-ext/georuby/locators.rb', line 166 def segment @segment end |
#target ⇒ Object (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
#angle ⇒ Object
209 210 211 |
# File 'lib/georuby-ext/georuby/locators.rb', line 209 def angle acos cos_angle end |
#cos_angle ⇒ Object
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_segment ⇒ Object
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_segment ⇒ Object
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_point ⇒ Object
193 194 195 |
# File 'lib/georuby-ext/georuby/locators.rb', line 193 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
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_distance ⇒ Object
225 226 227 |
# File 'lib/georuby-ext/georuby/locators.rb', line 225 def segment_distance segment.distance end |
#sin_angle ⇒ Object
213 214 215 |
# File 'lib/georuby-ext/georuby/locators.rb', line 213 def sin_angle sin angle end |
#square_of_segment_distance ⇒ Object
221 222 223 |
# File 'lib/georuby-ext/georuby/locators.rb', line 221 def square_of_segment_distance segment_distance ** 2 end |
#target_distance_from_departure ⇒ Object
229 230 231 |
# File 'lib/georuby-ext/georuby/locators.rb', line 229 def target_distance_from_departure departure.spherical_distance target end |