Class: RGeo::GeoJSON::Feature
- Inherits:
-
Object
- Object
- RGeo::GeoJSON::Feature
- 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 Attribute Summary collapse
-
#geometry ⇒ Object
readonly
Returns the geometry contained in this feature, which may be nil.
Instance Method Summary collapse
-
#==(other) ⇒ Object
Two features are equal if their geometries, IDs, and properties are all equal.
-
#eql?(other) ⇒ Boolean
Two features are equal if their geometries, IDs, and properties are all equal.
-
#feature_id ⇒ Object
Returns the ID for this feature, which may be nil.
- #hash ⇒ Object
-
#initialize(geometry, id = nil, properties = {}) ⇒ Feature
constructor
Create a feature wrapping the given geometry, with the given ID and properties.
- #inspect ⇒ Object
-
#keys ⇒ Object
Gets an array of the known property keys in this feature.
-
#properties ⇒ Object
Returns a copy of the properties for this feature.
-
#property(key) ⇒ Object
(also: #[])
Gets the value of the given named property.
- #to_s ⇒ Object
Constructor Details
#initialize(geometry, id = nil, properties = {}) ⇒ Feature
Create a feature wrapping the given geometry, with the given ID and properties.
19 20 21 22 23 24 25 26 |
# File 'lib/rgeo/geo_json/entities.rb', line 19 def initialize(geometry, id = nil, properties = {}) @geometry = geometry @id = id @properties = {} properties.each do |k, v| @properties[k.to_s] = v end end |
Instance Attribute Details
#geometry ⇒ Object (readonly)
Returns the geometry contained in this feature, which may be nil.
60 61 62 |
# File 'lib/rgeo/geo_json/entities.rb', line 60 def geometry @geometry end |
Instance Method Details
#==(other) ⇒ 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.
54 55 56 |
# File 'lib/rgeo/geo_json/entities.rb', line 54 def ==(other) other.is_a?(Feature) && @geometry == other.geometry && @id == other.feature_id && @properties == other.instance_variable_get(:@properties) end |
#eql?(other) ⇒ 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.
45 46 47 |
# File 'lib/rgeo/geo_json/entities.rb', line 45 def eql?(other) other.is_a?(Feature) && @geometry.eql?(other.geometry) && @id.eql?(other.feature_id) && @properties.eql?(other.instance_variable_get(:@properties)) end |
#feature_id ⇒ Object
Returns the ID for this feature, which may be nil.
64 65 66 |
# File 'lib/rgeo/geo_json/entities.rb', line 64 def feature_id @id end |
#hash ⇒ Object
36 37 38 |
# File 'lib/rgeo/geo_json/entities.rb', line 36 def hash @geometry.hash + @id.hash + @properties.hash end |
#inspect ⇒ Object
28 29 30 |
# File 'lib/rgeo/geo_json/entities.rb', line 28 def inspect "#<#{self.class}:0x#{object_id.to_s(16)} id=#{@id.inspect} geom=#{@geometry ? @geometry.as_text.inspect : 'nil'}>" end |
#keys ⇒ Object
Gets an array of the known property keys in this feature.
84 85 86 |
# File 'lib/rgeo/geo_json/entities.rb', line 84 def keys @properties.keys end |
#properties ⇒ Object
Returns a copy of the properties for this feature.
70 71 72 |
# File 'lib/rgeo/geo_json/entities.rb', line 70 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.
77 78 79 |
# File 'lib/rgeo/geo_json/entities.rb', line 77 def property(key) @properties[key.to_s] end |
#to_s ⇒ Object
32 33 34 |
# File 'lib/rgeo/geo_json/entities.rb', line 32 def to_s inspect end |