Class: MapWKT::Geometry::LineString

Inherits:
MapWKT::Geometry show all
Defined in:
lib/mapwkt/wkt/line_string.rb

Instance Method Summary collapse

Methods inherited from MapWKT::Geometry

parse_linestrings, parse_points, parse_polygons, parse_wkt, parse_x_y

Constructor Details

#initialize(*points) ⇒ LineString

Returns a new LineString containing the given Points.



42
43
44
45
# File 'lib/mapwkt/wkt/line_string.rb', line 42

def initialize (*points)
  @points = points
  self.refresh!
end

Instance Method Details

#centerObject

Returns a Point midway between the N/S- & E/W-most Points in the LineString.



5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
# File 'lib/mapwkt/wkt/line_string.rb', line 5

def center
  return if self.points.empty?
  n, s, e, w = nil
  
  self.points.each do |p|
    n = p.y if !n || p.y > n
    s = p.y if !s || p.y < s
    e = p.x if !e || p.x > e
    w = p.x if !w || p.x < w
  end
  
  x = (e + w) / 2
  y = (n + s) / 2
  
  Point.new(y, x)
end

#close!Object

Returns this LineString after closing its endpoints.



23
24
25
26
27
# File 'lib/mapwkt/wkt/line_string.rb', line 23

def close!
  self.refresh! unless @refreshed
  @closed = true
  self
end

#closed?Boolean

Returns true if this LineString is closed, false otherwise.

Returns:

  • (Boolean)


30
31
32
33
# File 'lib/mapwkt/wkt/line_string.rb', line 30

def closed?
  self.refresh!
  @closed ||= false
end

#dupObject

Returns a new LineString with the same geographic Points as this LineString.



36
37
38
39
# File 'lib/mapwkt/wkt/line_string.rb', line 36

def dup
  points = self.points.map(&:dup)
  LineString(*points).tap {|ls| ls.close! if self.closed? }
end

#open!Object

Returns this LineString after opening its endpoints.



53
54
55
56
57
# File 'lib/mapwkt/wkt/line_string.rb', line 53

def open!
  self.refresh!
  @closed = false
  self
end

#open?Boolean

Returns false if this LineString is closed, true otherwise.

Returns:

  • (Boolean)


48
49
50
# File 'lib/mapwkt/wkt/line_string.rb', line 48

def open?
  !self.closed?
end

#pointsObject

Returns the array of Points in this LineString.



60
61
62
63
# File 'lib/mapwkt/wkt/line_string.rb', line 60

def points
  self.refresh!
  @points
end

#refresh!Object

Removes any non-Point objects from this LineString’s #points array. If the first Point has been added to the end of the #points array, closes the LineString and removes the Point. Returns this LineString.



68
69
70
71
72
73
74
75
76
77
78
79
80
# File 'lib/mapwkt/wkt/line_string.rb', line 68

def refresh!
  return self if @refreshed
  @refreshed = true
  
  self.points.select! {|p| MapWKT::Geometry::Point === p }
  
  while (self.points.many?) && (self.points.last == self.points.first)
    self.points.pop
    @closed = true
  end
  
  self
end

#to_sObject

Returns a string representation of this LineString. ⤿ indicates an open LineString, while ⟲ indicates a closed LineString.



84
85
86
87
88
89
# File 'lib/mapwkt/wkt/line_string.rb', line 84

def to_s
  points = self.points.join(", ")
  marker = self.closed? ? ?⟲ : ?⤿
  
  "#{marker} #{points} #{marker} "
end

#wktObject

Returns this LineString’s WKT representation, with latitudes and longitudes rounded to 7 decimal places.



93
94
95
96
# File 'lib/mapwkt/wkt/line_string.rb', line 93

def wkt
  points = self.open? ? self.points : [*self.points, self.points.first]
  "LINESTRING(#{points.map {|p| "#{p.longitude_f} #{p.latitude_f}" }.join(", ")})"
end