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
13 has_many :ways, :through => :way_nodes
14 has_many :old_nodes, :foreign_key => :id
18 # Sanity check the latitude and longitude and add an error if it's broken
20 errors.add_to_base("Node is not in the world") unless in_world?
24 # Search for nodes matching tags within bounding_box
26 # Also adheres to limitations such as within max_number_of_nodes
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 })
34 # using named placeholders http://www.robbyonrails.com/articles/2005/10/21/using-named-placeholders-in-ruby
38 #conditions_hash.each do |key,value|
39 # keys << "#{key} = :#{key}"
40 # values[key.to_sym] = value
42 #conditions = keys.join(' AND ')
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)
49 # Read in xml as text and return it's Node object representation
50 def self.from_xml(xml, create=false)
58 doc.find('//osm/node').each do |pt|
59 node.lat = pt['lat'].to_f
60 node.lon = pt['lon'].to_f
62 return nil unless node.in_world?
66 node.id = pt['id'].to_i
70 node.visible = pt['visible'] and pt['visible'] == 'true'
73 node.timestamp = Time.now
76 node.timestamp = Time.parse(pt['timestamp'])
82 pt.find('tag').each do |tag|
83 tags << [tag['k'],tag['v']]
86 node.tags = Tags.join(tags)
95 # Save this node with the appropriate OldNode object to represent it's history.
96 def save_with_history!
98 self.timestamp = Time.now
100 old_node = OldNode.from_node(self)
105 # Turn this Node in to a complete OSM XML object with <osm> wrapper
107 doc = OSM::API.new.get_xml_doc
108 doc.root << to_xml_node()
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
119 user_display_name_cache = {} if user_display_name_cache.nil?
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
126 user_display_name_cache[self.user_id] = nil
129 el1['user'] = user_display_name_cache[self.user_id] unless user_display_name_cache[self.user_id].nil?
131 Tags.split(self.tags) do |k,v|
132 el2 = XML::Node.new('tag')
138 el1['visible'] = self.visible.to_s
139 el1['timestamp'] = self.timestamp.xmlschema
143 # Return the node's tags as a Hash of keys and their values
146 Tags.split(self.tags) do |k,v|