Class: RGeo::GeoJSON::Feature

Inherits:
Object
  • Object
show all
Defined in:
lib/rgeo/geo_json/entities.rb

Overview

This is a GeoJSON wrapper entity that corresponds to the GeoJSON “Feature” type. It is an immutable type.

This is the default implementation that is generated by RGeo::GeoJSON::EntityFactory. You may replace this implementation by writing your own entity factory. Note that an alternate Feature implementation need not subclass or even duck-type this class. the entity factory mediates all interaction between the GeoJSON engine and features.

Instance Method Summary collapse

Constructor Details

#initialize(geometry_, id_ = nil, properties_ = {}) ⇒ Feature

Create a feature wrapping the given geometry, with the given ID and properties.


22
23
24
25
26
27
28
29
# File 'lib/rgeo/geo_json/entities.rb', line 22

def initialize(geometry_, id_=nil, properties_={})
  @geometry = geometry_
  @id = id_
  @properties = {}
  properties_.each do |k_, v_|
    @properties[k_.to_s] = v_
  end
end

Instance Method Details

#==(rhs_) ⇒ Object

Two features are equal if their geometries, IDs, and properties are all equal. This method uses the == operator to test geometry equality, which may behave differently than the eql? method.


60
61
62
# File 'lib/rgeo/geo_json/entities.rb', line 60

def ==(rhs_)
  rhs_.kind_of?(Feature) && @geometry == rhs_.geometry && @id == rhs_.feature_id && @properties == rhs_.instance_variable_get(:@properties)
end

#eql?(rhs_) ⇒ Boolean

Two features are equal if their geometries, IDs, and properties are all equal. This method uses the eql? method to test geometry equality, which may behave differently than the == operator.

Returns:

  • (Boolean)

50
51
52
# File 'lib/rgeo/geo_json/entities.rb', line 50

def eql?(rhs_)
  rhs_.kind_of?(Feature) && @geometry.eql?(rhs_.geometry) && @id.eql?(rhs_.feature_id) && @properties.eql?(rhs_.instance_variable_get(:@properties))
end

#feature_idObject

Returns the ID for this feature, which may be nil.


74
75
76
# File 'lib/rgeo/geo_json/entities.rb', line 74

def feature_id
  @id
end

#geometryObject

Returns the geometry contained in this feature, which may be nil.


67
68
69
# File 'lib/rgeo/geo_json/entities.rb', line 67

def geometry
  @geometry
end

#hashObject

:nodoc:


40
41
42
# File 'lib/rgeo/geo_json/entities.rb', line 40

def hash  # :nodoc:
  @geometry.hash + @id.hash + @properties.hash
end

#inspectObject

:nodoc:


32
33
34
# File 'lib/rgeo/geo_json/entities.rb', line 32

def inspect  # :nodoc:
  "#<#{self.class}:0x#{object_id.to_s(16)} id=#{@id.inspect} geom=#{@geometry ? @geometry.as_text.inspect : 'nil'}>"
end

#keysObject

Gets an array of the known property keys in this feature.


97
98
99
# File 'lib/rgeo/geo_json/entities.rb', line 97

def keys
  @properties.keys
end

#propertiesObject

Returns a copy of the properties for this feature.


81
82
83
# File 'lib/rgeo/geo_json/entities.rb', line 81

def properties
  @properties.dup
end

#property(key_) ⇒ Object Also known as: []

Gets the value of the given named property. Returns nil if the given property is not found.


89
90
91
# File 'lib/rgeo/geo_json/entities.rb', line 89

def property(key_)
  @properties[key_.to_s]
end

#to_sObject

:nodoc:


36
37
38
# File 'lib/rgeo/geo_json/entities.rb', line 36

def to_s  # :nodoc:
  inspect
end