Class: Geos::CoordinateSequence

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

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(*args) ⇒ CoordinateSequence

:call-seq:

new(ptr, auto_free = true, parent = nil)
new(size = 0, dimensions = 0)
new(options)
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
# 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)
    elsif !args.length.between?(0, 2)
      raise ArgumentError, "wrong number of arguments (#{args.length} for 0-2)"
    else
      [args[0], args[1]]
    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

#ptrObject (readonly)

Returns the value of attribute ptr.



43
44
45
# File 'lib/ffi-geos/coordinate_sequence.rb', line 43

def ptr
  @ptr
end

#xObject (readonly)

Returns the value of attribute x.



43
44
45
# File 'lib/ffi-geos/coordinate_sequence.rb', line 43

def x
  @x
end

#yObject (readonly)

Returns the value of attribute y.



43
44
45
# File 'lib/ffi-geos/coordinate_sequence.rb', line 43

def y
  @y
end

#zObject (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:



126
127
128
# File 'lib/ffi-geos/coordinate_sequence.rb', line 126

def self.release(ptr) # :nodoc:
  FFIGeos.GEOSCoordSeq_destroy_r(Geos.current_handle_pointer, ptr)
end

Instance Method Details

#[](*args) ⇒ Object Also known as: slice



145
146
147
148
149
150
151
152
153
154
# File 'lib/ffi-geos/coordinate_sequence.rb', line 145

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



373
374
375
# File 'lib/ffi-geos/coordinate_sequence.rb', line 373

def affine(options)
  dup.affine!(options)
end

#affine!(options) ⇒ Object



347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
# File 'lib/ffi-geos/coordinate_sequence.rb', line 347

def affine!(options)
  options.default = 0.0

  if has_z?
    length.times do |i|
      x = self.x[i]
      y = self.y[i]
      z = self.z[i]

      self.x[i] = (options[:afac] * x) + (options[:bfac] * y) + (options[:cfac] * z) + options[:xoff]
      self.y[i] = (options[:dfac] * x) + (options[:efac] * y) + (options[:ffac] * z) + options[:yoff]
      self.z[i] = (options[:gfac] * x) + (options[:hfac] * y) + (options[:ifac] * z) + options[:zoff]
    end
  else
    length.times do |i|
      x = self.x[i]
      y = self.y[i]

      self.x[i] = (options[:afac] * x) + (options[:bfac] * y) + options[:xoff]
      self.y[i] = (options[:dfac] * x) + (options[:efac] * y) + options[:yoff]
    end
  end

  self
end

#counter_clockwise?Boolean Also known as: ccw?

Available in GEOS 3.7+.

Returns:

  • (Boolean)


238
239
240
241
242
# File 'lib/ffi-geos/coordinate_sequence.rb', line 238

def counter_clockwise?
  char_ptr = FFI::MemoryPointer.new(:char)
  FFIGeos.GEOSCoordSeq_isCCW_r(Geos.current_handle_pointer, ptr, char_ptr)
  Tools.bool_result(char_ptr.read_char)
end

#dimensionsObject



226
227
228
229
230
231
232
233
234
# File 'lib/ffi-geos/coordinate_sequence.rb', line 226

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

#eachObject

Yields coordinates as [ x, y, z ]. The z coordinate may be omitted for 2-dimensional CoordinateSequences.



132
133
134
135
136
137
138
139
140
141
142
143
# File 'lib/ffi-geos/coordinate_sequence.rb', line 132

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

Returns:

  • (Boolean)


222
223
224
# File 'lib/ffi-geos/coordinate_sequence.rb', line 222

def empty?
  length.zero?
end

#get_ordinate(idx, dim) ⇒ Object



208
209
210
211
212
213
# File 'lib/ffi-geos/coordinate_sequence.rb', line 208

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



185
186
187
188
189
190
# File 'lib/ffi-geos/coordinate_sequence.rb', line 185

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



193
194
195
196
197
198
# File 'lib/ffi-geos/coordinate_sequence.rb', line 193

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



201
202
203
204
205
206
# File 'lib/ffi-geos/coordinate_sequence.rb', line 201

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

Returns:

  • (Boolean)


157
158
159
# File 'lib/ffi-geos/coordinate_sequence.rb', line 157

def has_z?
  dimensions == 3
end

#initialize_copy(source) ⇒ Object



115
116
117
118
119
120
121
122
123
124
# File 'lib/ffi-geos/coordinate_sequence.rb', line 115

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

#lengthObject Also known as: size



215
216
217
218
219
# File 'lib/ffi-geos/coordinate_sequence.rb', line 215

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_coordsObject



341
342
343
344
345
# File 'lib/ffi-geos/coordinate_sequence.rb', line 341

def remove_duplicate_coords
  Geos::CoordinateSequence.new(to_a.each_with_object([]) do |v, memo|
    memo << v unless memo.last == v
  end)
end

#rotate(radians, origin = [0.0, 0.0]) ⇒ Object



404
405
406
# File 'lib/ffi-geos/coordinate_sequence.rb', line 404

def rotate(radians, origin = [0.0, 0.0])
  dup.rotate!(radians, origin)
end

#rotate!(radians, origin = [0.0, 0.0]) ⇒ Object



377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
# File 'lib/ffi-geos/coordinate_sequence.rb', line 377

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, 'Expected an Array or a Geos::Geometry for the origin'
  end

  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



425
426
427
# File 'lib/ffi-geos/coordinate_sequence.rb', line 425

def rotate_x(radians)
  dup.rotate_x!(radians)
end

#rotate_x!(radians) ⇒ Object



408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
# File 'lib/ffi-geos/coordinate_sequence.rb', line 408

def rotate_x!(radians)
  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



446
447
448
# File 'lib/ffi-geos/coordinate_sequence.rb', line 446

def rotate_y(radians)
  dup.rotate_y!(radians)
end

#rotate_y!(radians) ⇒ Object



429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
# File 'lib/ffi-geos/coordinate_sequence.rb', line 429

def rotate_y!(radians)
  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



454
455
456
# File 'lib/ffi-geos/coordinate_sequence.rb', line 454

def rotate_z(radians)
  dup.rotate!(radians)
end

#rotate_z!(radians) ⇒ Object



450
451
452
# File 'lib/ffi-geos/coordinate_sequence.rb', line 450

def rotate_z!(radians)
  rotate!(radians)
end

#scale(*args, **kwargs) ⇒ Object



483
484
485
# File 'lib/ffi-geos/coordinate_sequence.rb', line 483

def scale(*args, **kwargs)
  dup.scale!(*args, **kwargs)
end

#scale!(*args, **kwargs) ⇒ Object



458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
# File 'lib/ffi-geos/coordinate_sequence.rb', line 458

def scale!(*args, **kwargs)
  x, y, z = if !kwargs.empty?
    kwargs.values_at(:x, :y, :z)
  elsif args.length.between?(1, 3)
    args.values_at(0...3)
  else
    raise ArgumentError, "Wrong number of arguments #{args.length} for 1-3"
  end

  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



179
180
181
182
# File 'lib/ffi-geos/coordinate_sequence.rb', line 179

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[]=.



162
163
164
165
# File 'lib/ffi-geos/coordinate_sequence.rb', line 162

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[]=.



168
169
170
171
# File 'lib/ffi-geos/coordinate_sequence.rb', line 168

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[]=.



174
175
176
177
# File 'lib/ffi-geos/coordinate_sequence.rb', line 174

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, **)
  dup.snap_to_grid!(*args)
end

#snap_to_grid!(*args, **kwargs) ⇒ Object



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 290

def snap_to_grid!(*args, **kwargs)
  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 !kwargs.empty?
    grid.merge!(kwargs)
  end

  grid[:size_x] = grid[:size_y] = grid[:size_z] = grid[:size] if grid[:size]

  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, 'Expected :offset option to be a Geos::Point'
    end
  end

  length.times do |i|
    x[i] = (((x[i] - grid[:offset_x]) / grid[:size_x]).round * grid[:size_x]) + grid[:offset_x] if grid[:size_x] != 0

    y[i] = (((y[i] - grid[:offset_y]) / grid[:size_y]).round * grid[:size_y]) + grid[:offset_y] if grid[:size_y] != 0

    z[i] = (((z[i] - grid[:offset_z]) / grid[:size_z]).round * grid[:size_z]) + grid[:offset_z] if has_z? && grid[:size_z] != 0
  end

  cs = remove_duplicate_coords
  @ptr = cs.ptr

  self
end

#to_line_string(options = {}) ⇒ Object



254
255
256
# File 'lib/ffi-geos/coordinate_sequence.rb', line 254

def to_line_string(options = {})
  Geos.create_line_string(self, srid: options[:srid])
end

#to_linear_ring(options = {}) ⇒ Object



250
251
252
# File 'lib/ffi-geos/coordinate_sequence.rb', line 250

def to_linear_ring(options = {})
  Geos.create_linear_ring(self, srid: options[:srid])
end

#to_point(options = {}) ⇒ Object



246
247
248
# File 'lib/ffi-geos/coordinate_sequence.rb', line 246

def to_point(options = {})
  Geos.create_point(self, srid: options[:srid])
end

#to_polygon(options = {}) ⇒ Object



258
259
260
# File 'lib/ffi-geos/coordinate_sequence.rb', line 258

def to_polygon(options = {})
  Geos.create_polygon(self, srid: options[:srid])
end

#to_sObject



262
263
264
265
266
# File 'lib/ffi-geos/coordinate_sequence.rb', line 262

def to_s
  entries.collect { |entry|
    entry.join(' ')
  }.join(', ')
end

#trans_scale(*args, **kwargs) ⇒ Object



517
518
519
# File 'lib/ffi-geos/coordinate_sequence.rb', line 517

def trans_scale(*args, **kwargs)
  dup.trans_scale!(*args, **kwargs)
end

#trans_scale!(*args, **kwargs) ⇒ Object



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
514
515
# File 'lib/ffi-geos/coordinate_sequence.rb', line 487

def trans_scale!(*args, **kwargs)
  delta_x, delta_y, x_factor, y_factor = if !kwargs.empty?
    kwargs.values_at(:delta_x, :delta_y, :x_factor, :y_factor)
  elsif args.length.between?(1, 4)
    args.values_at(0...4)
  else
    raise ArgumentError, "Wrong number of arguments #{args.length} for 1-4"
  end

  x_factor ||= 1
  y_factor ||= 1
  delta_x ||= 0
  delta_y ||= 0

  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, **kwargs) ⇒ Object



546
547
548
# File 'lib/ffi-geos/coordinate_sequence.rb', line 546

def translate(*args, **kwargs)
  dup.translate!(*args, **kwargs)
end

#translate!(*args, **kwargs) ⇒ Object



521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
# File 'lib/ffi-geos/coordinate_sequence.rb', line 521

def translate!(*args, **kwargs)
  x, y, z = if !kwargs.empty?
    kwargs.values_at(:x, :y, :z)
  elsif args.length.between?(1, 3)
    args.values_at(0...3)
  else
    raise ArgumentError, "Wrong number of arguments #{args.length} for 1-3"
  end

  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