1 # The node model represents a current existing node, that is, the latest version. Use OldNode for historical nodes.
6 set_table_name 'current_nodes'
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
15 has_many :old_nodes, :foreign_key => :id
18 has_many :ways, :through => :way_nodes
20 has_many :containing_relation_members, :as => :member
21 has_many :containing_relations, :through => :containing_relation_members
23 # Sanity check the latitude and longitude and add an error if it's broken
25 errors.add_to_base("Node is not in the world") unless in_world?
29 # Search for nodes matching tags within bounding_box
31 # Also adheres to limitations such as within max_number_of_nodes
33 def self.search(bounding_box, tags = {})
34 min_lon, min_lat, max_lon, max_lat = *bounding_box
35 # @fixme a bit of a hack to search for only visible nodes
36 # couldn't think of another to add to tags condition
37 #conditions_hash = tags.merge({ 'visible' => 1 })
39 # using named placeholders http://www.robbyonrails.com/articles/2005/10/21/using-named-placeholders-in-ruby
43 #conditions_hash.each do |key,value|
44 # keys << "#{key} = :#{key}"
45 # values[key.to_sym] = value
47 #conditions = keys.join(' AND ')
49 find_by_area(min_lat, min_lon, max_lat, max_lon,
50 :conditions => 'visible = 1',
51 :limit => APP_CONFIG['max_number_of_nodes']+1)
54 # Read in xml as text and return it's Node object representation
55 def self.from_xml(xml, create=false)
63 doc.find('//osm/node').each do |pt|
64 node.lat = pt['lat'].to_f
65 node.lon = pt['lon'].to_f
67 return nil unless node.in_world?
71 node.id = pt['id'].to_i
75 node.visible = pt['visible'] and pt['visible'] == 'true'
78 node.timestamp = Time.now
81 node.timestamp = Time.parse(pt['timestamp'])
87 pt.find('tag').each do |tag|
88 tags << [tag['k'],tag['v']]
91 node.tags = Tags.join(tags)
100 # Save this node with the appropriate OldNode object to represent it's history.
101 def save_with_history!
103 self.timestamp = Time.now
105 old_node = OldNode.from_node(self)
110 # Turn this Node in to a complete OSM XML object with <osm> wrapper
112 doc = OSM::API.new.get_xml_doc
113 doc.root << to_xml_node()
117 # Turn this Node in to an XML Node without the <osm> wrapper.
118 def to_xml_node(user_display_name_cache = nil)
119 el1 = XML::Node.new 'node'
120 el1['id'] = self.id.to_s
121 el1['lat'] = self.lat.to_s
122 el1['lon'] = self.lon.to_s
124 user_display_name_cache = {} if user_display_name_cache.nil?
126 if user_display_name_cache and user_display_name_cache.key?(self.user_id)
127 # use the cache if available
128 elsif self.user.data_public?
129 user_display_name_cache[self.user_id] = self.user.display_name
131 user_display_name_cache[self.user_id] = nil
134 el1['user'] = user_display_name_cache[self.user_id] unless user_display_name_cache[self.user_id].nil?
136 Tags.split(self.tags) do |k,v|
137 el2 = XML::Node.new('tag')
143 el1['visible'] = self.visible.to_s
144 el1['timestamp'] = self.timestamp.xmlschema
148 # Return the node's tags as a Hash of keys and their values
151 Tags.split(self.tags) do |k,v|