4 set_table_name 'current_nodes'
6 validates_presence_of :user_id, :timestamp
7 validates_inclusion_of :visible, :in => [ true, false ]
8 validates_numericality_of :latitude, :longitude
9 validate :validate_position
11 has_many :old_nodes, :foreign_key => :id
13 has_many :node_tags, :foreign_key => :id
17 errors.add_to_base("Node is not in the world") unless in_world?
21 return false if self.lat < -90 or self.lat > 90
22 return false if self.lon < -180 or self.lon > 180
27 # Search for nodes matching tags within bounding_box
29 # Also adheres to limitations such as within max_number_of_nodes
31 def self.search(bounding_box, tags = {})
32 min_lon, min_lat, max_lon, max_lat = *bounding_box
33 # @fixme a bit of a hack to search for only visible nodes
34 # couldn't think of another to add to tags condition
35 #conditions_hash = tags.merge({ 'visible' => 1 })
37 # using named placeholders http://www.robbyonrails.com/articles/2005/10/21/using-named-placeholders-in-ruby
41 #conditions_hash.each do |key,value|
42 # keys << "#{key} = :#{key}"
43 # values[key.to_sym] = value
45 #conditions = keys.join(' AND ')
47 find_by_area(min_lat, min_lon, max_lat, max_lon,
48 :conditions => 'visible = 1',
49 :limit => APP_CONFIG['max_number_of_nodes']+1)
52 # Read in xml as text and return it's Node object representation
53 def self.from_xml(xml, create=false)
59 doc.find('//osm/node').each do |pt|
60 return Node.from_xml_node(pt, create)
67 def self.from_xml_node(pt, create=false)
70 node.version = pt['version']
71 node.lat = pt['lat'].to_f
72 node.lon = pt['lon'].to_f
74 return nil unless node.in_world?
78 node.id = pt['id'].to_i
82 node.visible = pt['visible'] and pt['visible'] == 'true'
85 node.timestamp = Time.now
88 node.timestamp = Time.parse(pt['timestamp'])
94 pt.find('tag').each do |tag|
95 node.add_tag_key_val(tag['k'],tag['v'])
101 def save_with_history!
110 NodeTag.delete_all(['id = ?', self.id])
120 old_node = OldNode.from_node(self)
121 old_node.timestamp = t
122 old_node.save_with_dependencies!
126 def delete_with_history(user)
128 if WayNode.find(:first, :joins => "INNER JOIN current_ways ON current_ways.id = current_way_nodes.id", :conditions => [ "current_ways.visible = 1 AND current_way_nodes.node_id = ?", self.id ])
129 raise OSM::APIPreconditionFailedError.new
130 elsif RelationMember.find(:first, :joins => "INNER JOIN current_relations ON current_relations.id=current_relation_members.id", :conditions => [ "visible = 1 AND member_type='node' and member_id=?", self.id])
131 raise OSM::APIPreconditionFailedError.new
133 self.user_id = user.id
138 raise OSM::APIAlreadyDeletedError.new
142 def update_from(new_node, user)
143 self.user_id = user.id
144 self.latitude = new_node.latitude
145 self.longitude = new_node.longitude
146 self.tags = new_node.tags
152 doc = OSM::API.new.get_xml_doc
153 doc.root << to_xml_node()
157 def to_xml_node(user_display_name_cache = nil)
158 el1 = XML::Node.new 'node'
159 el1['id'] = self.id.to_s
160 el1['lat'] = self.lat.to_s
161 el1['lon'] = self.lon.to_s
163 user_display_name_cache = {} if user_display_name_cache.nil?
165 if user_display_name_cache and user_display_name_cache.key?(self.user_id)
166 # use the cache if available
167 elsif self.user.data_public?
168 user_display_name_cache[self.user_id] = self.user.display_name
170 user_display_name_cache[self.user_id] = nil
173 el1['user'] = user_display_name_cache[self.user_id] unless user_display_name_cache[self.user_id].nil?
175 self.tags.each do |k,v|
176 el2 = XML::Node.new('tag')
182 el1['visible'] = self.visible.to_s
183 el1['timestamp'] = self.timestamp.xmlschema
184 el1['version'] = self.version.to_s
190 Tags.split(self.tags) do |k,v|
199 self.node_tags.each do |tag|
210 def add_tag_key_val(k,v)
211 @tags = Hash.new unless @tags