Class: Geos::CoordinateSequence
- Inherits:
-
Object
- Object
- Geos::CoordinateSequence
- Includes:
- Enumerable
- Defined in:
- lib/ffi-geos/coordinate_sequence.rb
Overview
A CoordinateSequence is a list of coordinates in a Geometry.
Defined Under Namespace
Classes: CoordinateAccessor, ParseError
Instance Attribute Summary collapse
-
#ptr ⇒ Object
readonly
Returns the value of attribute ptr.
-
#x ⇒ Object
readonly
Returns the value of attribute x.
-
#y ⇒ Object
readonly
Returns the value of attribute y.
-
#z ⇒ Object
readonly
Returns the value of attribute z.
Class Method Summary collapse
-
.release(ptr) ⇒ Object
:nodoc:.
Instance Method Summary collapse
- #[](*args) ⇒ Object (also: #slice)
- #dimensions ⇒ Object
-
#each ⇒ Object
Yields coordinates as [ x, y, z ].
- #empty? ⇒ Boolean
- #get_ordinate(idx, dim) ⇒ Object
-
#get_x(idx) ⇒ Object
Gets the x value of a coordinate.
-
#get_y(idx) ⇒ Object
Gets the y value of a coordinate.
-
#get_z(idx) ⇒ Object
Gets the z value of a coordinate.
- #has_z? ⇒ Boolean
-
#initialize(*args) ⇒ CoordinateSequence
constructor
:call-seq: new(ptr, auto_free = true) new(size = 0, dimensions = 0) new(options) new(points).
- #initialize_copy(source) ⇒ Object
- #length ⇒ Object (also: #size)
- #set_ordinate(idx, dim, val) ⇒ Object
-
#set_x(idx, val) ⇒ Object
Sets the x value of a coordinate.
-
#set_y(idx, val) ⇒ Object
Sets the y value of a coordinate.
-
#set_z(idx, val) ⇒ Object
Sets the z value of a coordinate.
- #to_line_string(options = {}) ⇒ Object
- #to_linear_ring(options = {}) ⇒ Object
- #to_point(options = {}) ⇒ Object
- #to_polygon(options = {}) ⇒ Object
- #to_s ⇒ Object
Constructor Details
#initialize(*args) ⇒ CoordinateSequence
:call-seq:
new(ptr, auto_free = true)
new(size = 0, dimensions = 0)
new()
new(points)
The ptr version of the initializer is for internal use.
new(points) will try to glean the size and dimensions of your CoordinateSequence from an Array of points. The Array should contain uniform-sized Arrays which represent the [ x, y, z ] values of your coordinates.
58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 |
# File 'lib/ffi-geos/coordinate_sequence.rb', line 58 def initialize(*args) points = nil # forward declaration we can use later ptr, auto_free = if args.first.is_a?(FFI::Pointer) [ args.first, args[1] ] else size, dimensions = if args.first.is_a?(Array) points = if args.first.first.is_a?(Array) args.first else args end lengths = points.collect(&:length).uniq if lengths.empty? [ 0, 0 ] elsif lengths.length != 1 raise ParseError.new("Different sized points found in Array") elsif !lengths.first.between?(1, 3) raise ParseError.new("Expected points to contain 1-3 elements") else [ points.length, points.first.length ] end elsif args.first.is_a?(Hash) args.first.values_at(:size, :dimensions) else if !args.length.between?(0, 2) raise ArgumentError.new("wrong number of arguments (#{args.length} for 0-2)") else [ args[0], args[1] ] end end size ||= 0 dimensions ||= 0 [ FFIGeos.GEOSCoordSeq_create_r(Geos.current_handle, size, dimensions), true ] end @ptr = FFI::AutoPointer.new( ptr, self.class.method(:release) ) @ptr.autorelease = auto_free @x = CoordinateAccessor.new(self, 0) @y = CoordinateAccessor.new(self, 1) @z = CoordinateAccessor.new(self, 2) if points points.each_with_index do |point, idx| point.each_with_index do |val, dim| self.set_ordinate(idx, dim, val) end end end end |
Instance Attribute Details
#ptr ⇒ Object (readonly)
Returns the value of attribute ptr.
44 45 46 |
# File 'lib/ffi-geos/coordinate_sequence.rb', line 44 def ptr @ptr end |
#x ⇒ Object (readonly)
Returns the value of attribute x.
44 45 46 |
# File 'lib/ffi-geos/coordinate_sequence.rb', line 44 def x @x end |
#y ⇒ Object (readonly)
Returns the value of attribute y.
44 45 46 |
# File 'lib/ffi-geos/coordinate_sequence.rb', line 44 def y @y end |
#z ⇒ Object (readonly)
Returns the value of attribute z.
44 45 46 |
# File 'lib/ffi-geos/coordinate_sequence.rb', line 44 def z @z end |
Class Method Details
.release(ptr) ⇒ Object
:nodoc:
128 129 130 |
# File 'lib/ffi-geos/coordinate_sequence.rb', line 128 def self.release(ptr) #:nodoc: FFIGeos.GEOSCoordSeq_destroy_r(Geos.current_handle, ptr) end |
Instance Method Details
#[](*args) ⇒ Object Also known as: slice
147 148 149 150 151 152 153 154 155 156 |
# File 'lib/ffi-geos/coordinate_sequence.rb', line 147 def [](*args) if args.length == 1 && args.first.is_a?(Numeric) && args.first >= 0 i = args.first ary = [ self.get_x(i), self.get_y(i) ] ary << self.get_z(i) if self.has_z? ary else self.to_a[*args] end end |
#dimensions ⇒ Object
228 229 230 231 232 233 234 235 236 |
# File 'lib/ffi-geos/coordinate_sequence.rb', line 228 def dimensions if defined?(@dimensions) @dimensions else int_ptr = FFI::MemoryPointer.new(:int) FFIGeos.GEOSCoordSeq_getDimensions_r(Geos.current_handle, self.ptr, int_ptr) @dimensions = int_ptr.read_int end end |
#each ⇒ Object
Yields coordinates as [ x, y, z ]. The z coordinate may be omitted for 2-dimensional CoordinateSequences.
134 135 136 137 138 139 140 141 142 143 144 145 |
# File 'lib/ffi-geos/coordinate_sequence.rb', line 134 def each if block_given? self.length.times do |n| yield self.build_coordinate(n) end self else self.length.times.collect { |n| self.build_coordinate(n) }.to_enum end end |
#empty? ⇒ Boolean
224 225 226 |
# File 'lib/ffi-geos/coordinate_sequence.rb', line 224 def empty? self.length == 0 end |
#get_ordinate(idx, dim) ⇒ Object
210 211 212 213 214 215 |
# File 'lib/ffi-geos/coordinate_sequence.rb', line 210 def get_ordinate(idx, dim) self.check_bounds(idx) double_ptr = FFI::MemoryPointer.new(:double) FFIGeos.GEOSCoordSeq_getOrdinate_r(Geos.current_handle, self.ptr, idx, dim, double_ptr) double_ptr.read_double end |
#get_x(idx) ⇒ Object
Gets the x value of a coordinate. Can also be retrieved via #x[].
187 188 189 190 191 192 |
# File 'lib/ffi-geos/coordinate_sequence.rb', line 187 def get_x(idx) self.check_bounds(idx) double_ptr = FFI::MemoryPointer.new(:double) FFIGeos.GEOSCoordSeq_getX_r(Geos.current_handle, self.ptr, idx, double_ptr) double_ptr.read_double end |
#get_y(idx) ⇒ Object
Gets the y value of a coordinate. Can also be retrieved via #y[].
195 196 197 198 199 200 |
# File 'lib/ffi-geos/coordinate_sequence.rb', line 195 def get_y(idx) self.check_bounds(idx) double_ptr = FFI::MemoryPointer.new(:double) FFIGeos.GEOSCoordSeq_getY_r(Geos.current_handle, self.ptr, idx, double_ptr) double_ptr.read_double end |
#get_z(idx) ⇒ Object
Gets the z value of a coordinate. Can also be retrieved via #z[].
203 204 205 206 207 208 |
# File 'lib/ffi-geos/coordinate_sequence.rb', line 203 def get_z(idx) self.check_bounds(idx) double_ptr = FFI::MemoryPointer.new(:double) FFIGeos.GEOSCoordSeq_getZ_r(Geos.current_handle, self.ptr, idx, double_ptr) double_ptr.read_double end |
#has_z? ⇒ Boolean
159 160 161 |
# File 'lib/ffi-geos/coordinate_sequence.rb', line 159 def has_z? self.dimensions == 3 end |
#initialize_copy(source) ⇒ Object
117 118 119 120 121 122 123 124 125 126 |
# File 'lib/ffi-geos/coordinate_sequence.rb', line 117 def initialize_copy(source) @ptr = FFI::AutoPointer.new( FFIGeos.GEOSCoordSeq_clone_r(Geos.current_handle, source.ptr), self.class.method(:release) ) @x = CoordinateAccessor.new(self, 0) @y = CoordinateAccessor.new(self, 1) @z = CoordinateAccessor.new(self, 2) end |
#length ⇒ Object Also known as: size
217 218 219 220 221 |
# File 'lib/ffi-geos/coordinate_sequence.rb', line 217 def length int_ptr = FFI::MemoryPointer.new(:int) FFIGeos.GEOSCoordSeq_getSize_r(Geos.current_handle, self.ptr, int_ptr) int_ptr.read_int end |
#set_ordinate(idx, dim, val) ⇒ Object
181 182 183 184 |
# File 'lib/ffi-geos/coordinate_sequence.rb', line 181 def set_ordinate(idx, dim, val) self.check_bounds(idx) FFIGeos.GEOSCoordSeq_setOrdinate_r(Geos.current_handle, self.ptr, idx, dim, val) end |
#set_x(idx, val) ⇒ Object
Sets the x value of a coordinate. Can also be set via #x[]=.
164 165 166 167 |
# File 'lib/ffi-geos/coordinate_sequence.rb', line 164 def set_x(idx, val) self.check_bounds(idx) FFIGeos.GEOSCoordSeq_setX_r(Geos.current_handle, self.ptr, idx, val) end |
#set_y(idx, val) ⇒ Object
Sets the y value of a coordinate. Can also be set via #y[]=.
170 171 172 173 |
# File 'lib/ffi-geos/coordinate_sequence.rb', line 170 def set_y(idx, val) self.check_bounds(idx) FFIGeos.GEOSCoordSeq_setY_r(Geos.current_handle, self.ptr, idx, val) end |
#set_z(idx, val) ⇒ Object
Sets the z value of a coordinate. Can also be set via #z[]=.
176 177 178 179 |
# File 'lib/ffi-geos/coordinate_sequence.rb', line 176 def set_z(idx, val) self.check_bounds(idx) FFIGeos.GEOSCoordSeq_setZ_r(Geos.current_handle, self.ptr, idx, val) end |
#to_line_string(options = {}) ⇒ Object
246 247 248 |
# File 'lib/ffi-geos/coordinate_sequence.rb', line 246 def to_line_string( = {}) Geos.create_line_string(self, :srid => [:srid]) end |
#to_linear_ring(options = {}) ⇒ Object
242 243 244 |
# File 'lib/ffi-geos/coordinate_sequence.rb', line 242 def to_linear_ring( = {}) Geos.create_linear_ring(self, :srid => [:srid]) end |
#to_point(options = {}) ⇒ Object
238 239 240 |
# File 'lib/ffi-geos/coordinate_sequence.rb', line 238 def to_point( = {}) Geos.create_point(self, :srid => [:srid]) end |
#to_polygon(options = {}) ⇒ Object
250 251 252 |
# File 'lib/ffi-geos/coordinate_sequence.rb', line 250 def to_polygon( = {}) Geos.create_polygon(self, :srid => [:srid]) end |
#to_s ⇒ Object
254 255 256 257 258 |
# File 'lib/ffi-geos/coordinate_sequence.rb', line 254 def to_s self.entries.collect { |entry| entry.join(' ') }.join(', ') end |