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:


144
145
146
147
148
# File 'lib/rgeo/feature/types.rb', line 144

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

Instance Method Details

#_add_subtype(type_) ⇒ Object

:nodoc:


139
140
141
# File 'lib/rgeo/feature/types.rb', line 139

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.


99
100
101
102
# File 'lib/rgeo/feature/types.rb', line 99

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

#each_immediate_subtype(&block_) ⇒ Object

Iterates over the known immediate subtypes of this type.


124
125
126
# File 'lib/rgeo/feature/types.rb', line 124

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)

109
110
111
# File 'lib/rgeo/feature/types.rb', line 109

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

#supertypeObject

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


117
118
119
# File 'lib/rgeo/feature/types.rb', line 117

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"

133
134
135
# File 'lib/rgeo/feature/types.rb', line 133

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