Class: Terraformer::LineString

Inherits:
Geometry show all
Defined in:
lib/terraformer/line_string.rb

Constant Summary

Constants inherited from Geometry

Geometry::MULTI_REGEX

Instance Attribute Summary

Attributes inherited from Geometry

#coordinates, #crs

Instance Method Summary collapse

Methods inherited from Geometry

#==, #convex_hull, #each_coordinate, #geographic?, #get, #intersects?, iter_coordinate, #map_coordinate, #mercator?, #to_feature, #to_geographic, #to_hash, #to_mercator

Methods included from Geometry::ClassMethods

#arrays_intersect_arrays?, #coordinates_contain_point?, #edge_intersects_edge?, #line_contains_point?

Methods inherited from Primitive

#bbox, #envelope, #to_json, #type

Constructor Details

#initialize(*args) ⇒ LineString

Returns a new instance of LineString.



5
6
7
8
9
10
11
12
13
# File 'lib/terraformer/line_string.rb', line 5

def initialize *args
  super *args

  # must be an array of coordinates
  unless Array === coordinates &&
         Terraformer::Coordinate === coordinates[0]
    raise ArgumentError.new 'invalid coordinates for Terraformer::LineString'
  end
end

Instance Method Details

#add_vertex(p) ⇒ Object Also known as: <<

Raises:

  • (ArgumentError)


71
72
73
74
75
# File 'lib/terraformer/line_string.rb', line 71

def add_vertex p
  p = p.coordinates if Point === p
  raise ArgumentError unless Coordinate === p
  coordinates << p
end

#contains?(obj) ⇒ Boolean

Returns:

  • (Boolean)


29
30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/terraformer/line_string.rb', line 29

def contains? obj
  case obj
  when Point
    lines.any? {|l| Geometry.line_contains_point? l, obj.coordinates}
  when LineString
    self == obj or coordinates.slice_exists? obj.coordinates
    # todo this does not case for a line string of different coordinates
    #      that is actually contained yet
  when MultiLineString
    obj.line_strings.all? {|ls| ls.within? self}
  else
    raise ArgumentError.new "unsupported type: #{obj.type rescue obj.class}"
  end
end

#first_coordinateObject



15
16
17
# File 'lib/terraformer/line_string.rb', line 15

def first_coordinate
  coordinates[0]
end

#insert_vertex(idx, p) ⇒ Object

Raises:

  • (ArgumentError)


78
79
80
81
82
# File 'lib/terraformer/line_string.rb', line 78

def insert_vertex idx, p
  p = p.coordinates if Point === p
  raise ArgumentError unless Coordinate === p
  coordinates.insert idx, p
end

#linear_ring?Boolean

Returns:

  • (Boolean)


19
20
21
# File 'lib/terraformer/line_string.rb', line 19

def linear_ring?
  coordinates.length > 3 and coordinates.first == coordinates.last
end

#linesObject



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

def lines
  ls = []
  coordinates.each_cons(2) {|l| ls << l}
  ls
end

#point_at(idx) ⇒ Object Also known as: vertex_at



66
67
68
# File 'lib/terraformer/line_string.rb', line 66

def point_at idx
  coordinates[idx].to_point
end

#pointsObject Also known as: vertices



61
62
63
# File 'lib/terraformer/line_string.rb', line 61

def points
  coordinates.map &:to_point
end

#remove_vertex(p) ⇒ Object

Raises:

  • (ArgumentError)


84
85
86
87
88
# File 'lib/terraformer/line_string.rb', line 84

def remove_vertex p
  p = p.coordinates if Point === p
  raise ArgumentError unless Coordinate === p
  coordinates.delete p
end

#remove_vertex_at(idx) ⇒ Object



90
91
92
# File 'lib/terraformer/line_string.rb', line 90

def remove_vertex_at idx
  coordinates.delete_at idx
end

#within?(obj) ⇒ Boolean

Returns:

  • (Boolean)


44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/terraformer/line_string.rb', line 44

def within? obj
  case obj
  when LineString
    self == obj or obj.coordinates.slice_exists? coordinates
    # todo this does not case for a line string of different coordinates
    #      that is actually contained yet
  when MultiLineString
    obj.line_strings.any? {|ls| ls.contains? self}
  when Polygon
    obj.contains? self
  when MultiPolygon
    obj.polygons.any? {|p| p.contains? self}
  else
    raise ArgumentError.new "unsupported type: #{obj.type rescue obj.class}"
  end
end