Class: Dse::Geometry::LineString

Inherits:
Object
  • Object
show all
Includes:
Cassandra::CustomData
Defined in:
lib/dse/geometry/line_string.rb

Overview

Encapsulates a set of lines, characterized by a sequence of Points in the xy-plane. It corresponds to the org.apache.cassandra.db.marshal.LineStringType column type in DSE.

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(*args) ⇒ LineString

Returns a new instance of LineString.

Examples:

Construct an empty LineString

line = LineString.new

Construct a LineString with Point objects.

line = LineString.new(Point.new(1.0, 2.0), Point.new(3.0, 4.0))

Construct a LineString with a wkt string.

line = LineString.new('LINESTRING (1.0 2.0, 3.0 4.0)')

Parameters:

  • args (Array<Point>, Array<String>)

    varargs-style arguments in two forms:

    • ordered collection of points that make up this line-string. Must be empty or have at least two points.
    • one-element string array with the wkt representation.


38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/dse/geometry/line_string.rb', line 38

def initialize(*args)
  # The constructor has two forms:
  # 1. 0, 2, or more Point objects in the order in which one connects them to make a line-string.
  # 2. one String arg as the wkt representation.

  if args.size == 1
    wkt = args.first
    Cassandra::Util.assert_instance_of(String, wkt)

    if wkt == 'LINESTRING EMPTY'
      @points = [].freeze
    else
      match = wkt.match(WKT_RE)
      raise ArgumentError, "#{wkt.inspect} is not a valid WKT representation of a line-string" unless match
      @points = self.class.parse_wkt_internal(match[1])
    end
  else
    @points = args.freeze
    @points.each do |p|
      Cassandra::Util.assert_instance_of(Point, p, "#{p.inspect} is not a Point")
    end
  end
end

Instance Attribute Details

#pointsArray<Point> (readonly)

Returns collection of points that make up this line-string.

Returns:

  • (Array<Point>)

    collection of points that make up this line-string.



20
21
22
# File 'lib/dse/geometry/line_string.rb', line 20

def points
  @points
end

Class Method Details

.deserialize(data) ⇒ LineString

Deserialize the given data into an instance of this domain object class.

Parameters:

  • data (String)

    byte-array representation of a column value of this custom type.

Returns:

Raises:

  • (Cassandra::Errors::DecodingError)

    upon failure.



121
122
123
124
125
126
127
128
129
130
131
132
133
# File 'lib/dse/geometry/line_string.rb', line 121

def self.deserialize(data)
  buffer = Cassandra::Protocol::CqlByteBuffer.new(data)
  little_endian = buffer.read(1) != "\x00"

  # Depending on the endian-ness of the data, we want to read it differently. Wrap the buffer
  # with an "endian-aware" reader that reads the desired way.
  buffer = Dse::Util::EndianBuffer.new(buffer, little_endian)

  type = buffer.read_unsigned
  raise Cassandra::Errors::DecodingError, "LineString data-type value should be 2, but was #{type}" if type != 2

  deserialize_raw(buffer)
end

.typeCassandra::Types::Custom

Returns type of column that is processed by this domain object class.

Returns:

  • (Cassandra::Types::Custom)

    type of column that is processed by this domain object class.



113
114
115
# File 'lib/dse/geometry/line_string.rb', line 113

def self.type
  TYPE
end

Instance Method Details

#serializeString

Serialize this domain object into a byte array to send to DSE.

Returns:

  • (String)

    byte-array representation of this domain object.



152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
# File 'lib/dse/geometry/line_string.rb', line 152

def serialize
  buffer = Cassandra::Protocol::CqlByteBuffer.new

  # We serialize in little-endian form.

  buffer << "\x01"

  # This is a line-string.
  buffer.append([2].pack(Cassandra::Protocol::Formats::INT_FORMAT_LE))

  # Write out the count of how many points we have.
  serialize_raw(buffer)

  buffer
end

#to_sString

Returns a human-readable English string describing this Dse::Geometry::LineString.

Returns:



79
80
81
# File 'lib/dse/geometry/line_string.rb', line 79

def to_s
  @points.join(' to ')
end

#wktString

Returns well-known-text representation of this line-string.

Returns:

  • (String)

    well-known-text representation of this line-string.



74
75
76
# File 'lib/dse/geometry/line_string.rb', line 74

def wkt
  @points.empty? ? 'LINESTRING EMPTY' : "LINESTRING (#{wkt_internal})"
end