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)
- #affine(options) ⇒ Object
- #affine!(options) ⇒ Object
- #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, parent = nil) new(size = 0, dimensions = 0) new(options) new(points).
- #initialize_copy(source) ⇒ Object
- #length ⇒ Object (also: #size)
- #remove_duplicate_coords ⇒ Object
- #rotate(radians, origin = [ 0.0, 0.0 ]) ⇒ Object
- #rotate!(radians, origin = [ 0.0, 0.0 ]) ⇒ Object
- #rotate_x(radians) ⇒ Object
- #rotate_x!(radians) ⇒ Object
- #rotate_y(radians) ⇒ Object
- #rotate_y!(radians) ⇒ Object
- #rotate_z(radians) ⇒ Object
- #rotate_z!(radians) ⇒ Object
- #scale(*args) ⇒ Object
- #scale!(*args) ⇒ Object
- #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.
- #snap_to_grid(*args) ⇒ Object
- #snap_to_grid!(*args) ⇒ Object
- #to_line_string(options = {}) ⇒ Object
- #to_linear_ring(options = {}) ⇒ Object
- #to_point(options = {}) ⇒ Object
- #to_polygon(options = {}) ⇒ Object
- #to_s ⇒ Object
- #trans_scale(*args) ⇒ Object
- #trans_scale!(*args) ⇒ Object
- #translate(*args) ⇒ Object
- #translate!(*args) ⇒ Object
Constructor Details
#initialize(*args) ⇒ CoordinateSequence
:call-seq:
new(ptr, auto_free = true, parent = nil)
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.
57 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 57 def initialize(*args) points = nil # forward declaration we can use later ptr, auto_free, parent = if args.first.is_a?(FFI::Pointer) args.first(3) 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, 'Different sized points found in Array' elsif !lengths.first.between?(1, 3) raise ParseError, '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, "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_pointer, size, dimensions), true ] end @ptr = FFI::AutoPointer.new( ptr, self.class.method(:release) ) @ptr.autorelease = auto_free @parent = parent if parent @x = CoordinateAccessor.new(self, 0) @y = CoordinateAccessor.new(self, 1) @z = CoordinateAccessor.new(self, 2) return unless points points.each_with_index do |point, idx| point.each_with_index do |val, dim| set_ordinate(idx, dim, val) end end end |
Instance Attribute Details
#ptr ⇒ Object (readonly)
Returns the value of attribute ptr.
43 44 45 |
# File 'lib/ffi-geos/coordinate_sequence.rb', line 43 def ptr @ptr end |
#x ⇒ Object (readonly)
Returns the value of attribute x.
43 44 45 |
# File 'lib/ffi-geos/coordinate_sequence.rb', line 43 def x @x end |
#y ⇒ Object (readonly)
Returns the value of attribute y.
43 44 45 |
# File 'lib/ffi-geos/coordinate_sequence.rb', line 43 def y @y end |
#z ⇒ Object (readonly)
Returns the value of attribute z.
43 44 45 |
# File 'lib/ffi-geos/coordinate_sequence.rb', line 43 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_pointer, 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 = [ get_x(i), get_y(i) ] ary << get_z(i) if has_z? ary else to_a[*args] end end |
#affine(options) ⇒ Object
371 372 373 |
# File 'lib/ffi-geos/coordinate_sequence.rb', line 371 def affine() self.dup.affine!() end |
#affine!(options) ⇒ Object
348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 |
# File 'lib/ffi-geos/coordinate_sequence.rb', line 348 def affine!() .default = 0.0 if self.has_z? self.length.times do |i| x, y, z = self.x[i], self.y[i], self.z[i] self.x[i] = [:afac] * x + [:bfac] * y + [:cfac] * z + [:xoff] self.y[i] = [:dfac] * x + [:efac] * y + [:ffac] * z + [:yoff] self.z[i] = [:gfac] * x + [:hfac] * y + [:ifac] * z + [:zoff] end else self.length.times do |i| x, y = self.x[i], self.y[i] self.x[i] = [:afac] * x + [:bfac] * y + [:xoff] self.y[i] = [:dfac] * x + [:efac] * y + [:yoff] end end self 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_pointer, 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? length.times do |n| yield build_coordinate(n) end self else length.times.collect { |n| build_coordinate(n) }.to_enum end end |
#empty? ⇒ Boolean
224 225 226 |
# File 'lib/ffi-geos/coordinate_sequence.rb', line 224 def empty? length.zero? 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) check_bounds(idx) double_ptr = FFI::MemoryPointer.new(:double) FFIGeos.GEOSCoordSeq_getOrdinate_r(Geos.current_handle_pointer, 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) check_bounds(idx) double_ptr = FFI::MemoryPointer.new(:double) FFIGeos.GEOSCoordSeq_getX_r(Geos.current_handle_pointer, 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) check_bounds(idx) double_ptr = FFI::MemoryPointer.new(:double) FFIGeos.GEOSCoordSeq_getY_r(Geos.current_handle_pointer, 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) check_bounds(idx) double_ptr = FFI::MemoryPointer.new(:double) FFIGeos.GEOSCoordSeq_getZ_r(Geos.current_handle_pointer, 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? 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_pointer, 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_pointer, ptr, int_ptr) int_ptr.read_int end |
#remove_duplicate_coords ⇒ Object
341 342 343 344 345 346 |
# File 'lib/ffi-geos/coordinate_sequence.rb', line 341 def remove_duplicate_coords Geos::CoordinateSequence.new(self.to_a.inject([]) { |memo, v| memo << v unless memo.last == v memo }) end |
#rotate(radians, origin = [ 0.0, 0.0 ]) ⇒ Object
402 403 404 |
# File 'lib/ffi-geos/coordinate_sequence.rb', line 402 def rotate(radians, origin = [ 0.0, 0.0 ]) self.dup.rotate!(radians, origin) end |
#rotate!(radians, origin = [ 0.0, 0.0 ]) ⇒ Object
375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 |
# File 'lib/ffi-geos/coordinate_sequence.rb', line 375 def rotate!(radians, origin = [ 0.0, 0.0 ]) origin = case origin when Array origin when Geos::Geometry center = origin.centroid [ center.x, center.y ] else raise ArgumentError.new("Expected an Array or a Geos::Geometry for the origin") end self.affine!({ :afac => Math.cos(radians), :bfac => -Math.sin(radians), :cfac => 0, :dfac => Math.sin(radians), :efac => Math.cos(radians), :ffac => 0, :gfac => 0, :hfac => 0, :ifac => 1, :xoff => origin[0] - Math.cos(radians) * origin[0] + Math.sin(radians) * origin[1], :yoff => origin[1] - Math.sin(radians) * origin[0] - Math.cos(radians) * origin[1], :zoff => 0 }) end |
#rotate_x(radians) ⇒ Object
423 424 425 |
# File 'lib/ffi-geos/coordinate_sequence.rb', line 423 def rotate_x(radians) self.dup.rotate_x!(radians) end |
#rotate_x!(radians) ⇒ Object
406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 |
# File 'lib/ffi-geos/coordinate_sequence.rb', line 406 def rotate_x!(radians) self.affine!({ :afac => 1, :bfac => 0, :cfac => 0, :dfac => 0, :efac => Math.cos(radians), :ffac => -Math.sin(radians), :gfac => 0, :hfac => Math.sin(radians), :ifac => Math.cos(radians), :xoff => 0, :yoff => 0, :zoff => 0 }) end |
#rotate_y(radians) ⇒ Object
444 445 446 |
# File 'lib/ffi-geos/coordinate_sequence.rb', line 444 def rotate_y(radians) self.dup.rotate_y!(radians) end |
#rotate_y!(radians) ⇒ Object
427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 |
# File 'lib/ffi-geos/coordinate_sequence.rb', line 427 def rotate_y!(radians) self.affine!({ :afac => Math.cos(radians), :bfac => 0, :cfac => Math.sin(radians), :dfac => 0, :efac => 1, :ffac => 0, :gfac => -Math.sin(radians), :hfac => 0, :ifac => Math.cos(radians), :xoff => 0, :yoff => 0, :zoff => 0 }) end |
#rotate_z(radians) ⇒ Object
452 453 454 |
# File 'lib/ffi-geos/coordinate_sequence.rb', line 452 def rotate_z(radians) self.dup.rotate!(radians) end |
#rotate_z!(radians) ⇒ Object
448 449 450 |
# File 'lib/ffi-geos/coordinate_sequence.rb', line 448 def rotate_z!(radians) self.rotate!(radians) end |
#scale(*args) ⇒ Object
481 482 483 |
# File 'lib/ffi-geos/coordinate_sequence.rb', line 481 def scale(*args) self.dup.scale!(*args) end |
#scale!(*args) ⇒ Object
456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 |
# File 'lib/ffi-geos/coordinate_sequence.rb', line 456 def scale!(*args) x, y, z = if args.length == 1 && args[0].is_a?(Hash) args[0].values_at(:x, :y, :z) elsif args.length.between?(1, 3) args.values_at(0...3) else raise ArgumentError.new("Wrong number of arguments #{args.length} for 1-3") end self.affine!({ :afac => x || 1, :bfac => 0, :cfac => 0, :dfac => 0, :efac => y || 1, :ffac => 0, :gfac => 0, :hfac => 0, :ifac => z || 1, :xoff => 0, :yoff => 0, :zoff => 0 }) 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) check_bounds(idx) FFIGeos.GEOSCoordSeq_setOrdinate_r(Geos.current_handle_pointer, ptr, idx, dim, val.to_f) 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) check_bounds(idx) FFIGeos.GEOSCoordSeq_setX_r(Geos.current_handle_pointer, ptr, idx, val.to_f) 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) check_bounds(idx) FFIGeos.GEOSCoordSeq_setY_r(Geos.current_handle_pointer, ptr, idx, val.to_f) 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) check_bounds(idx) FFIGeos.GEOSCoordSeq_setZ_r(Geos.current_handle_pointer, ptr, idx, val.to_f) end |
#snap_to_grid(*args) ⇒ Object
337 338 339 |
# File 'lib/ffi-geos/coordinate_sequence.rb', line 337 def snap_to_grid(*args) self.dup.snap_to_grid!(*args) end |
#snap_to_grid!(*args) ⇒ Object
282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 |
# File 'lib/ffi-geos/coordinate_sequence.rb', line 282 def snap_to_grid!(*args) grid = { :offset_x => 0, # 1 :offset_y => 0, # 2 :offset_z => 0, # - :size_x => 0, # 3 :size_y => 0, # 4 :size_z => 0 # - } if args.length == 1 && args[0].is_a?(Numeric) grid[:size_x] = grid[:size_y] = grid[:size_z] = args[0] elsif args[0].is_a?(Hash) grid.merge!(args[0]) end if grid[:size] grid[:size_x] = grid[:size_y] = grid[:size_z] = grid[:size] end if grid[:offset] case grid[:offset] when Geos::Geometry point = grid[:offset].centroid grid[:offset_x] = point.x grid[:offset_y] = point.y grid[:offset_z] = point.z when Array grid[:offset_x], grid[:offset_y], grid[:offset_z] = grid[:offset] else raise ArgumentError.new("Expected :offset option to be a Geos::Point") end end self.length.times do |i| if grid[:size_x] != 0 self.x[i] = ((self.x[i] - grid[:offset_x]) / grid[:size_x]).round * grid[:size_x] + grid[:offset_x] end if grid[:size_y] != 0 self.y[i] = ((self.y[i] - grid[:offset_y]) / grid[:size_y]).round * grid[:size_y] + grid[:offset_y] end if self.has_z? && grid[:size_z] != 0 self.z[i] = ((self.z[i] - grid[:offset_z]) / grid[:size_z]).round * grid[:size_z] + grid[:offset_z] end end cs = self.remove_duplicate_coords @ptr = cs.ptr self 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 entries.collect { |entry| entry.join(' ') }.join(', ') end |
#trans_scale(*args) ⇒ Object
515 516 517 |
# File 'lib/ffi-geos/coordinate_sequence.rb', line 515 def trans_scale(*args) self.dup.trans_scale!(*args) end |
#trans_scale!(*args) ⇒ Object
485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 |
# File 'lib/ffi-geos/coordinate_sequence.rb', line 485 def trans_scale!(*args) delta_x, delta_y, x_factor, y_factor = if args.length == 1 && args[0].is_a?(Hash) args[0].values_at(:delta_x, :delta_y, :x_factor, :y_factor) elsif args.length.between?(1, 4) args.values_at(0...4) else raise ArgumentError.new("Wrong number of arguments #{args.length} for 1-4") end x_factor ||= 1 y_factor ||= 1 delta_x ||= 0 delta_y ||= 0 self.affine!({ :afac => x_factor, :bfac => 0, :cfac => 0, :dfac => 0, :efac => y_factor, :ffac => 0, :gfac => 0, :hfac => 0, :ifac => 1, :xoff => delta_x * x_factor, :yoff => delta_y * y_factor, :zoff => 0 }) end |
#translate(*args) ⇒ Object
544 545 546 |
# File 'lib/ffi-geos/coordinate_sequence.rb', line 544 def translate(*args) self.dup.translate!(*args) end |
#translate!(*args) ⇒ Object
519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 |
# File 'lib/ffi-geos/coordinate_sequence.rb', line 519 def translate!(*args) x, y, z = if args.length == 1 && args[0].is_a?(Hash) args[0].values_at(:x, :y, :z) elsif args.length.between?(1, 3) args.values_at(0...3) else raise ArgumentError.new("Wrong number of arguments #{args.length} for 1-3") end self.affine!({ :afac => 1, :bfac => 0, :cfac => 0, :dfac => 0, :efac => 1, :ffac => 0, :gfac => 0, :hfac => 0, :ifac => 1, :xoff => x || 0, :yoff => y || 0, :zoff => z || 1 }) end |