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