Class OSM::Database
In: lib/OSM/Database.rb
Parent: Object

An OSM database. It holds nodes, ways, and relations in memory. Contents are unordered.

If you add an object to the database with the same ID as a previously added object, the old object will be silently deleted from the database and the new one will be added.

Usage:

  require 'OSM/Database'
  db = OSM::Database.new

Methods

<<   add_node   add_relation   add_way   clear   get_node   get_relation   get_way   new   to_xml  

Attributes

nodes  [R]  a hash of all nodes
relations  [R]  a hash of all relations
version  [RW]  OpenStreetMap API version of this database
ways  [R]  a hash of all ways

Public Class methods

Create an empty database.

version:OpenStreetMap API Version (String, Default: @@DEFAULT_API_VERSION)

[Source]

    # File lib/OSM/Database.rb, line 41
41:         def initialize(version = @@DEFAULT_API_VERSION)
42:             @version = version
43:             @nodes     = Hash.new
44:             @ways      = Hash.new
45:             @relations = Hash.new
46:         end

Public Instance methods

Add an object (Node, Way, or Relation) to the database.

object:object of class Node, Way, or Relation

[Source]

     # File lib/OSM/Database.rb, line 120
120:         def <<(object)
121:             case object
122:                 when OSM::Node     then add_node(object)
123:                 when OSM::Way      then add_way(object)
124:                 when OSM::Relation then add_relation(object)
125:                 else raise ArgumentError.new('Can only add objects of classes OSM::Node, OSM::Way, or OSM::Relation')
126:             end
127:             self    # return self so calls can be chained
128:         end

Add a Node to the database.

[Source]

    # File lib/OSM/Database.rb, line 65
65:         def add_node(node)
66:             id = node.id.to_i
67:             @nodes[id].db = nil if @nodes[id] # remove old object with same id from database
68:             @nodes[id] = node
69:             node.db = self
70:         end

Add a Relation to the database.

[Source]

    # File lib/OSM/Database.rb, line 81
81:         def add_relation(relation)
82:             id = relation.id.to_i
83:             @relations[id].db = nil if @relations[id] # remove old object with same id from database
84:             @relations[id] = relation
85:             relation.db = self
86:         end

Add a Way to the database.

[Source]

    # File lib/OSM/Database.rb, line 73
73:         def add_way(way)
74:             id = way.id.to_i
75:             @ways[id].db = nil if @ways[id] # remove old object with same id from database
76:             @ways[id] = way
77:             way.db = self
78:         end

Delete all nodes, ways and relations from the database. You should call this before deleting a database to break internal loop references.

[Source]

    # File lib/OSM/Database.rb, line 54
54:         def clear
55:             @nodes.each_value{     |obj| obj.db = nil }
56:             @ways.each_value{      |obj| obj.db = nil }
57:             @relations.each_value{ |obj| obj.db = nil }
58: 
59:             @nodes     = Hash.new
60:             @ways      = Hash.new
61:             @relations = Hash.new
62:         end

Get node from the database with given ID. Returns nil if there is no node with this ID.

[Source]

    # File lib/OSM/Database.rb, line 93
93:         def get_node(id)
94:             @nodes[id.to_i]
95:         end

Get relation from the database with given ID. Returns nil if there is no relation with this ID.

[Source]

     # File lib/OSM/Database.rb, line 111
111:         def get_relation(id)
112:             @relations[id.to_i]
113:         end

Get way from the database with given ID. Returns nil if there is no way with this ID.

[Source]

     # File lib/OSM/Database.rb, line 102
102:         def get_way(id)
103:             @ways[id.to_i]
104:         end

Dump database to XML. This uses the XML Builder library

doc:Builder::XmlMarkup object
generator:Name of generator to put in generator attribute of osm element (String, Default: @@DEFAULT_XML_GENERATOR)

This method is used like this:

  db = OSM::Database.new
  # add data to database...
  doc = Builder::XmlMarkup.new(:indent => 2, :target => STDOUT)
  doc.instruct!
  db.to_xml(doc, 'test')

[Source]

     # File lib/OSM/Database.rb, line 143
143:         def to_xml(doc, generator=@@DEFAULT_XML_GENERATOR)
144:             doc.osm(:generator => generator, :version => version) do |xml|
145:                 nodes.each_value do |node|
146:                     node.to_xml(xml)
147:                 end
148:                 ways.each_value do |way|
149:                     way.to_xml(xml)
150:                 end
151:                 relations.each_value do |way|
152:                     way.to_xml(xml)
153:                 end
154:             end
155:         end

[Validate]