File: tc_gc.rb

package info (click to toggle)
ruby-libxml 2.3.2-1
  • links: PTS, VCS
  • area: main
  • in suites: wheezy
  • size: 1,812 kB
  • sloc: xml: 9,628; ruby: 7,119; ansic: 6,665; makefile: 2
file content (86 lines) | stat: -rw-r--r-- 2,131 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
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
52
53
54
55
56
57
58
59
60
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
# encoding: UTF-8
require './test_helper'
require 'test/unit'

class TestFoo < Test::Unit::TestCase
  def namespaces
    {'kml' => 'http://earth.google.com/kml/2.2'}
  end

  def doc
    path = File.expand_path(File.join('model', 'kml_sample.xml'))
    data = File.read(path)
    parser = LibXML::XML::Parser.string(data)
    doc = parser.parse
    result = doc.root
    doc = nil
    GC.start
  end

  def test_read
    1000.times do
      read_placemarks
      GC.start
    end
  end

  def read_placemarks
    root = doc.root
    result = Array.new
    root.find('//kml:Placemark', self.namespaces).each do |entry|
      result << self.read_placemark(entry)
    end
    result
  end

  def read_placemark(entry)
    geometries = read_geoms(entry)
    #stop processing this placemark if there aren't any geometries.
    return if geometries.empty?
  end

  def read_geoms(entry)

    geoms = []
    entry.find('//kml:Point', self.namespaces).each do |point_entry|
      geoms << parse_point(point_entry)
    end

    entry.find('//kml:LineString', self.namespaces).each do |point_entry|
      geoms << parse_point(point_entry)
    end

    entry.find('//kml:Polygon', self.namespaces).each do |point_entry|
      geoms << parse_polygon(point_entry)
    end

    geoms
  end

  def parse_point(entry)
    coordinate_entry = entry.find('//kml:coordinates',self.namespaces).first
    coordinate_entry.content.split(",")
  end

  def parse_coordinate_string(entry)
    coordinates = entry.content.split
  end

  def parse_line_string(entry)
    coord_sequence = parse_coordinate_string(entry.find('kml:coordinates',self.namespaces).first)
  end

  def parse_linear_ring(entry)
    coord_sequence = parse_coordinate_string(entry.find('kml:coordinates',self.namespaces).first)
  end

  def parse_polygon(entry)
    exterior_ring = parse_linear_ring(entry.find('kml:outerBoundaryIs/kml:LinearRing', self.namespaces).first)

    interior_rings = []
    entry.find('kml:innerBoundaryIs/kml:LinearRing', self.namespaces).each do |interior_ring|
      interior_rings << parse_linear_ring(interior_ring)
    end
    interior_rings
  end
end