Class: Dse::Geometry::Point

Inherits:
Object
  • Object
show all
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

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(*args) ⇒ Point

Returns a new instance of Point.

Examples:

Construct a Point with numeric arguments.

point = Point.new(3, 4)

Construct a Point with a wkt string.

point = Point.new('POINT (3.0 4.0)')

Parameters:

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

    varargs-style arguments in two forms:

    • two-element numeric array representing x,y coordinates.
    • one-element string array with the wkt representation.


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

#xFloat (readonly)

Returns the x coordinate of the point.

Returns:

  • (Float)

    the x coordinate of the point.



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

def x
  @x
end

#yFloat (readonly)

Returns the y coordinate of the point.

Returns:

  • (Float)

    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.

Parameters:

  • data (String)

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

Returns:

Raises:

  • (Cassandra::Errors::DecodingError)

    upon failure.



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

.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.



122
123
124
# File 'lib/dse/geometry/point.rb', line 122

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.



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_sString

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

Returns:



73
74
75
# File 'lib/dse/geometry/point.rb', line 73

def to_s
  "#{@x},#{@y}"
end

#wktString

Returns well-known-text representation of this point.

Returns:

  • (String)

    well-known-text representation of this point.



68
69
70
# File 'lib/dse/geometry/point.rb', line 68

def wkt
  "POINT (#{wkt_internal})"
end