1 class Node < ActiveRecord::Base
3 set_table_name 'current_nodes'
5 validates_presence_of :user_id, :timestamp
6 validates_inclusion_of :visible, :in => [ true, false ]
7 validates_numericality_of :latitude, :longitude
8 validate :validate_position
10 has_many :old_nodes, :foreign_key => :id
13 before_save :update_tile
15 def self.find_by_area(minlat, minlon, maxlat, maxlon, options)
16 self.with_scope(:find => {:conditions => OSM.sql_for_area(minlat, minlon, maxlat, maxlon)}) do
17 return self.find(:all, options)
22 self.tile = QuadTile.tile_for_point(lat, lon)
26 self.latitude = (l * 10000000).round
30 self.longitude = (l * 10000000).round
34 return self.latitude.to_f / 10000000
38 return self.longitude.to_f / 10000000
42 errors.add_to_base("Node is not in the world") unless in_world?
46 return false if self.lat < -90 or self.lat > 90
47 return false if self.lon < -180 or self.lon > 180
51 def self.from_xml(xml, create=false)
59 doc.find('//osm/node').each do |pt|
60 node.lat = pt['lat'].to_f
61 node.lon = pt['lon'].to_f
63 return nil unless node.in_world?
67 node.id = pt['id'].to_i
71 node.visible = pt['visible'] and pt['visible'] == 'true'
74 node.timestamp = Time.now
77 node.timestamp = Time.parse(pt['timestamp'])
83 pt.find('tag').each do |tag|
84 tags << [tag['k'],tag['v']]
87 tags = tags.collect { |k,v| "#{k}=#{v}" }.join(';')
88 tags = '' if tags.nil?
99 def save_with_history!
101 self.timestamp = Time.now
103 old_node = OldNode.from_node(self)
109 doc = OSM::API.new.get_xml_doc
110 doc.root << to_xml_node()
114 def to_xml_node(user_display_name_cache = nil)
115 el1 = XML::Node.new 'node'
116 el1['id'] = self.id.to_s
117 el1['lat'] = self.lat.to_s
118 el1['lon'] = self.lon.to_s
120 user_display_name_cache = {} if user_display_name_cache.nil?
122 if user_display_name_cache and user_display_name_cache.key?(self.user_id)
123 # use the cache if available
124 elsif self.user.data_public?
125 user_display_name_cache[self.user_id] = self.user.display_name
127 user_display_name_cache[self.user_id] = nil
130 el1['user'] = user_display_name_cache[self.user_id] unless user_display_name_cache[self.user_id].nil?
132 Node.split_tags(el1, self.tags)
133 el1['visible'] = self.visible.to_s
134 el1['timestamp'] = self.timestamp.xmlschema
138 def self.split_tags(el, tags)
139 tags.split(';').each do |tag|
140 parts = tag.split('=')
143 key = parts[0].strip unless parts[0].nil?
144 val = parts[1].strip unless parts[1].nil?
145 if key != '' && val != ''
146 el2 = XML::Node.new('tag')