Class: GeoRuby::KmlParser

Inherits:
Object
  • Object
show all
Defined in:
lib/geo_ruby/kml.rb

Constant Summary collapse

ELEMENT_MAP =
{
  # "Point" => SimpleFeatures::Point, # we don't need to map points.  they are done automatically by the coordinate parsing
  'LineString' => SimpleFeatures::LineString,
  'LinearRing' => SimpleFeatures::LinearRing,
  'Polygon' => SimpleFeatures::Polygon,
  'MultiGeometry' => SimpleFeatures::GeometryCollection
}

Instance Method Summary collapse

Constructor Details

#initialize(factory) ⇒ KmlParser

Returns a new instance of KmlParser.



12
13
14
15
16
# File 'lib/geo_ruby/kml.rb', line 12

def initialize(factory)
  @factory = factory
  @buffer = ''
  @with_z = false
end

Instance Method Details

#parse(kml) ⇒ Object

argument should be a valid kml geometry fragment ie. <Point> .… </Point> returns the GeoRuby geometry object back



20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/geo_ruby/kml.rb', line 20

def parse(kml)
  @factory.reset
  @with_z = false
  @parser = REXML::Parsers::PullParser.new(kml)
  while @parser.has_next?
    e = @parser.pull
    if e.start_element?
      if (type = ELEMENT_MAP[e[0]])
        @factory.begin_geometry(type)
      else
        @buffer = '' if (e[0] == 'coordinates') # clear the buffer
        accumulate_start(e)
      end
    elsif e.end_element?
      if ELEMENT_MAP[e[0]]
        @factory.end_geometry(@with_z)
        @buffer = '' # clear the buffer
      else
        accumulate_end(e)
        if (e[0] == 'coordinates')
          parse_coordinates(@buffer)
          @buffer = '' # clear the buffer
        end
      end
    elsif e.text?
      accumulate_text(e)
    elsif e.cdata?
      accumulate_cdata(e)
    end
  end
  @factory.geometry.dup
end