Class: AIXM::Document

Inherits:
Object
  • Object
show all
Defined in:
lib/aixm/document.rb

Overview

The AIXM-Snapshot or OFMX-Snapshot document is the root container for aeronautical information such as airports or airspaces.

Cheat Sheet in Pseudo Code:

document = AIXM.document(
  region: String
  namespace: String (UUID)
  created_at: Time or Date or String
  effective_at: Time or Date or String
)
document.features << AIXM::Feature

Constant Summary collapse

REGION_RE =
/\A[A-Z]{2}\z/.freeze
NAMESPACE_RE =
/\A[a-f\d]{8}-[a-f\d]{4}-[a-f\d]{4}-[a-f\d]{4}-[a-f\d]{12}\z/.freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(region: nil, namespace: nil, created_at: nil, effective_at: nil) ⇒ Document


38
39
40
41
# File 'lib/aixm/document.rb', line 38

def initialize(region: nil, namespace: nil, created_at: nil, effective_at: nil)
  self.region, self.namespace, self.created_at, self.effective_at = region, namespace, created_at, effective_at
  @features = []
end

Instance Attribute Details

#created_atTime


30
31
32
# File 'lib/aixm/document.rb', line 30

def created_at
  @created_at
end

#effective_atTime


33
34
35
# File 'lib/aixm/document.rb', line 33

def effective_at
  @effective_at
end

#featuresArray<AIXM::Feature>


36
37
38
# File 'lib/aixm/document.rb', line 36

def features
  @features
end

#namespaceString


27
28
29
# File 'lib/aixm/document.rb', line 27

def namespace
  @namespace
end

#regionString


24
25
26
# File 'lib/aixm/document.rb', line 24

def region
  @region
end

Instance Method Details

#errorsArray<String>

Validate the generated AIXM or OFMX atainst it's XSD and return the errors found.


124
125
126
127
128
129
# File 'lib/aixm/document.rb', line 124

def errors
  xsd = Nokogiri::XML::Schema(File.open(AIXM.schema(:xsd)))
  xsd.validate(Nokogiri::XML(to_xml)).reject do |error|
    AIXM.config.ignored_errors && error.message.match?(AIXM.config.ignored_errors)
  end
end

#group_obstacles!(max_distance: AIXM.d(1, :nm)) ⇒ Integer

Compare all ungrouped obstacles and create new obstacle groups whose members are located within max_distance pairwise.


97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
# File 'lib/aixm/document.rb', line 97

def group_obstacles!(max_distance: AIXM.d(1, :nm))
  obstacles, list = select_features(:obstacle), {}
  while subject = obstacles.shift
    obstacles.each do |obstacle|
      if subject.xy.distance(obstacle.xy) <= max_distance
        [subject, obstacle].each { |o| list[o] = list[subject] || SecureRandom.uuid }
      end
    end
  end
  list.group_by(&:last).each do |_, grouped_list|
    obstacle_group = AIXM.obstacle_group(source: grouped_list.first.first.source)
    grouped_list.each { |o, _| obstacle_group.obstacles << features.delete(o) }
    features << obstacle_group
  end.count
end

#inspectString


44
45
46
# File 'lib/aixm/document.rb', line 44

def inspect
  %Q(#<#{self.class} created_at=#{created_at.inspect}>)
end

#select_features(klass, attributes = {}) ⇒ Array<AIXM::Feature>

Search features and return those matching the given class and attribute values

Examples:

select_features(:airport, id: "LFNT")

77
78
79
80
81
82
83
84
85
86
87
88
89
# File 'lib/aixm/document.rb', line 77

def select_features(klass, attributes={})
  if klass.is_a? Symbol
    klass = AIXM::CLASSES.fetch(klass, nil)
    fail(ArgumentError, "unknown feature shortcut") unless klass
  end
  features.select do |feature|
    if feature.is_a? klass
      attributes.reduce(true) do |memo, (attribute, value)|
        memo && feature.send(attribute) == value
      end
    end
  end
end

#to_xmlString


132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
# File 'lib/aixm/document.rb', line 132

def to_xml
  meta = {
    'xmlns:xsi': AIXM.schema(:namespace),
    version: AIXM.schema(:version),
    origin: "rubygem aixm-#{AIXM::VERSION}",
    region: (region if AIXM.ofmx?),
    namespace: (namespace if AIXM.ofmx?),
    created: @created_at.xmlschema,
    effective: @effective_at.xmlschema
  }.compact
  builder = Builder::XmlMarkup.new(indent: 2)
  builder.instruct!
  builder.tag!(AIXM.schema(:root), meta) do |root|
    root << features.map { |f| f.to_xml }.join.indent(2)
  end
end

#valid?Boolean

Validate the generated AIXM or OFMX atainst it's XSD.


116
117
118
# File 'lib/aixm/document.rb', line 116

def valid?
  errors.none?
end