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



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

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



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

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

#change(options) ⇒ Object



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

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



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

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

#is_closedObject Also known as: closed?



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

def is_closed
      #a bit naive...
    points.first == points.last
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



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

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



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

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



38
39
40
# File 'lib/georuby-ext/georuby/line_string.rb', line 38

def side_count
  size - 1
end

#to_kmlObject



55
56
57
# File 'lib/georuby-ext/georuby/line_string.rb', line 55

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

#to_rgeoObject



34
35
36
# File 'lib/georuby-ext/georuby/line_string.rb', line 34

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

#to_ringObject



51
52
53
# File 'lib/georuby-ext/georuby/line_string.rb', line 51

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