Class: Dse::Geometry::LineString
- Inherits:
-
Object
- Object
- Dse::Geometry::LineString
- 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
-
#points ⇒ Array<Point>
readonly
Collection of points that make up this line-string.
Class Method Summary collapse
-
.deserialize(data) ⇒ LineString
Deserialize the given data into an instance of this domain object class.
-
.type ⇒ Cassandra::Types::Custom
Type of column that is processed by this domain object class.
Instance Method Summary collapse
-
#initialize(*args) ⇒ LineString
constructor
A new instance of LineString.
-
#serialize ⇒ String
Serialize this domain object into a byte array to send to DSE.
-
#to_s ⇒ String
A human-readable English string describing this LineString.
-
#wkt ⇒ String
Well-known-text representation of this line-string.
Constructor Details
#initialize(*args) ⇒ LineString
Returns a new instance of LineString.
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
#points ⇒ Array<Point> (readonly)
Returns 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.
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 |
.type ⇒ Cassandra::Types::Custom
Returns 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
#serialize ⇒ String
Serialize this domain object into a byte array to send to DSE.
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_s ⇒ String
Returns a human-readable English string describing this Dse::Geometry::LineString.
79 80 81 |
# File 'lib/dse/geometry/line_string.rb', line 79 def to_s @points.join(' to ') end |
#wkt ⇒ String
Returns 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 |