Class: Rosemary::Way

Inherits:
Element show all
Defined in:
lib/rosemary/way.rb

Overview

OpenStreetMap Way.

To create a new Rosemary::Way object:

way = Rosemary::Way.new()

To get a way from the API:

way = Rosemary::Way.find_way(17)

Instance Attribute Summary collapse

Attributes inherited from Element

#changeset, #id, #tags, #timestamp, #uid, #user, #version

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Element

#[], #[]=, #add_tags, #attributes, from_api, #get_history_from_api, #get_relations_from_api, #initialize_copy, #is_tagged?, #method_missing, #shape, #update_attributes

Constructor Details

#initialize(attrs = {}) ⇒ Way

Create new Way object.

id

ID of this way. If nil a new unique negative ID will be allocated.

user

Username

timestamp

Timestamp of last change

nodes

Array of Node objects and/or node IDs



21
22
23
24
25
# File 'lib/rosemary/way.rb', line 21

def initialize(attrs = {})
  attrs.stringify_keys!
  @nodes = []
  super(attrs)
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method in the class Rosemary::Element

Instance Attribute Details

#nodesArray (readonly)

Array of node IDs in this way.

Returns:

  • (Array)


13
14
15
# File 'lib/rosemary/way.rb', line 13

def nodes
  @nodes
end

Class Method Details

.from_xml(xml_string) ⇒ Rosemary::Way

Instantiate a way from an XML representation.

Parameters:

  • xml_string (String)

    the xml representing a way.

Returns:

  • (Rosemary::Way)

    the way represented by the given XML string.



71
72
73
# File 'lib/rosemary/way.rb', line 71

def self.from_xml(xml_string)
  Parser.call(xml_string, :xml)
end

Instance Method Details

#<<(stuff) ⇒ Rosemary::Way

Add one or more tags or nodes to this way.

The argument can be one of the following:

  • If the argument is a Hash or an OSM::Tags object, those tags are added.

  • If the argument is an OSM::Node object, its ID is added to the list of node IDs.

  • If the argument is an Integer or String containing an Integer, this ID is added to the list of node IDs.

  • If the argument is an Array the function is called recursively, i.e. all items in the Array are added.

Returns the way to allow chaining.

Returns:



44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/rosemary/way.rb', line 44

def <<(stuff)
  case stuff
    when Array  # call this method recursively
      stuff.each do |item|
        self << item
      end
    when Rosemary::Node
      nodes << stuff.id
    when String
      nodes << stuff.to_i
    when Integer
      nodes << stuff
    else
      tags.merge!(stuff)
   end
   self    # return self to allow chaining
end

#<=>(another_way) ⇒ Object



88
89
90
91
92
93
94
95
96
97
98
99
100
# File 'lib/rosemary/way.rb', line 88

def <=>(another_way)
  parent_compare = super(another_way)
  # don't bother to compare more stuff if parent comparison failed
  return parent_compare unless parent_compare == 0

  return -1 if self.send(:tags) != another_way.send(:tags)

  nodes_compare = self.send(:nodes).sort <=> another_way.send(:nodes).sort
  # don't bother to compare more stuff if nodes comparison failed
  return nodes_compare unless nodes_compare == 0

  0
end

#attribute_listObject

The list of attributes for this Way



64
65
66
# File 'lib/rosemary/way.rb', line 64

def attribute_list # :nodoc:
  [:id, :version, :uid, :user, :timestamp, :changeset]
end

#to_xml(options = {}) ⇒ Object



75
76
77
78
79
80
81
82
83
84
85
86
# File 'lib/rosemary/way.rb', line 75

def to_xml(options = {})
  xml = options[:builder] ||= Builder::XmlMarkup.new
  xml.instruct! unless options[:skip_instruct]
  xml.osm(:generator => "rosemary v#{Rosemary::VERSION}", :version => Rosemary::Api::API_VERSION) do
    xml.way(attributes) do
      nodes.each do |node_id|
        xml.nd(:ref => node_id)
      end unless nodes.empty?
      tags.to_xml(:builder => xml, :skip_instruct => true)
    end
  end
end

#typeObject



27
28
29
# File 'lib/rosemary/way.rb', line 27

def type
    'Way'
end