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