Class: RGeo::GeoJSON::Feature

Inherits:
Object
  • Object
show all
Includes:
ConversionMethods, DelegateToGeometry
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

Instance Method Summary collapse

Methods included from ConversionMethods

#as_geojson, #to_geojson

Constructor Details

#initialize(geometry, id = nil, properties = {}) ⇒ Feature

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


53
54
55
56
57
58
59
60
# File 'lib/rgeo/geo_json/entities.rb', line 53

def initialize(geometry, id = nil, properties = {})
  @geometry = geometry
  @id = id
  @properties = {}
  properties.each do |k, v|
    @properties[k.to_s] = v
  end
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method in the class RGeo::GeoJSON::DelegateToGeometry

Instance Attribute Details

#geometryObject (readonly)

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


91
92
93
# File 'lib/rgeo/geo_json/entities.rb', line 91

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.


86
87
88
# File 'lib/rgeo/geo_json/entities.rb', line 86

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.

Returns:

  • (Boolean)

78
79
80
# File 'lib/rgeo/geo_json/entities.rb', line 78

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_idObject

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


94
95
96
# File 'lib/rgeo/geo_json/entities.rb', line 94

def feature_id
  @id
end

#hashObject


70
71
72
# File 'lib/rgeo/geo_json/entities.rb', line 70

def hash
  @geometry.hash + @id.hash + @properties.hash
end

#inspectObject


62
63
64
# File 'lib/rgeo/geo_json/entities.rb', line 62

def inspect
  "#<#{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.


111
112
113
# File 'lib/rgeo/geo_json/entities.rb', line 111

def keys
  @properties.keys
end

#propertiesObject

Returns a copy of the properties for this feature.


99
100
101
# File 'lib/rgeo/geo_json/entities.rb', line 99

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.


105
106
107
# File 'lib/rgeo/geo_json/entities.rb', line 105

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

#to_sObject


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

def to_s
  inspect
end