Class: GmlImport

Inherits:
Object
  • Object
show all
Defined in:
lib/charta/gml_import.rb

Instance Method Summary collapse

Constructor Details

#initialize(data) ⇒ GmlImport

Returns a new instance of GmlImport.


4
5
6
7
# File 'lib/charta/gml_import.rb', line 4

def initialize(data)
  @shapes = nil
  @xml = data
end

Instance Method Details

#as_geojsonObject


40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/charta/gml_import.rb', line 40

def as_geojson
  geojson_features_collection = {}
  geojson_features = []

  if @shapes.is_a? Nokogiri::XML::Node

    geojson_features << featurize(@shapes)

  elsif @shapes.is_a? Nokogiri::XML::NodeSet

    @shapes.each do |node|
      geojson_features << featurize(node)
    end

  end

  geojson_features_collection = {
    type: 'FeatureCollection',
    features: geojson_features
  }

  geojson_features_collection
end

#sanitize(xml) ⇒ Object


16
17
18
# File 'lib/charta/gml_import.rb', line 16

def sanitize(xml)
  xml.to_s.squish
end

#shapes(options = {}) ⇒ Object


20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/charta/gml_import.rb', line 20

def shapes(options = {})
  options[:to] ||= ''

  f = sanitize @xml

  doc = Nokogiri::XML(f) do |config|
    config.options = Nokogiri::XML::ParseOptions::NOBLANKS
  end

  @shapes = doc.root

  if options[:to].equal? :xml
    @shapes = @shapes.to_xml
  elsif options[:to].equal? :string
    @shapes = @shapes.to_s
  else
    @shapes
  end
end

#valid?Boolean

Returns:

  • (Boolean)

9
10
11
12
13
14
# File 'lib/charta/gml_import.rb', line 9

def valid?
  shapes
  geometries = as_geojson || {}

  !geometries.empty?
end