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