Module: RGeo::ImplHelper::ValidityCheck

Overview

This helper enforces valid geometry computation, avoiding results such as a 0 area for a bowtie shaped polygon. Implementations that are part of RGeo core should all include this.

You can play around validity checks if needed:

  • #check_validity! is the method that will raise if your geometry is not valid. Its message will be the same as #invalid_reason.

  • #make_valid is the method you can call to get a valid copy of the current geometry.

  • finally, you can bypass any checked method by prepending ‘unsafe_` to it. At your own risk.

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.included(klass) ⇒ Object

:nodoc:



59
60
61
# File 'lib/rgeo/impl_helper/validity_check.rb', line 59

def included(klass) # :nodoc:
  classes << klass
end

.override_classesObject

Note for contributors: this should be called after all methods are loaded for a given feature classe. No worries though, this is tested.



52
53
54
55
56
57
# File 'lib/rgeo/impl_helper/validity_check.rb', line 52

def override_classes # :nodoc:
  # Using pop here to be thread safe.
  while (klass = classes.pop)
    override(klass)
  end
end

Instance Method Details

#check_validity!Object

Raises #invalid_reason if the polygon is not valid, does nothing otherwise.



100
101
102
103
104
105
106
# File 'lib/rgeo/impl_helper/validity_check.rb', line 100

def check_validity!
  # This method will use a cached invalid_reason for performance purposes.
  # DO NOT MUTATE GEOMETRIES.
  return unless invalid_reason_memo

  raise Error::InvalidGeometry, invalid_reason_memo
end

#invalid_reasonObject

Tell why the geometry is not valid, ‘nil` means it is valid.



109
110
111
112
113
114
115
116
# File 'lib/rgeo/impl_helper/validity_check.rb', line 109

def invalid_reason
  if defined?(super) == "super"
    raise Error::RGeoError, "ValidityCheck MUST be loaded before " \
                            "definition of #{self.class}##{__method__}."
  end

  raise Error::UnsupportedOperation, "Method #{self.class}##{__method__} not defined."
end

#make_validObject

Try and make the geometry valid, this may change its shape. Returns a valid copy of the geometry.



120
121
122
123
124
125
126
127
# File 'lib/rgeo/impl_helper/validity_check.rb', line 120

def make_valid
  if defined?(super) == "super"
    raise Error::RGeoError, "ValidityCheck MUST be loaded before " \
                            "definition of #{self.class}##{__method__}."
  end

  raise Error::UnsupportedOperation, "Method #{self.class}##{__method__} not defined."
end