Class: AIXM::Document

Inherits:
Object show all
Includes:
Association
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(
  namespace: String (UUID)
  created_at: Time or Date or String
  effective_at: Time or Date or String
)
document.add_feature(AIXM::Feature)

Constant Summary collapse

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

Methods included from Association

included

Constructor Details

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

Returns a new instance of Document.


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

def initialize(namespace: nil, created_at: nil, effective_at: nil)
  self.namespace, self.created_at, self.effective_at = namespace, created_at, effective_at
end

Instance Attribute Details

#created_atTime

Returns creation date and time (default: #effective_at or now).

Returns:

  • (Time)

    creation date and time (default: #effective_at or now)


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

def created_at
  @created_at
end

#effective_atTime

Returns effective after date and time (default: #created_at or now).

Returns:

  • (Time)

    effective after date and time (default: #created_at or now)


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

def effective_at
  @effective_at
end

#namespaceString

Returns UUID to namespace the data contained in this document.

Returns:

  • (String)

    UUID to namespace the data contained in this document


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

def namespace
  @namespace
end

Instance Method Details

#add_feature(feature) ⇒ self

Parameters:

Returns:

  • (self)

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

has_many :features, accept: ['AIXM::Feature']

#errorsArray<String>

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

Returns:

  • (Array<String>)

    validation errors


95
96
97
98
99
100
# File 'lib/aixm/document.rb', line 95

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

#featuresArray<AIXM::Feature>

Returns features (e.g. airport or airspace) present in this document.

Returns:


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

has_many :features, accept: ['AIXM::Feature']

#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.

Parameters:

  • max_distance (AIXM::D) (defaults to: AIXM.d(1, :nm))

    max distance between obstacle group member pairs (default: 1 NM)

Returns:

  • (Integer)

    number of obstacle groups added


67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
# File 'lib/aixm/document.rb', line 67

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

#inspectString

Returns:

  • (String)

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

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

#to_xmlString

Returns AIXM or OFMX markup.

Returns:

  • (String)

    AIXM or OFMX markup


103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
# File 'lib/aixm/document.rb', line 103

def to_xml
  meta = {
    'xmlns:xsi': AIXM.schema(:namespace),
    version: AIXM.schema(:version),
    origin: "rubygem aixm-#{AIXM::VERSION}",
    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|
    AIXM::Memoize.method :to_uid do
      root << features.map { _1.to_xml }.join.indent(2)
    end
  end
  if AIXM.ofmx? && AIXM.config.mid
    AIXM::PayloadHash::Mid.new(builder.target!).insert_mid.to_xml
  else
    builder.target!
  end
end

#valid?Boolean

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

Returns:

  • (Boolean)

    whether valid or not


87
88
89
# File 'lib/aixm/document.rb', line 87

def valid?
  errors.none?
end