]> git.openstreetmap.org Git - rails.git/blob - app/models/node.rb
Fill in license details.
[rails.git] / app / models / node.rb
1 # The node model represents a current existing node, that is, the latest version. Use OldNode for historical nodes.
2
3 class Node < GeoRecord
4   require 'xml/libxml'
5
6   set_table_name 'current_nodes'
7   
8   validates_presence_of :user_id, :timestamp
9   validates_inclusion_of :visible, :in => [ true, false ]
10   validates_numericality_of :latitude, :longitude
11   validate :validate_position
12
13   has_many :ways, :through => :way_nodes
14   has_many :old_nodes, :foreign_key => :id
15   has_many :way_nodes
16   belongs_to :user
17  
18   # Sanity check the latitude and longitude and add an error if it's broken
19   def validate_position
20     errors.add_to_base("Node is not in the world") unless in_world?
21   end
22
23   #
24   # Search for nodes matching tags within bounding_box
25   #
26   # Also adheres to limitations such as within max_number_of_nodes
27   #
28   def self.search(bounding_box, tags = {})
29     min_lon, min_lat, max_lon, max_lat = *bounding_box
30     # @fixme a bit of a hack to search for only visible nodes
31     # couldn't think of another to add to tags condition
32     #conditions_hash = tags.merge({ 'visible' => 1 })
33   
34     # using named placeholders http://www.robbyonrails.com/articles/2005/10/21/using-named-placeholders-in-ruby
35     #keys = []
36     #values = {}
37
38     #conditions_hash.each do |key,value|
39     #  keys <<  "#{key} = :#{key}"
40     #  values[key.to_sym] = value
41     #end 
42     #conditions = keys.join(' AND ')
43  
44     find_by_area(min_lat, min_lon, max_lat, max_lon,
45                     :conditions => 'visible = 1',
46                     :limit => APP_CONFIG['max_number_of_nodes']+1)  
47   end
48
49   # Read in xml as text and return it's Node object representation
50   def self.from_xml(xml, create=false)
51     begin
52       p = XML::Parser.new
53       p.string = xml
54       doc = p.parse
55   
56       node = Node.new
57
58       doc.find('//osm/node').each do |pt|
59         node.lat = pt['lat'].to_f
60         node.lon = pt['lon'].to_f
61
62         return nil unless node.in_world?
63
64         unless create
65           if pt['id'] != '0'
66             node.id = pt['id'].to_i
67           end
68         end
69
70         node.visible = pt['visible'] and pt['visible'] == 'true'
71
72         if create
73           node.timestamp = Time.now
74         else
75           if pt['timestamp']
76             node.timestamp = Time.parse(pt['timestamp'])
77           end
78         end
79
80         tags = []
81
82         pt.find('tag').each do |tag|
83           tags << [tag['k'],tag['v']]
84         end
85
86         node.tags = Tags.join(tags)
87       end
88     rescue
89       node = nil
90     end
91
92     return node
93   end
94
95   # Save this node with the appropriate OldNode object to represent it's history.
96   def save_with_history!
97     Node.transaction do
98       self.timestamp = Time.now
99       self.save!
100       old_node = OldNode.from_node(self)
101       old_node.save!
102     end
103   end
104
105   # Turn this Node in to a complete OSM XML object with <osm> wrapper
106   def to_xml
107     doc = OSM::API.new.get_xml_doc
108     doc.root << to_xml_node()
109     return doc
110   end
111
112   # Turn this Node in to an XML Node without the <osm> wrapper.
113   def to_xml_node(user_display_name_cache = nil)
114     el1 = XML::Node.new 'node'
115     el1['id'] = self.id.to_s
116     el1['lat'] = self.lat.to_s
117     el1['lon'] = self.lon.to_s
118
119     user_display_name_cache = {} if user_display_name_cache.nil?
120
121     if user_display_name_cache and user_display_name_cache.key?(self.user_id)
122       # use the cache if available
123     elsif self.user.data_public?
124       user_display_name_cache[self.user_id] = self.user.display_name
125     else
126       user_display_name_cache[self.user_id] = nil
127     end
128
129     el1['user'] = user_display_name_cache[self.user_id] unless user_display_name_cache[self.user_id].nil?
130
131     Tags.split(self.tags) do |k,v|
132       el2 = XML::Node.new('tag')
133       el2['k'] = k.to_s
134       el2['v'] = v.to_s
135       el1 << el2
136     end
137
138     el1['visible'] = self.visible.to_s
139     el1['timestamp'] = self.timestamp.xmlschema
140     return el1
141   end
142
143   # Return the node's tags as a Hash of keys and their values
144   def tags_as_hash
145     hash = {}
146     Tags.split(self.tags) do |k,v|
147       hash[k] = v
148     end
149     hash
150   end
151 end