Module: RGeo::Feature::Type

Overview

This module provides the API for geometry type objects. Technically these objects are modules (such as ::RGeo::Feature::Point), but as objects they respond to the methods documented here.

For example, you may determine whether a feature object is a point by calling:

::RGeo::Feature::Point.check_type(object)

A corresponding === operator is provided so you can use the type modules in a case-when clause:

case object
when ::RGeo::Feature::Point
  # do stuff here...

However, a feature object may not actually include the point module itself; hence, the following will not work:

object.is_a?(::RGeo::Feature::Point)  # DON'T DO THIS-- DOES NOT WORK

You may obtain the type of a feature object by calling its geometry_type method. You may then use the methods in this module to interrogate that type.

# supppose object is a Point
type = object.geometry_type  # ::RGeo::Feature::Point
type.type_name               # "Point"
type.supertype               # ::RGeo::Feature::Geometry

You may also use the presence of this module to determine whether a particular object is a feature type:

::RGeo::Feature::Type === object.geometry_type  # true

Constant Summary collapse

Instance =

Deprecated alias for RGeo::Feature::Instance

Feature::Instance

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.extended(type_) ⇒ Object

:nodoc:



101
102
103
104
105
# File 'lib/rgeo/feature/types.rb', line 101

def self.extended(type_) # :nodoc:
  supertype_ = type_.included_modules.find { |m_| m_.is_a?(self) }
  type_.instance_variable_set(:@supertype, supertype_)
  supertype_._add_subtype(type_) if supertype_
end

Instance Method Details

#_add_subtype(type_) ⇒ Object

:nodoc:



97
98
99
# File 'lib/rgeo/feature/types.rb', line 97

def _add_subtype(type_) # :nodoc:
  (@subtypes ||= []) << type_
end

#check_type(rhs_) ⇒ Object Also known as: ===

Returns true if the given object is this type or a subtype thereof, or if it is a feature object whose geometry_type is this type or a subtype thereof.

Note that feature objects need not actually include this module. Therefore, the is_a? method will generally not work.



62
63
64
65
# File 'lib/rgeo/feature/types.rb', line 62

def check_type(rhs_)
  rhs_ = rhs_.geometry_type if rhs_.is_a?(Feature::Instance)
  rhs_.is_a?(Type) && (rhs_ == self || rhs_.include?(self))
end

#each_immediate_subtype(&block_) ⇒ Object

Iterates over the known immediate subtypes of this type.



84
85
86
# File 'lib/rgeo/feature/types.rb', line 84

def each_immediate_subtype(&block_)
  @subtypes.each(&block_) if defined?(@subtypes) && @subtypes
end

#subtype_of?(type_) ⇒ Boolean

Returns true if this type is the same type or a subtype of the given type.

Returns:

  • (Boolean)


71
72
73
# File 'lib/rgeo/feature/types.rb', line 71

def subtype_of?(type_)
  self == type_ || self.include?(type_)
end

#supertypeObject

Returns the supertype of this type. The supertype of Geometry is nil.



78
79
80
# File 'lib/rgeo/feature/types.rb', line 78

def supertype
  @supertype
end

#type_nameObject Also known as: to_s

Returns the OpenGIS type name of this type. For example:

::RGeo::Feature::Point.type_name  # "Point"


92
93
94
# File 'lib/rgeo/feature/types.rb', line 92

def type_name
  name.sub("RGeo::Feature::", "")
end