Class: Geos::Geometry

Inherits:
Object
  • Object
show all
Includes:
Tools
Defined in:
lib/ffi-geos/geometry.rb

Direct Known Subclasses

GeometryCollection, LineString, Point, Polygon

Defined Under Namespace

Classes: CouldntNormalizeError

Constant Summary

Constants included from GeomTypes

Geos::GeomTypes::GEOS_GEOMETRYCOLLECTION, Geos::GeomTypes::GEOS_LINEARRING, Geos::GeomTypes::GEOS_LINESTRING, Geos::GeomTypes::GEOS_MULTILINESTRING, Geos::GeomTypes::GEOS_MULTIPOINT, Geos::GeomTypes::GEOS_MULTIPOLYGON, Geos::GeomTypes::GEOS_POINT, Geos::GeomTypes::GEOS_POLYGON

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Tools

#bool_result, #bool_to_int, #cast_geometry_ptr, #check_enum_value, #check_geometry, #extract_options!, #pick_srid_according_to_policy, #pick_srid_from_geoms, #symbol_for_enum

Constructor Details

#initialize(ptr, options = {}) ⇒ Geometry

For internal use. Geometry objects should be created via WkbReader, WktReader and the various Geos.create_* methods.



17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/ffi-geos/geometry.rb', line 17

def initialize(ptr, options = {})
  options = {
    auto_free: true
  }.merge(options)

  @ptr = FFI::AutoPointer.new(
    ptr,
    self.class.method(:release)
  )

  @ptr.autorelease = !!options[:auto_free]
  @parent = options[:parent] if options[:parent]
end

Instance Attribute Details

#ptrObject (readonly)

Returns the value of attribute ptr.



7
8
9
# File 'lib/ffi-geos/geometry.rb', line 7

def ptr
  @ptr
end

Class Method Details

.release(ptr) ⇒ Object

:nodoc:



41
42
43
# File 'lib/ffi-geos/geometry.rb', line 41

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

Instance Method Details

#==(other) ⇒ Object



339
340
341
342
343
# File 'lib/ffi-geos/geometry.rb', line 339

def ==(other)
  return eql?(other) if other.is_a?(Geos::Geometry)

  false
end

#areaObject



447
448
449
450
451
452
453
# File 'lib/ffi-geos/geometry.rb', line 447

def area
  return 0 if empty?

  double_ptr = FFI::MemoryPointer.new(:double)
  FFIGeos.GEOSArea_r(Geos.current_handle_pointer, ptr, double_ptr)
  double_ptr.read_double
end

#boundaryObject



155
156
157
# File 'lib/ffi-geos/geometry.rb', line 155

def boundary
  cast_geometry_ptr(FFIGeos.GEOSBoundary_r(Geos.current_handle_pointer, ptr), srid_copy: srid)
end

#buffer(width, options = nil) ⇒ Object

:call-seq:

buffer(width)
buffer(width, options)
buffer(width, buffer_params)
buffer(width, quad_segs)

Calls buffer on the Geometry. Options can be passed as either a BufferParams object, as an equivalent Hash or as a quad_segs value. Default values can be found in Geos::Constants::BUFFER_PARAM_DEFAULTS.

Note that when using versions of GEOS prior to 3.3.0, only the quad_segs option is recognized when using Geometry#buffer and other options are ignored.



107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
# File 'lib/ffi-geos/geometry.rb', line 107

def buffer(width, options = nil)
  options ||= {}
  params = case options
    when Hash
      Geos::BufferParams.new(options)
    when Geos::BufferParams
      options
    when Numeric
      Geos::BufferParams.new(quad_segs: options)
    else
      raise ArgumentError, 'Expected Geos::BufferParams, a Hash or a Numeric'
  end

  cast_geometry_ptr(FFIGeos.GEOSBufferWithParams_r(Geos.current_handle_pointer, ptr, params.ptr, width), srid_copy: srid)
end

#centroidObject Also known as: center



206
207
208
# File 'lib/ffi-geos/geometry.rb', line 206

def centroid
  cast_geometry_ptr(FFIGeos.GEOSGetCentroid_r(Geos.current_handle_pointer, ptr), srid_copy: srid)
end

#clip_by_rect(xmin, ymin, xmax, ymax) ⇒ Object Also known as: clip_by_rectangle

Available in GEOS 3.5.0+.



200
201
202
# File 'lib/ffi-geos/geometry.rb', line 200

def clip_by_rect(xmin, ymin, xmax, ymax)
  cast_geometry_ptr(FFIGeos.GEOSClipByRect_r(Geos.current_handle_pointer, ptr, xmin, ymin, xmax, ymax))
end

#contains?(geom) ⇒ Boolean

Returns:

  • (Boolean)


279
280
281
282
# File 'lib/ffi-geos/geometry.rb', line 279

def contains?(geom)
  check_geometry(geom)
  bool_result(FFIGeos.GEOSContains_r(Geos.current_handle_pointer, ptr, geom.ptr))
end

#convex_hullObject



140
141
142
# File 'lib/ffi-geos/geometry.rb', line 140

def convex_hull
  cast_geometry_ptr(FFIGeos.GEOSConvexHull_r(Geos.current_handle_pointer, ptr), srid_copy: srid)
end

#coord_seqObject



84
85
86
# File 'lib/ffi-geos/geometry.rb', line 84

def coord_seq
  CoordinateSequence.new(FFIGeos.GEOSGeom_getCoordSeq_r(Geos.current_handle_pointer, ptr), false, self)
end

#covered_by?(geom) ⇒ Boolean

:nodoc:

Returns:

  • (Boolean)


315
316
317
318
# File 'lib/ffi-geos/geometry.rb', line 315

def covered_by?(geom)
  check_geometry(geom)
  bool_result(FFIGeos.GEOSCoveredBy_r(Geos.current_handle_pointer, ptr, geom.ptr))
end

#covers?(geom) ⇒ Boolean

:nodoc:

Returns:

  • (Boolean)


293
294
295
296
# File 'lib/ffi-geos/geometry.rb', line 293

def covers?(geom)
  check_geometry(geom)
  bool_result(FFIGeos.GEOSCovers_r(Geos.current_handle_pointer, ptr, geom.ptr))
end

#crosses?(geom) ⇒ Boolean

Returns:

  • (Boolean)


269
270
271
272
# File 'lib/ffi-geos/geometry.rb', line 269

def crosses?(geom)
  check_geometry(geom)
  bool_result(FFIGeos.GEOSCrosses_r(Geos.current_handle_pointer, ptr, geom.ptr))
end

#delaunay_triangulation(*args) ⇒ Object

:call-seq:

 delaunay_triangulation(options = {})
 delaunay_triangulation(tolerance, options = {})

Options:

* :tolerance
* :only_edges


555
556
557
558
559
560
561
562
# File 'lib/ffi-geos/geometry.rb', line 555

def delaunay_triangulation(*args)
  options = extract_options!(args)

  tolerance = args.first || options[:tolerance] || 0.0
  only_edges = bool_to_int(options[:only_edges])

  cast_geometry_ptr(FFIGeos.GEOSDelaunayTriangulation_r(Geos.current_handle_pointer, ptr, tolerance, only_edges))
end

#difference(geom) ⇒ Object



144
145
146
147
# File 'lib/ffi-geos/geometry.rb', line 144

def difference(geom)
  check_geometry(geom)
  cast_geometry_ptr(FFIGeos.GEOSDifference_r(Geos.current_handle_pointer, ptr, geom.ptr), srid_copy: pick_srid_from_geoms(srid, geom.srid))
end

#dimensionsObject



72
73
74
# File 'lib/ffi-geos/geometry.rb', line 72

def dimensions
  FFIGeos.GEOSGeom_getDimensions_r(Geos.current_handle_pointer, ptr)
end

#disjoint?(geom) ⇒ Boolean

Returns:

  • (Boolean)


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

def disjoint?(geom)
  check_geometry(geom)
  bool_result(FFIGeos.GEOSDisjoint_r(Geos.current_handle_pointer, ptr, geom.ptr))
end

#distance(geom) ⇒ Object



463
464
465
466
467
468
# File 'lib/ffi-geos/geometry.rb', line 463

def distance(geom)
  check_geometry(geom)
  double_ptr = FFI::MemoryPointer.new(:double)
  FFIGeos.GEOSDistance_r(Geos.current_handle_pointer, ptr, geom.ptr, double_ptr)
  double_ptr.read_double
end

#empty?Boolean

Returns:

  • (Boolean)


359
360
361
# File 'lib/ffi-geos/geometry.rb', line 359

def empty?
  bool_result(FFIGeos.GEOSisEmpty_r(Geos.current_handle_pointer, ptr))
end

#end_pointObject



443
444
445
# File 'lib/ffi-geos/geometry.rb', line 443

def end_point
  cast_geometry_ptr(FFIGeos.GEOSGeomGetEndPoint_r(Geos.current_handle_pointer, ptr), srid_copy: srid)
end

#envelopeObject



211
212
213
# File 'lib/ffi-geos/geometry.rb', line 211

def envelope
  cast_geometry_ptr(FFIGeos.GEOSEnvelope_r(Geos.current_handle_pointer, ptr), srid_copy: srid)
end

#eql?(other) ⇒ Boolean Also known as: equals?

Returns:

  • (Boolean)


333
334
335
336
# File 'lib/ffi-geos/geometry.rb', line 333

def eql?(other)
  check_geometry(other)
  bool_result(FFIGeos.GEOSEquals_r(Geos.current_handle_pointer, ptr, other.ptr))
end

#eql_almost?(other, decimal = 6) ⇒ Boolean Also known as: equals_almost?, almost_equals?

Returns:

  • (Boolean)


352
353
354
355
# File 'lib/ffi-geos/geometry.rb', line 352

def eql_almost?(other, decimal = 6)
  check_geometry(other)
  bool_result(FFIGeos.GEOSEqualsExact_r(Geos.current_handle_pointer, ptr, other.ptr, 0.5 * 10 ** -decimal))
end

#eql_exact?(other, tolerance) ⇒ Boolean Also known as: equals_exact?, exactly_equals?

Returns:

  • (Boolean)


345
346
347
348
# File 'lib/ffi-geos/geometry.rb', line 345

def eql_exact?(other, tolerance)
  check_geometry(other)
  bool_result(FFIGeos.GEOSEqualsExact_r(Geos.current_handle_pointer, ptr, other.ptr, tolerance))
end

#extract_unique_pointsObject Also known as: unique_points



249
250
251
# File 'lib/ffi-geos/geometry.rb', line 249

def extract_unique_points
  cast_geometry_ptr(FFIGeos.GEOSGeom_extractUniquePoints_r(Geos.current_handle_pointer, ptr), srid_copy: srid)
end

#frechet_distance(geom, densify_frac = nil) ⇒ Object



660
661
662
663
664
665
666
667
668
669
670
671
672
# File 'lib/ffi-geos/geometry.rb', line 660

def frechet_distance(geom, densify_frac = nil)
  check_geometry(geom)

  double_ptr = FFI::MemoryPointer.new(:double)

  if densify_frac
    FFIGeos.GEOSFrechetDistanceDensify_r(Geos.current_handle_pointer, ptr, geom.ptr, densify_frac, double_ptr)
  else
    FFIGeos.GEOSFrechetDistance_r(Geos.current_handle_pointer, ptr, geom.ptr, double_ptr)
  end

  double_ptr.read_double
end

#geom_typeObject

Returns the name of the Geometry type, i.e. “Point”, “Polygon”, etc.



46
47
48
# File 'lib/ffi-geos/geometry.rb', line 46

def geom_type
  FFIGeos.GEOSGeomType_r(Geos.current_handle_pointer, ptr)
end

#has_z?Boolean

Returns:

  • (Boolean)


403
404
405
# File 'lib/ffi-geos/geometry.rb', line 403

def has_z?
  bool_result(FFIGeos.GEOSHasZ_r(Geos.current_handle_pointer, ptr))
end

#hausdorff_distance(geom, densify_frac = nil) ⇒ Object



470
471
472
473
474
475
476
477
478
479
480
481
482
# File 'lib/ffi-geos/geometry.rb', line 470

def hausdorff_distance(geom, densify_frac = nil)
  check_geometry(geom)

  double_ptr = FFI::MemoryPointer.new(:double)

  if densify_frac
    FFIGeos.GEOSHausdorffDistanceDensify_r(Geos.current_handle_pointer, ptr, geom.ptr, densify_frac, double_ptr)
  else
    FFIGeos.GEOSHausdorffDistance_r(Geos.current_handle_pointer, ptr, geom.ptr, double_ptr)
  end

  double_ptr.read_double
end

#initialize_copy(source) ⇒ Object



31
32
33
34
35
36
37
38
39
# File 'lib/ffi-geos/geometry.rb', line 31

def initialize_copy(source)
  @ptr = FFI::AutoPointer.new(
    FFIGeos.GEOSGeom_clone_r(Geos.current_handle_pointer, source.ptr),
    self.class.method(:release)
  )

  # Copy over SRID since GEOS does not
  self.srid = source.srid
end

#interpolate(d, normalized = false) ⇒ Object



425
426
427
428
429
430
431
432
433
# File 'lib/ffi-geos/geometry.rb', line 425

def interpolate(d, normalized = false)
  ret = if normalized
    FFIGeos.GEOSInterpolateNormalized_r(Geos.current_handle_pointer, ptr, d)
  else
    FFIGeos.GEOSInterpolate_r(Geos.current_handle_pointer, ptr, d)
  end

  cast_geometry_ptr(ret, srid_copy: srid)
end

#interpolate_normalized(d) ⇒ Object



435
436
437
# File 'lib/ffi-geos/geometry.rb', line 435

def interpolate_normalized(d)
  interpolate(d, true)
end

#intersection(geom) ⇒ Object



88
89
90
91
# File 'lib/ffi-geos/geometry.rb', line 88

def intersection(geom)
  check_geometry(geom)
  cast_geometry_ptr(FFIGeos.GEOSIntersection_r(Geos.current_handle_pointer, ptr, geom.ptr), srid_copy: pick_srid_from_geoms(srid, geom.srid))
end

#intersects?(geom) ⇒ Boolean

Returns:

  • (Boolean)


264
265
266
267
# File 'lib/ffi-geos/geometry.rb', line 264

def intersects?(geom)
  check_geometry(geom)
  bool_result(FFIGeos.GEOSIntersects_r(Geos.current_handle_pointer, ptr, geom.ptr))
end

#lengthObject



455
456
457
458
459
460
461
# File 'lib/ffi-geos/geometry.rb', line 455

def length
  return 0 if empty?

  double_ptr = FFI::MemoryPointer.new(:double)
  FFIGeos.GEOSLength_r(Geos.current_handle_pointer, ptr, double_ptr)
  double_ptr.read_double
end

#line_mergeObject



237
238
239
# File 'lib/ffi-geos/geometry.rb', line 237

def line_merge
  cast_geometry_ptr(FFIGeos.GEOSLineMerge_r(Geos.current_handle_pointer, ptr), srid_copy: srid)
end

#minimum_clearanceObject



634
635
636
637
638
# File 'lib/ffi-geos/geometry.rb', line 634

def minimum_clearance
  double_ptr = FFI::MemoryPointer.new(:double)
  FFIGeos.GEOSMinimumClearance_r(Geos.current_handle_pointer, ptr, double_ptr)
  double_ptr.read_double
end

#minimum_clearance_lineObject



642
643
644
# File 'lib/ffi-geos/geometry.rb', line 642

def minimum_clearance_line
  cast_geometry_ptr(FFIGeos.GEOSMinimumClearanceLine_r(Geos.current_handle_pointer, ptr))
end

#minimum_rotated_rectangleObject



628
629
630
# File 'lib/ffi-geos/geometry.rb', line 628

def minimum_rotated_rectangle
  cast_geometry_ptr(FFIGeos.GEOSMinimumRotatedRectangle_r(Geos.current_handle_pointer, ptr))
end

#minimum_widthObject



648
649
650
# File 'lib/ffi-geos/geometry.rb', line 648

def minimum_width
  cast_geometry_ptr(FFIGeos.GEOSMinimumWidth_r(Geos.current_handle_pointer, ptr))
end

#nearest_points(geom) ⇒ Object

Available in GEOS 3.4+.



486
487
488
489
490
491
# File 'lib/ffi-geos/geometry.rb', line 486

def nearest_points(geom)
  check_geometry(geom)
  nearest_points_ptr = FFIGeos.GEOSNearestPoints_r(Geos.current_handle_pointer, ptr, geom.ptr)

  return CoordinateSequence.new(nearest_points_ptr) unless nearest_points_ptr.null?
end

#nodeObject

Available in GEOS 3.3.4+



188
189
190
# File 'lib/ffi-geos/geometry.rb', line 188

def node
  cast_geometry_ptr(FFIGeos.GEOSNode_r(Geos.current_handle_pointer, ptr))
end

#normalize!Object Also known as: normalize



55
56
57
58
59
60
61
# File 'lib/ffi-geos/geometry.rb', line 55

def normalize!
  if FFIGeos.GEOSNormalize_r(Geos.current_handle_pointer, ptr) == -1
    raise Geos::Geometry::CouldntNormalizeError, self.class
  end

  self
end

#num_coordinatesObject



80
81
82
# File 'lib/ffi-geos/geometry.rb', line 80

def num_coordinates
  FFIGeos.GEOSGetNumCoordinates_r(Geos.current_handle_pointer, ptr)
end

#num_geometriesObject



76
77
78
# File 'lib/ffi-geos/geometry.rb', line 76

def num_geometries
  FFIGeos.GEOSGetNumGeometries_r(Geos.current_handle_pointer, ptr)
end

#overlaps?(geom) ⇒ Boolean

Returns:

  • (Boolean)


284
285
286
287
# File 'lib/ffi-geos/geometry.rb', line 284

def overlaps?(geom)
  check_geometry(geom)
  bool_result(FFIGeos.GEOSOverlaps_r(Geos.current_handle_pointer, ptr, geom.ptr))
end

#point_on_surfaceObject Also known as: representative_point



193
194
195
# File 'lib/ffi-geos/geometry.rb', line 193

def point_on_surface
  cast_geometry_ptr(FFIGeos.GEOSPointOnSurface_r(Geos.current_handle_pointer, ptr), srid_copy: srid)
end

#polygonizeObject



532
533
534
535
536
537
# File 'lib/ffi-geos/geometry.rb', line 532

def polygonize
  ary = FFI::MemoryPointer.new(:pointer)
  ary.write_array_of_pointer([ ptr ])

  cast_geometry_ptr(FFIGeos.GEOSPolygonize_r(Geos.current_handle_pointer, ary, 1), srid_copy: srid).to_a
end

#polygonize_cut_edgesObject



539
540
541
542
543
544
# File 'lib/ffi-geos/geometry.rb', line 539

def polygonize_cut_edges
  ary = FFI::MemoryPointer.new(:pointer)
  ary.write_array_of_pointer([ ptr ])

  cast_geometry_ptr(FFIGeos.GEOSPolygonizer_getCutEdges_r(Geos.current_handle_pointer, ary, 1), srid_copy: srid).to_a
end

#polygonize_fullObject

Returns a Hash with the following structure:

{
  rings: [ ... ],
  cuts: [ ... ],
  dangles: [ ... ],
  invalid_rings: [ ... ]
}


513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
# File 'lib/ffi-geos/geometry.rb', line 513

def polygonize_full
  cuts = FFI::MemoryPointer.new(:pointer)
  dangles = FFI::MemoryPointer.new(:pointer)
  invalid_rings = FFI::MemoryPointer.new(:pointer)

  rings = cast_geometry_ptr(FFIGeos.GEOSPolygonize_full_r(Geos.current_handle_pointer, ptr, cuts, dangles, invalid_rings), srid_copy: srid)

  cuts = cast_geometry_ptr(cuts.read_pointer, srid_copy: srid)
  dangles = cast_geometry_ptr(dangles.read_pointer, srid_copy: srid)
  invalid_rings = cast_geometry_ptr(invalid_rings.read_pointer, srid_copy: srid)

  {
    rings: rings.to_a,
    cuts: cuts.to_a,
    dangles: dangles.to_a,
    invalid_rings: invalid_rings.to_a
  }
end

#precisionObject



606
607
608
# File 'lib/ffi-geos/geometry.rb', line 606

def precision
  FFIGeos.GEOSGeom_getPrecision_r(Geos.current_handle_pointer, ptr)
end

#project(geom, normalized = false) ⇒ Object

Raises:

  • (TypeError)


410
411
412
413
414
415
416
417
418
# File 'lib/ffi-geos/geometry.rb', line 410

def project(geom, normalized = false)
  raise TypeError, 'Expected Geos::Point type' unless geom.is_a?(Geos::Point)

  if normalized
    FFIGeos.GEOSProjectNormalized_r(Geos.current_handle_pointer, ptr, geom.ptr)
  else
    FFIGeos.GEOSProject_r(Geos.current_handle_pointer, ptr, geom.ptr)
  end
end

#project_normalized(geom) ⇒ Object



420
421
422
# File 'lib/ffi-geos/geometry.rb', line 420

def project_normalized(geom)
  project(geom, true)
end

#relate(geom) ⇒ Object

Returns the Dimensionally Extended Nine-Intersection Model (DE-9IM) matrix of the geometries as a String.



217
218
219
220
# File 'lib/ffi-geos/geometry.rb', line 217

def relate(geom)
  check_geometry(geom)
  FFIGeos.GEOSRelate_r(Geos.current_handle_pointer, ptr, geom.ptr)
end

#relate_boundary_node_rule(geom, bnr = :mod2) ⇒ Object

Available in GEOS 3.3+.



230
231
232
233
234
# File 'lib/ffi-geos/geometry.rb', line 230

def relate_boundary_node_rule(geom, bnr = :mod2)
  check_geometry(geom)
  check_enum_value(Geos::RelateBoundaryNodeRules, bnr)
  FFIGeos.GEOSRelateBoundaryNodeRule_r(Geos.current_handle_pointer, ptr, geom.ptr, bnr)
end

#relate_pattern(geom, pattern) ⇒ Object

Checks the DE-9IM pattern against the geoms.



223
224
225
226
# File 'lib/ffi-geos/geometry.rb', line 223

def relate_pattern(geom, pattern)
  check_geometry(geom)
  bool_result(FFIGeos.GEOSRelatePattern_r(Geos.current_handle_pointer, ptr, geom.ptr, pattern))
end

#reverseObject



654
655
656
# File 'lib/ffi-geos/geometry.rb', line 654

def reverse
  cast_geometry_ptr(FFIGeos.GEOSReverse_r(Geos.current_handle_pointer, ptr))
end

#ring?Boolean

Returns:

  • (Boolean)


399
400
401
# File 'lib/ffi-geos/geometry.rb', line 399

def ring?
  bool_result(FFIGeos.GEOSisRing_r(Geos.current_handle_pointer, ptr))
end

#shared_paths(geom) ⇒ Object



500
501
502
503
# File 'lib/ffi-geos/geometry.rb', line 500

def shared_paths(geom)
  check_geometry(geom)
  cast_geometry_ptr(FFIGeos.GEOSSharedPaths_r(Geos.current_handle_pointer, ptr, geom.ptr), srid_copy: pick_srid_from_geoms(srid, geom.srid)).to_a
end

#simple?Boolean

Returns:

  • (Boolean)


395
396
397
# File 'lib/ffi-geos/geometry.rb', line 395

def simple?
  bool_result(FFIGeos.GEOSisSimple_r(Geos.current_handle_pointer, ptr))
end

#simplify(tolerance) ⇒ Object



241
242
243
# File 'lib/ffi-geos/geometry.rb', line 241

def simplify(tolerance)
  cast_geometry_ptr(FFIGeos.GEOSSimplify_r(Geos.current_handle_pointer, ptr, tolerance), srid_copy: srid)
end

#snap(geom, tolerance) ⇒ Object Also known as: snap_to



494
495
496
497
# File 'lib/ffi-geos/geometry.rb', line 494

def snap(geom, tolerance)
  check_geometry(geom)
  cast_geometry_ptr(FFIGeos.GEOSSnap_r(Geos.current_handle_pointer, ptr, geom.ptr, tolerance), srid_copy: pick_srid_from_geoms(srid, geom.srid))
end

#sridObject



64
65
66
# File 'lib/ffi-geos/geometry.rb', line 64

def srid
  FFIGeos.GEOSGetSRID_r(Geos.current_handle_pointer, ptr)
end

#srid=(s) ⇒ Object



68
69
70
# File 'lib/ffi-geos/geometry.rb', line 68

def srid=(s)
  FFIGeos.GEOSSetSRID_r(Geos.current_handle_pointer, ptr, s)
end

#start_pointObject



439
440
441
# File 'lib/ffi-geos/geometry.rb', line 439

def start_point
  cast_geometry_ptr(FFIGeos.GEOSGeomGetStartPoint_r(Geos.current_handle_pointer, ptr), srid_copy: srid)
end

#sym_difference(geom) ⇒ Object Also known as: symmetric_difference



149
150
151
152
# File 'lib/ffi-geos/geometry.rb', line 149

def sym_difference(geom)
  check_geometry(geom)
  cast_geometry_ptr(FFIGeos.GEOSSymDifference_r(Geos.current_handle_pointer, ptr, geom.ptr), srid_copy: pick_srid_from_geoms(srid, geom.srid))
end

#to_preparedObject



593
594
595
# File 'lib/ffi-geos/geometry.rb', line 593

def to_prepared
  Geos::PreparedGeometry.new(self)
end

#to_sObject



597
598
599
600
601
602
603
# File 'lib/ffi-geos/geometry.rb', line 597

def to_s
  writer = WktWriter.new
  wkt = writer.write(self)
  wkt = "#{wkt[0...120]} ... " if wkt.length > 120

  "#<Geos::#{geom_type}: #{wkt}>"
end

#topology_preserve_simplify(tolerance) ⇒ Object



245
246
247
# File 'lib/ffi-geos/geometry.rb', line 245

def topology_preserve_simplify(tolerance)
  cast_geometry_ptr(FFIGeos.GEOSTopologyPreserveSimplify_r(Geos.current_handle_pointer, ptr, tolerance), srid_copy: srid)
end

#touches?(geom) ⇒ Boolean

Returns:

  • (Boolean)


259
260
261
262
# File 'lib/ffi-geos/geometry.rb', line 259

def touches?(geom)
  check_geometry(geom)
  bool_result(FFIGeos.GEOSTouches_r(Geos.current_handle_pointer, ptr, geom.ptr))
end

#type_idObject

Returns one of the values from Geos::GeomTypes.



51
52
53
# File 'lib/ffi-geos/geometry.rb', line 51

def type_id
  FFIGeos.GEOSGeomTypeId_r(Geos.current_handle_pointer, ptr)
end

#unary_unionObject

Available in GEOS 3.3+



181
182
183
# File 'lib/ffi-geos/geometry.rb', line 181

def unary_union
  cast_geometry_ptr(FFIGeos.GEOSUnaryUnion_r(Geos.current_handle_pointer, ptr), srid_copy: srid)
end

#union(geom = nil) ⇒ Object

Calling without a geom argument is equivalent to calling unary_union when using GEOS 3.3+ and is equivalent to calling union_cascaded in older versions.



162
163
164
165
166
167
168
169
170
171
172
173
# File 'lib/ffi-geos/geometry.rb', line 162

def union(geom = nil)
  if geom
    check_geometry(geom)
    cast_geometry_ptr(FFIGeos.GEOSUnion_r(Geos.current_handle_pointer, ptr, geom.ptr), srid_copy: pick_srid_from_geoms(srid, geom.srid))
  else
    if respond_to?(:unary_union)
      unary_union
    else
      union_cascaded
    end
  end
end

#union_cascadedObject



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

def union_cascaded
  cast_geometry_ptr(FFIGeos.GEOSUnionCascaded_r(Geos.current_handle_pointer, ptr), srid_copy: srid)
end

#valid?Boolean

Returns:

  • (Boolean)


363
364
365
# File 'lib/ffi-geos/geometry.rb', line 363

def valid?
  bool_result(FFIGeos.GEOSisValid_r(Geos.current_handle_pointer, ptr))
end

#valid_detail(flags = 0) ⇒ Object

Returns a Hash containing the following structure on invalid geometries:

{
  detail: "String explaining the problem",
  location: Geos::Point # centered on the problem
}

If the Geometry is valid, returns nil.



380
381
382
383
384
385
386
387
388
389
390
391
392
393
# File 'lib/ffi-geos/geometry.rb', line 380

def valid_detail(flags = 0)
  detail = FFI::MemoryPointer.new(:pointer)
  location = FFI::MemoryPointer.new(:pointer)
  valid = bool_result(
    FFIGeos.GEOSisValidDetail_r(Geos.current_handle_pointer, ptr, flags, detail, location)
  )

  return if valid

  {
    detail: detail.read_pointer.read_string,
    location: cast_geometry_ptr(location.read_pointer, srid_copy: srid)
  }
end

#valid_reasonObject

Returns a String describing whether or not the Geometry is valid.



368
369
370
# File 'lib/ffi-geos/geometry.rb', line 368

def valid_reason
  FFIGeos.GEOSisValidReason_r(Geos.current_handle_pointer, ptr)
end

#voronoi_diagram(*args) ⇒ Object

Available in GEOS 3.5.0+

:call-seq:

 voronoi_diagram(options = {})
 voronoi_diagram(tolerance, options = {})

Options:

* :tolerance
* :envelope
* :only_edges


577
578
579
580
581
582
583
584
585
586
587
588
589
590
# File 'lib/ffi-geos/geometry.rb', line 577

def voronoi_diagram(*args)
  options = extract_options!(args)

  tolerance = args.first || options[:tolerance] || 0.0

  envelope_ptr = if options[:envelope]
    check_geometry(options[:envelope])
    options[:envelope].ptr
  end

  only_edges = bool_to_int(options[:only_edges])

  cast_geometry_ptr(FFIGeos.GEOSVoronoiDiagram_r(Geos.current_handle_pointer, ptr, envelope_ptr, tolerance, only_edges))
end

#with_precision(grid_size, options = {}) ⇒ Object



612
613
614
615
616
617
618
619
620
621
622
623
624
# File 'lib/ffi-geos/geometry.rb', line 612

def with_precision(grid_size, options = {})
  options = {
    no_topology: false,
    keep_collapsed: false
  }.merge(options)

  flags = options.reduce(0) do |memo, (key, value)|
    memo |= Geos::PrecisionOptions[key] if value
    memo
  end

  cast_geometry_ptr(FFIGeos.GEOSGeom_setPrecision_r(Geos.current_handle_pointer, ptr, grid_size, flags))
end

#within?(geom) ⇒ Boolean

Returns:

  • (Boolean)


274
275
276
277
# File 'lib/ffi-geos/geometry.rb', line 274

def within?(geom)
  check_geometry(geom)
  bool_result(FFIGeos.GEOSWithin_r(Geos.current_handle_pointer, ptr, geom.ptr))
end