class RGeo::Kml::Coder

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

Constants

ELEMENT_MAP

Public Class Methods

new(opts_={}) click to toggle source

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 Kml entities that are created. It defaults to the default RGeo::Kml::EntityFactory, which generates objects of type RGeo::Kml::Feature or RGeo::Kml::FeatureCollection. See RGeo::Kml::EntityFactory for more information.

# File lib/rgeo/kml/coder.rb, line 49
def initialize(opts_={})
  @geo_factory = opts_[:geo_factory] || ::RGeo::Cartesian.preferred_factory
  @entity_factory = opts_[:entity_factory] || EntityFactory.instance
  @buffer = ''
  
  @num_coordinates = 2
  @num_coordinates += 1 if @geo_factory.property(:has_z_coordinate)
  @num_coordinates += 1 if @geo_factory.property(:has_m_coordinate)
end

Public Instance Methods

decode(input_) click to toggle source

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

# File lib/rgeo/kml/coder.rb, line 212
def decode(input_) 
  @kml_stream_listener = KmlStreamListener.new(geo_factory)
  @kml_stream_listener.parse(input_)
  @kml_stream_listener.result
end
encode(object_) click to toggle source

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

This method returns a KML object (xml with Placemark).

Returns nil if nil is passed in as the object.

# File lib/rgeo/kml/coder.rb, line 68
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
entity_factory() click to toggle source

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

# File lib/rgeo/kml/coder.rb, line 94
def entity_factory
  @entity_factory
end
geo_factory() click to toggle source

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

# File lib/rgeo/kml/coder.rb, line 86
def geo_factory
  @geo_factory
end