Class: GeoRuby::SimpleFeatures::LineString

Inherits:
Object
  • Object
show all
Defined in:
lib/georuby-ext/georuby/line_string.rb,
lib/georuby-ext/georuby/locators.rb

Defined Under Namespace

Classes: PointLocator, Segment

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.merge(lines) ⇒ Object



20
21
22
23
24
25
26
# File 'lib/georuby-ext/georuby/line_string.rb', line 20

def self.merge(lines)
  # FIXME flatten.uniq can break crossing lines
  merged_points = lines.map(&:points).flatten.uniq
  if merged_points.size > 1
    from_points merged_points, srid!(lines), lines.first.with_z, lines.first.with_m
  end
end

Instance Method Details

#==(other) ⇒ Object



36
37
38
# File 'lib/georuby-ext/georuby/line_string.rb', line 36

def ==(other)
  other.respond_to?(:points) and points == other.points
end

#change(options) ⇒ Object



3
4
5
6
7
8
9
# File 'lib/georuby-ext/georuby/line_string.rb', line 3

def change(options)
  self.class.from_points(options[:points] || points, 
                         options[:srid] || srid,
                         options[:with_z] || with_z, 
                         options[:with_m] || with_m)
  # or instead of || requires parenthesis
end

#close!Object



40
41
42
43
# File 'lib/georuby-ext/georuby/line_string.rb', line 40

def close!
  points << points.first unless closed?
  self
end

#distance_from_line(target) ⇒ Object



66
67
68
# File 'lib/georuby-ext/georuby/locators.rb', line 66

def distance_from_line(target)
  nearest_locator(target).distance_from_segment
end

#distance_on_line(target) ⇒ Object



61
62
63
64
# File 'lib/georuby-ext/georuby/locators.rb', line 61

def distance_on_line(target)
  nearest_locator = nearest_locator(target)
  nearest_locator.distance_on_segment + nearest_locator.segment.line_distance_at_departure
end

#interpolate_point(location) ⇒ Object



103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
# File 'lib/georuby-ext/georuby/locators.rb', line 103

def interpolate_point(location)
  return points.last if location >= 1
  return points.first if location <= 0

  distance_on_line = location * spherical_distance

  segment = segments.find do |segment|
    segment.line_distance_at_arrival > distance_on_line
  end

  location_on_segment =
    (distance_on_line - segment.line_distance_at_departure) / segment.distance

  segment.interpolate_point location_on_segment
end

#locate_point(target) ⇒ Object



57
58
59
# File 'lib/georuby-ext/georuby/locators.rb', line 57

def locate_point(target)
  distance_on_line(target) / spherical_distance
end

#locators(point) ⇒ Object



74
75
76
# File 'lib/georuby-ext/georuby/locators.rb', line 74

def locators(point)
  segments.collect { |segment| segment.locator(point) }
end

#nearest_locator(target) ⇒ Object



70
71
72
# File 'lib/georuby-ext/georuby/locators.rb', line 70

def nearest_locator(target)
  locators(target).min_by(&:distance_from_segment)
end

#project_to(target_srid) ⇒ Object



15
16
17
18
# File 'lib/georuby-ext/georuby/line_string.rb', line 15

def project_to(target_srid)
  return self if srid == target_srid
  change :points => points.map { |point| point.project_to(target_srid) }, :srid => target_srid
end

#reverseObject



11
12
13
# File 'lib/georuby-ext/georuby/line_string.rb', line 11

def reverse
  change :points => points.reverse
end

#segments_with_cacheObject Also known as: segments



98
99
100
# File 'lib/georuby-ext/georuby/locators.rb', line 98

def segments_with_cache
  @segments ||= segments_without_cache
end

#segments_without_cacheObject



78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
# File 'lib/georuby-ext/georuby/locators.rb', line 78

def segments_without_cache
  previous_point = nil
  distance_from_departure = 0

  
  points.inject([]) do |segments, point|
    Segment.new(previous_point, point).tap do |segment|
      segment.line = self
      segment.line_distance_at_departure = distance_from_departure

      distance_from_departure += segment.distance
      
      segments << segment
    end if previous_point
    
    previous_point = point
    segments
  end
end

#side_countObject



32
33
34
# File 'lib/georuby-ext/georuby/line_string.rb', line 32

def side_count
  size - 1
end

#to_kmlObject



49
50
51
# File 'lib/georuby-ext/georuby/line_string.rb', line 49

def to_kml
  GeoRuby::SimpleFeatures::Geometry.to_kml self
end

#to_rgeoObject



28
29
30
# File 'lib/georuby-ext/georuby/line_string.rb', line 28

def to_rgeo
 rgeo_factory.line_string(points.collect(&:to_rgeo))
end

#to_ringObject



45
46
47
# File 'lib/georuby-ext/georuby/line_string.rb', line 45

def to_ring
  GeoRuby::SimpleFeatures::LinearRing.from_points points, srid, with_z, with_m
end