Class: Dse::Geometry::Point
- Inherits:
-
Object
- Object
- Dse::Geometry::Point
- Includes:
- Cassandra::CustomData
- Defined in:
- lib/dse/geometry/point.rb
Overview
Encapsulates a 2D point with x,y coordinates. It corresponds to the org.apache.cassandra.db.marshal.PointType
column type in DSE.
Instance Attribute Summary collapse
-
#x ⇒ Float
readonly
The x coordinate of the point.
-
#y ⇒ Float
readonly
The y coordinate of the point.
Class Method Summary collapse
-
.deserialize(data) ⇒ Point
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) ⇒ Point
constructor
A new instance of Point.
-
#serialize ⇒ String
Serialize this domain object into a byte array to send to DSE.
-
#to_s ⇒ String
A human-readable English string describing this Point.
-
#wkt ⇒ String
Well-known-text representation of this point.
Constructor Details
#initialize(*args) ⇒ Point
Returns a new instance of Point.
39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 |
# File 'lib/dse/geometry/point.rb', line 39 def initialize(*args) # The constructor has two forms: # 1. two numeric args (x,y) # 2. one String arg as the wkt representation. case args.size when 2 x, y = args Cassandra::Util.assert_instance_of(::Numeric, x) Cassandra::Util.assert_instance_of(::Numeric, y) Cassandra::Util.assert(!x.nan?, 'x cannot be Float::NAN') if x.is_a?(Float) Cassandra::Util.assert(!y.nan?, 'y cannot be Float::NAN') if y.is_a?(Float) @x = x.to_f @y = y.to_f when 1 wkt = args.first Cassandra::Util.assert_instance_of(String, wkt) # subsitute eol chars in the string with a space. wkt.gsub!(EOL_RE, ' ') match = wkt.match(WKT_RE) raise ArgumentError, "#{wkt.inspect} is not a valid WKT representation of a point" unless match @x, @y = self.class.parse_wkt_internal(match[1]) else raise ArgumentError, 'wrong number of arguments: use one string argument (wkt) or two numeric arguments (x,y)' end end |
Instance Attribute Details
#x ⇒ Float (readonly)
Returns the x coordinate of the point.
20 21 22 |
# File 'lib/dse/geometry/point.rb', line 20 def x @x end |
#y ⇒ Float (readonly)
Returns the y coordinate of the point.
22 23 24 |
# File 'lib/dse/geometry/point.rb', line 22 def y @y end |
Class Method Details
.deserialize(data) ⇒ Point
Deserialize the given data into an instance of this domain object class.
130 131 132 133 134 135 136 137 138 139 140 141 |
# File 'lib/dse/geometry/point.rb', line 130 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, "Point data-type value should be 1, but was #{type}" if type != 1 deserialize_raw(buffer) end |
.type ⇒ Cassandra::Types::Custom
Returns type of column that is processed by this domain object class.
122 123 124 |
# File 'lib/dse/geometry/point.rb', line 122 def self.type TYPE end |
Instance Method Details
#serialize ⇒ String
Serialize this domain object into a byte array to send to DSE.
154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 |
# File 'lib/dse/geometry/point.rb', line 154 def serialize buffer = Cassandra::Protocol::CqlByteBuffer.new # Serialize little-endian. buffer << "\x01" # This is a point. buffer.append([1].pack(Cassandra::Protocol::Formats::INT_FORMAT_LE)) # Write out x and y. serialize_raw(buffer) buffer end |
#to_s ⇒ String
Returns a human-readable English string describing this Dse::Geometry::Point.
73 74 75 |
# File 'lib/dse/geometry/point.rb', line 73 def to_s "#{@x},#{@y}" end |
#wkt ⇒ String
Returns well-known-text representation of this point.
68 69 70 |
# File 'lib/dse/geometry/point.rb', line 68 def wkt "POINT (#{wkt_internal})" end |