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