Class: Geos::LineString

Inherits:
Geometry show all
Includes:
Enumerable
Defined in:
lib/ffi-geos/line_string.rb

Direct Known Subclasses

LinearRing

Constant Summary

Constants included from GeomTypes

GeomTypes::GEOS_GEOMETRYCOLLECTION, GeomTypes::GEOS_LINEARRING, GeomTypes::GEOS_LINESTRING, GeomTypes::GEOS_MULTILINESTRING, GeomTypes::GEOS_MULTIPOINT, GeomTypes::GEOS_MULTIPOLYGON, GeomTypes::GEOS_POINT, GeomTypes::GEOS_POLYGON

Instance Attribute Summary

Attributes inherited from Geometry

#ptr

Instance Method Summary collapse

Methods inherited from Geometry

#==, #area, #boundary, #buffer, #build_area, #centroid, #clip_by_rect, #constrained_delaunay_triangulation, #contains?, #convex_hull, #coord_seq, #coverage_union, #covered_by?, #covers?, #crosses?, #delaunay_triangulation, #difference, #dimensions, #disjoint?, #distance, #distance_indexed, #empty?, #end_point, #envelope, #eql?, #eql_almost?, #eql_exact?, #extract_unique_points, #frechet_distance, #geom_type, #has_z?, #hausdorff_distance, #initialize, #initialize_copy, #interpolate, #interpolate_normalized, #intersection, #intersects?, #largest_empty_circle, #length, #line_merge, #make_valid, #maximum_inscribed_circle, #minimum_bounding_circle, #minimum_clearance, #minimum_clearance_line, #minimum_rotated_rectangle, #minimum_width, #nearest_points, #node, #normalize!, #num_coordinates, #num_geometries, #overlaps?, #point_on_surface, #polygonize, #polygonize_cut_edges, #polygonize_full, #polygonize_valid, #precision, #project, #project_normalized, #relate, #relate_boundary_node_rule, #relate_pattern, release, #reverse, #ring?, #shared_paths, #simple?, #simplify, #snap, #srid, #srid=, #start_point, #sym_difference, #to_prepared, #to_s, #topology_preserve_simplify, #touches?, #type_id, #unary_union, #union, #union_cascaded, #valid?, #valid_detail, #valid_reason, #voronoi_diagram, #with_precision, #within?

Methods included from Tools

#bool_result, #bool_to_int, #cast_geometry_ptr, #check_enum_value, #check_geometry, #extract_options!, #pick_srid_according_to_policy, #pick_srid_from_geoms, #symbol_for_enum

Constructor Details

This class inherits a constructor from Geos::Geometry

Instance Method Details

#[](*args) ⇒ Object Also known as: slice



36
37
38
39
40
41
42
# File 'lib/ffi-geos/line_string.rb', line 36

def [](*args)
  if args.length == 1 && args.first.is_a?(Numeric) && args.first >= 0
    point_n(args.first)
  else
    to_a[*args]
  end
end

#closed?Boolean

Returns:

  • (Boolean)


62
63
64
# File 'lib/ffi-geos/line_string.rb', line 62

def closed?
  bool_result(FFIGeos.GEOSisClosed_r(Geos.current_handle_pointer, ptr))
end

#dump_points(cur_path = []) ⇒ Object



80
81
82
# File 'lib/ffi-geos/line_string.rb', line 80

def dump_points(cur_path = [])
  cur_path.concat(to_a)
end

#eachObject



7
8
9
10
11
12
13
14
15
16
17
18
# File 'lib/ffi-geos/line_string.rb', line 7

def each
  if block_given?
    num_points.times do |n|
      yield point_n(n)
    end
    self
  else
    num_points.times.collect { |n|
      point_n(n)
    }.to_enum
  end
end

#line_interpolate_point(fraction) ⇒ Object Also known as: interpolate_point

Raises:

  • (ArgumentError)


106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
# File 'lib/ffi-geos/line_string.rb', line 106

def line_interpolate_point(fraction)
  raise ArgumentError, 'fraction must be between 0 and 1' unless fraction.between?(0, 1)

  case fraction
    when 0
      start_point
    when 1
      end_point
    else
      length = self.length
      total_length = 0
      segs = num_points - 1

      segs.times do |i|
        p_1 = self[i]
        p_2 = self[i + 1]

        seg_length = p_1.distance(p_2) / length

        if fraction < total_length + seg_length
          dseg = (fraction - total_length) / seg_length

          args = []
          args << (p_1.x + ((p_2.x - p_1.x) * dseg))
          args << (p_1.y + ((p_2.y - p_1.y) * dseg))
          args << (p_1.z + ((p_2.z - p_1.z) * dseg)) if has_z?

          args << { srid: pick_srid_according_to_policy(srid) } unless srid.zero?

          return Geos.create_point(*args)
        end

        total_length += seg_length
      end

      # if all else fails...
      end_point
  end
end

#num_pointsObject



21
22
23
# File 'lib/ffi-geos/line_string.rb', line 21

def num_points
  FFIGeos.GEOSGeomGetNumPoints_r(Geos.current_handle_pointer, ptr)
end

#offset_curve(width, **options) ⇒ Object



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

def offset_curve(width, **options)
  options = Constants::BUFFER_PARAM_DEFAULTS.merge(options)

  cast_geometry_ptr(
    FFIGeos.GEOSOffsetCurve_r(
      Geos.current_handle_pointer,
      ptr,
      width,
      options[:quad_segs],
      options[:join],
      options[:mitre_limit]
    ),
    srid_copy: srid
  )
end

#point_n(n) ⇒ Object



30
31
32
33
34
# File 'lib/ffi-geos/line_string.rb', line 30

def point_n(n)
  raise Geos::IndexBoundsError if n.negative? || n >= num_points

  cast_geometry_ptr(FFIGeos.GEOSGeomGetPointN_r(Geos.current_handle_pointer, ptr, n), srid_copy: srid)
end

#snap_to_grid(*args) ⇒ Object



100
101
102
103
104
# File 'lib/ffi-geos/line_string.rb', line 100

def snap_to_grid(*args, **)
  ret = dup.snap_to_grid!(*args)
  ret.srid = pick_srid_according_to_policy(srid)
  ret
end

#snap_to_grid!(*args) ⇒ Object



84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
# File 'lib/ffi-geos/line_string.rb', line 84

def snap_to_grid!(*args, **)
  unless empty?
    cs = coord_seq.snap_to_grid!(*args)

    if cs.empty?
      @ptr = Geos.create_empty_line_string(srid: srid).ptr
    elsif cs.length <= 1
      raise Geos::InvalidGeometryError, "snap_to_grid! produced an invalid number of points in for a LineString - found #{cs.length} - must be 0 or > 1"
    else
      @ptr = Geos.create_line_string(cs).ptr
    end
  end

  self
end

#to_linear_ringObject



67
68
69
70
71
72
73
74
# File 'lib/ffi-geos/line_string.rb', line 67

def to_linear_ring
  return Geos.create_linear_ring(coord_seq, srid: pick_srid_according_to_policy(srid)) if closed?

  self_cs = coord_seq.to_a
  self_cs.push(self_cs[0])

  Geos.create_linear_ring(self_cs, srid: pick_srid_according_to_policy(srid))
end

#to_polygonObject



76
77
78
# File 'lib/ffi-geos/line_string.rb', line 76

def to_polygon
  to_linear_ring.to_polygon
end