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

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.extended(type) ⇒ Object

:nodoc:


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

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)
end

Instance Method Details

#add_subtype(type) ⇒ Object

:nodoc:


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

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.


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

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.


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

def each_immediate_subtype(&block)
  @subtypes&.each(&block)
end

#subtype_of?(type) ⇒ Boolean

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

Returns:

  • (Boolean)

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

def subtype_of?(type)
  self == type || include?(type)
end

#supertypeObject

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


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

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"

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

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