| 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
| 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 |
Add an object (Node, Way, or Relation) to the database.
| object: | object of class Node, Way, or Relation |
# 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
Delete all nodes, ways and relations from the database. You should call this before deleting a database to break internal loop references.
# 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.
# 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.
# 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.
# 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')
# 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