Class: RGeo::GeoJSON::Coder

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

Overview

This object encapsulates encoding and decoding settings (principally the RGeo::Feature::Factory and the RGeo::GeoJSON::EntityFactory to be used) so that you can encode and decode without specifying those settings every time.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(opts = {}) ⇒ Coder

Create a new coder settings object. The geo factory is passed as a required argument.

Options include:

:geo_factory

Specifies the geo factory to use to create geometry objects. Defaults to the preferred cartesian factory.

:entity_factory

Specifies an entity factory, which lets you override the types of GeoJSON entities that are created. It defaults to the default RGeo::GeoJSON::EntityFactory, which generates objects of type RGeo::GeoJSON::Feature or RGeo::GeoJSON::FeatureCollection. See RGeo::GeoJSON::EntityFactory for more information.



24
25
26
27
28
29
30
# File 'lib/rgeo/geo_json/coder.rb', line 24

def initialize(opts = {})
  @geo_factory = opts[:geo_factory] || RGeo::Cartesian.preferred_factory
  @entity_factory = opts[:entity_factory] || EntityFactory.instance
  @num_coordinates = 2
  @num_coordinates += 1 if @geo_factory.property(:has_z_coordinate)
  @num_coordinates += 1 if @geo_factory.property(:has_m_coordinate)
end

Instance Attribute Details

#entity_factoryObject (readonly)

Returns the RGeo::GeoJSON::EntityFactory used to generate GeoJSON wrapper entities.



95
96
97
# File 'lib/rgeo/geo_json/coder.rb', line 95

def entity_factory
  @entity_factory
end

#geo_factoryObject (readonly)

Returns the RGeo::Feature::Factory used to generate geometry objects.



90
91
92
# File 'lib/rgeo/geo_json/coder.rb', line 90

def geo_factory
  @geo_factory
end

Instance Method Details

#decode(input) ⇒ Object

Decode an object from GeoJSON. The input may be a JSON hash, a String, or an IO object from which to read the JSON string. If an error occurs, nil is returned.



61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
# File 'lib/rgeo/geo_json/coder.rb', line 61

def decode(input)
  if input.is_a?(IO)
    input = input.read rescue nil
  end
  if input.is_a?(String)
    input = MultiJson.load(input)
  end
  return unless input.is_a?(Hash)

  case input["type"]
  when "FeatureCollection"
    features = input["features"]
    features = [] unless features.is_a?(Array)
    decoded_features = []
    features.each do |f|
      if f["type"] == "Feature"
        decoded_features << decode_feature(f)
      end
    end
    @entity_factory.feature_collection(decoded_features)
  when "Feature"
    decode_feature(input)
  else
    decode_geometry(input)
  end
end

#encode(object) ⇒ Object

Encode the given object as GeoJSON. The object may be one of the geometry objects specified in RGeo::Feature, or an appropriate GeoJSON wrapper entity supported by this coder’s entity factory.

This method returns a JSON object (i.e. a hash). In order to generate a string suitable for transmitting to a service, you will need to JSON-encode it. This is usually accomplished by calling to_json on the hash object, if you have the appropriate JSON library installed.

Returns nil if nil is passed in as the object.



43
44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/rgeo/geo_json/coder.rb', line 43

def encode(object)
  if @entity_factory.is_feature_collection?(object)
    {
      "type" => "FeatureCollection",
      "features" => @entity_factory.map_feature_collection(object) { |f| encode_feature(f) },
    }
  elsif @entity_factory.is_feature?(object)
    encode_feature(object)
  elsif object.nil?
    nil
  else
    encode_geometry(object)
  end
end